Skip to content

Commit b271cf9

Browse files
authored
Merge pull request #692 from Palakis/fix/obs-shutdown-crash
Events: Fix multiple shutdown crashes
2 parents 0a08412 + 60e2f01 commit b271cf9

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/WSEvents.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ void WSEvents::FrontendEventHandler(enum obs_frontend_event event, void* private
249249
void WSEvents::broadcastUpdate(const char* updateType,
250250
obs_data_t* additionalFields = nullptr)
251251
{
252+
if (!_srv->isListening()) {
253+
return;
254+
}
252255
std::optional<uint64_t> streamTime;
253256
if (obs_frontend_streaming_active()) {
254257
streamTime = std::make_optional(getStreamingTime());

src/WSServer.cpp

+39-9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ WSServer::~WSServer()
6060
stop();
6161
}
6262

63+
void WSServer::serverRunner()
64+
{
65+
blog(LOG_INFO, "IO thread started.");
66+
try {
67+
_server.run();
68+
} catch (websocketpp::exception const & e) {
69+
blog(LOG_ERROR, "websocketpp instance returned an error: %s", e.what());
70+
} catch (const std::exception & e) {
71+
blog(LOG_ERROR, "websocketpp instance returned an error: %s", e.what());
72+
} catch (...) {
73+
blog(LOG_ERROR, "websocketpp instance returned an error");
74+
}
75+
blog(LOG_INFO, "IO thread exited.");
76+
}
77+
6378
void WSServer::start(quint16 port, bool lockToIPv4)
6479
{
6580
if (_server.is_listening() && (port == _serverPort && _lockToIPv4 == lockToIPv4)) {
@@ -102,11 +117,7 @@ void WSServer::start(quint16 port, bool lockToIPv4)
102117

103118
_server.start_accept();
104119

105-
QtConcurrent::run([=]() {
106-
blog(LOG_INFO, "io thread started");
107-
_server.run();
108-
blog(LOG_INFO, "io thread exited");
109-
});
120+
_serverThread = std::thread(&WSServer::serverRunner, this);
110121

111122
blog(LOG_INFO, "server started successfully on port %d", _serverPort);
112123
}
@@ -119,7 +130,18 @@ void WSServer::stop()
119130

120131
_server.stop_listening();
121132
for (connection_hdl hdl : _connections) {
122-
_server.close(hdl, websocketpp::close::status::going_away, "Server stopping");
133+
websocketpp::lib::error_code errorCode;
134+
_server.pause_reading(hdl, errorCode);
135+
if (errorCode) {
136+
blog(LOG_ERROR, "Error: %s", errorCode.message().c_str());
137+
continue;
138+
}
139+
140+
_server.close(hdl, websocketpp::close::status::going_away, "Server stopping", errorCode);
141+
if (errorCode) {
142+
blog(LOG_ERROR, "Error: %s", errorCode.message().c_str());
143+
continue;
144+
}
123145
}
124146

125147
_threadPool.waitForDone();
@@ -128,6 +150,8 @@ void WSServer::stop()
128150
std::this_thread::sleep_for(std::chrono::milliseconds(10));
129151
}
130152

153+
_serverThread.join();
154+
131155
blog(LOG_INFO, "server stopped successfully");
132156
}
133157

@@ -160,6 +184,11 @@ void WSServer::broadcast(const RpcEvent& event)
160184
}
161185
}
162186

187+
bool WSServer::isListening()
188+
{
189+
return _server.is_listening();
190+
}
191+
163192
void WSServer::onOpen(connection_hdl hdl)
164193
{
165194
QMutexLocker locker(&_clMutex);
@@ -217,11 +246,12 @@ void WSServer::onClose(connection_hdl hdl)
217246

218247
auto conn = _server.get_con_from_hdl(hdl);
219248
auto localCloseCode = conn->get_local_close_code();
249+
auto localCloseReason = conn->get_local_close_reason();
250+
QString clientIp = getRemoteEndpoint(hdl);
220251

221-
if (localCloseCode != websocketpp::close::status::going_away) {
222-
QString clientIp = getRemoteEndpoint(hdl);
252+
blog(LOG_INFO, "Websocket connection with client '%s' closed (disconnected). Code is %d, reason is: '%s'", clientIp.toUtf8().constData(), localCloseCode, localCloseReason.c_str());
253+
if (localCloseCode != websocketpp::close::status::going_away && _server.is_listening()) {
223254
notifyDisconnection(clientIp);
224-
blog(LOG_INFO, "client %s disconnected", clientIp.toUtf8().constData());
225255
}
226256
}
227257

src/WSServer.h

+4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ Q_OBJECT
4747
void start(quint16 port, bool lockToIPv4);
4848
void stop();
4949
void broadcast(const RpcEvent& event);
50+
bool isListening();
5051
QThreadPool* threadPool() {
5152
return &_threadPool;
5253
}
5354

5455
private:
56+
void serverRunner();
57+
5558
void onOpen(connection_hdl hdl);
5659
void onMessage(connection_hdl hdl, server::message_ptr message);
5760
void onClose(connection_hdl hdl);
@@ -60,6 +63,7 @@ Q_OBJECT
6063
void notifyConnection(QString clientIp);
6164
void notifyDisconnection(QString clientIp);
6265

66+
std::thread _serverThread;
6367
server _server;
6468
quint16 _serverPort;
6569
bool _lockToIPv4;

0 commit comments

Comments
 (0)