Skip to content

Commit

Permalink
Fix data race: Call close in the IO Service executor in PubSub.cpp
Browse files Browse the repository at this point in the history
This fixes a data race with the get_con_from_hdl function
  • Loading branch information
pajlada committed Aug 20, 2023
1 parent 381d5c4 commit eac7135
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/providers/twitch/PubSubClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit eac7135

Please sign in to comment.