Skip to content

Commit

Permalink
Fix potential deadlock in set_callback
Browse files Browse the repository at this point in the history
Signed-off-by: Sandro Scherer <[email protected]>
  • Loading branch information
sandro97git committed Oct 8, 2024
1 parent 6131a30 commit 5956f3b
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/async_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,10 @@ void async_client::remove_token(token* tok)

void async_client::set_callback(callback& cb)
{
guard g(lock_);
userCallback_ = &cb;

{
guard g(lock_);
userCallback_ = &cb;
}
int rc = MQTTAsync_setConnected(cli_, this, &async_client::on_connected);

if (rc == MQTTASYNC_SUCCESS) {
Expand All @@ -330,6 +331,7 @@ void async_client::set_callback(callback& cb)
MQTTAsync_setConnected(cli_, nullptr, nullptr);

if (rc != MQTTASYNC_SUCCESS) {
guard g(lock_);
userCallback_ = nullptr;
throw exception(rc);
}
Expand Down

0 comments on commit 5956f3b

Please sign in to comment.