diff --git a/CHANGELOG.md b/CHANGELOG.md index 86b6a8d69f1..e458407801b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unversioned +- Bugfix: Fixed a data race when disconnecting from Twitch PubSub. (#4771) - Dev: Fixed UTF16 encoding of `modes` file for the installer. (#4791) - Dev: Temporarily disable High DPI scaling on Qt6 builds on Windows. (#4767) - Dev: Tests now run on Ubuntu 22.04 instead of 20.04 to loosen C++ restrictions in tests. (#4774) diff --git a/src/providers/twitch/PubSubClient.cpp b/src/providers/twitch/PubSubClient.cpp index c35d9a41839..79232ef9cbd 100644 --- a/src/providers/twitch/PubSubClient.cpp +++ b/src/providers/twitch/PubSubClient.cpp @@ -45,22 +45,30 @@ void PubSubClient::stop() void PubSubClient::close(const std::string &reason, websocketpp::close::status::value code) { - WebsocketErrorCode ec; - - auto conn = this->websocketClient_.get_con_from_hdl(this->handle_, ec); - if (ec) - { - qCDebug(chatterinoPubSub) - << "Error getting con:" << ec.message().c_str(); - return; - } - - conn->close(code, reason, ec); - if (ec) - { - qCDebug(chatterinoPubSub) << "Error closing:" << ec.message().c_str(); - return; - } + boost::asio::post( + this->websocketClient_.get_io_service().get_executor(), + [this, reason, code] { + // We need to post this request to the io service executor + // to ensure the weak pointer used in get_con_from_hdl is used in a safe way + WebsocketErrorCode ec; + + auto conn = + this->websocketClient_.get_con_from_hdl(this->handle_, ec); + if (ec) + { + qCDebug(chatterinoPubSub) + << "Error getting con:" << ec.message().c_str(); + return; + } + + conn->close(code, reason, ec); + if (ec) + { + qCDebug(chatterinoPubSub) + << "Error closing:" << ec.message().c_str(); + return; + } + }); } bool PubSubClient::listen(PubSubListenMessage msg)