Merge pull request #1124 from roscaviktor/async-terminate-crash

Fix MQTTAsync_terminate() to re-check the number of handles after re-acquisition of mqttasync_mutex
This commit is contained in:
Ian Craggs 2021-09-30 16:39:45 +01:00 committed by GitHub
commit 286d65abd6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 4 deletions

View File

@ -629,8 +629,6 @@ 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)
@ -638,6 +636,7 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
MQTTAsync_lock_mutex(mqttasync_mutex);
locked = 1;
}
MQTTAsync_tostop = 0;
if (sendThread_state != STARTING && sendThread_state != RUNNING)
{
sendThread_state = STARTING;

View File

@ -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);
@ -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");