Skip to content

Commit

Permalink
Merge pull request #1124 from roscaviktor/async-terminate-crash
Browse files Browse the repository at this point in the history
Fix MQTTAsync_terminate() to re-check the number of handles after re-acquisition of mqttasync_mutex
  • Loading branch information
icraggs authored Sep 30, 2021
2 parents d1744cc + ee364a6 commit 286d65a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/MQTTAsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -629,15 +629,14 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
m->connect.context = options->context;
m->connectTimeout = options->connectTimeout;

MQTTAsync_tostop = 0;

/* don't lock async mutex if we are being called from a callback */
thread_id = Thread_getid();
if (thread_id != sendThread_id && thread_id != receiveThread_id)
{
MQTTAsync_lock_mutex(mqttasync_mutex);
locked = 1;
}
MQTTAsync_tostop = 0;
if (sendThread_state != STARTING && sendThread_state != RUNNING)
{
sendThread_state = STARTING;
Expand Down
6 changes: 4 additions & 2 deletions src/MQTTAsyncUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ void MQTTAsync_terminate(void)
{
FUNC_ENTRY;
MQTTAsync_stop();
if (global_initialized)

/* don't destroy global data if a new client was created while waiting for background threads to terminate */
if (global_initialized && bstate->clients->count == 0)
{
ListElement* elem = NULL;
ListFree(bstate->clients);
Expand Down Expand Up @@ -2309,7 +2311,7 @@ static void MQTTAsync_stop(void)
{
int count = 0;
MQTTAsync_tostop = 1;
while ((sendThread_state != STOPPED || receiveThread_state != STOPPED) && ++count < 100)
while ((sendThread_state != STOPPED || receiveThread_state != STOPPED) && MQTTAsync_tostop != 0 && ++count < 100)
{
MQTTAsync_unlock_mutex(mqttasync_mutex);
Log(TRACE_MIN, -1, "sleeping");
Expand Down

0 comments on commit 286d65a

Please sign in to comment.