Skip to content

Commit

Permalink
Add mutex protection to MqttClient_NetDisconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
embhorn committed Jul 14, 2023
1 parent 2c95cb5 commit 1551e05
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -2609,7 +2609,35 @@ int MqttClient_NetConnect(MqttClient *client, const char* host,

int MqttClient_NetDisconnect(MqttClient *client)
{
return MqttSocket_Disconnect(client);
int rc;

if (client == NULL) {
return MQTT_CODE_ERROR_BAD_ARG;
}

#ifdef WOLFMQTT_MULTITHREAD
MqttPendResp *tmpResp;

/* Get locks on all mutexes to ensure no other threads are active */
wm_SemLock(&client->lockSend);
wm_SemLock(&client->lockRecv);
wm_SemLock(&client->lockClient);

for (tmpResp = client->firstPendResp;
tmpResp != NULL;
tmpResp = tmpResp->next) {
MqttClient_RespList_Remove(client, tmpResp);
}
#endif

rc = MqttSocket_Disconnect(client);

#ifdef WOLFMQTT_MULTITHREAD
wm_SemUnlock(&client->lockClient);
wm_SemUnlock(&client->lockRecv);
wm_SemUnlock(&client->lockSend);
#endif
return rc;
}

int MqttClient_GetProtocolVersion(MqttClient *client)
Expand Down

0 comments on commit 1551e05

Please sign in to comment.