Skip to content

Commit

Permalink
Fix issue with publish write only incorrectly removing pending respon…
Browse files Browse the repository at this point in the history
…se. ZD 16769. Remove extra test non-block in `MqttClient_Publish_ReadPayload` (already handled in `MqttSocket_Read`). Cleanup return code 0=success.
  • Loading branch information
dgarske committed Nov 8, 2023
1 parent dd75c2f commit ec477df
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static int MqttClient_Publish_ReadPayload(MqttClient* client,

static int MqttWriteStart(MqttClient* client, MqttMsgStat* stat)
{
int rc = 0;
int rc = MQTT_CODE_SUCCESS;

#ifdef WOLFMQTT_MULTITHREAD
#ifdef WOLFMQTT_DEBUG_CLIENT
Expand Down Expand Up @@ -217,7 +217,7 @@ static void MqttWriteStop(MqttClient* client, MqttMsgStat* stat)

static int MqttReadStart(MqttClient* client, MqttMsgStat* stat)
{
int rc = 0;
int rc = MQTT_CODE_SUCCESS;

#ifdef WOLFMQTT_MULTITHREAD
#ifdef WOLFMQTT_DEBUG_CLIENT
Expand Down Expand Up @@ -362,6 +362,7 @@ void MqttClient_RespList_Remove(MqttClient *client, MqttPendResp *rmResp)
#endif
}

/* return codes: 0=not found, 1=found */
int MqttClient_RespList_Find(MqttClient *client,
MqttPacketType packet_type, word16 packet_id, MqttPendResp **retResp)
{
Expand Down Expand Up @@ -1318,7 +1319,7 @@ static int MqttClient_WaitType(MqttClient *client, void *packet_obj,
}

client->write.len = rc;
rc = 0;
rc = MQTT_CODE_SUCCESS;

mms_stat->write = MQTT_MSG_HEADER;
}
Expand All @@ -1339,7 +1340,7 @@ static int MqttClient_WaitType(MqttClient *client, void *packet_obj,
#endif
MqttWriteStop(client, mms_stat);
if (rc == xfer) {
rc = 0; /* success */
rc = MQTT_CODE_SUCCESS; /* success */
}

mms_stat->write = MQTT_MSG_BEGIN; /* reset write state */
Expand Down Expand Up @@ -1722,15 +1723,6 @@ static int MqttClient_Publish_ReadPayload(MqttClient* client,

/* make sure there is something to read */
if (msg_len > 0) {
#if defined(WOLFMQTT_NONBLOCK) && defined(WOLFMQTT_TEST_NONBLOCK)
static int testNbAlt = 0;
if (!testNbAlt) {
testNbAlt = 1;
return MQTT_CODE_CONTINUE;
}
testNbAlt = 0;
#endif

rc = MqttSocket_Read(client, client->rx_buf, msg_len,
timeout_ms);
if (rc < 0) {
Expand Down Expand Up @@ -2036,9 +2028,14 @@ static int MqttPublishMsg(MqttClient *client, MqttPublish *publish,

#ifdef WOLFMQTT_MULTITHREAD
if (writeOnly) {
/* another thread will handle the wait type */
/* another thread will handle response */
/* check if response already received from other thread */
rc = MqttClient_CheckPendResp(client, resp_type,
publish->packet_id);
if (rc == MQTT_CODE_CONTINUE) {
/* mark success, let other thread handle response */
rc = MQTT_CODE_SUCCESS;
}
}
else
#endif
Expand All @@ -2048,17 +2045,18 @@ static int MqttPublishMsg(MqttClient *client, MqttPublish *publish,
/* Wait for publish response packet */
rc = MqttClient_WaitType(client, &publish->resp, resp_type,
publish->packet_id, client->cmd_timeout_ms);

#if defined(WOLFMQTT_NONBLOCK) || defined(WOLFMQTT_MULTITHREAD)
if (rc == MQTT_CODE_CONTINUE)
break;
#endif
#ifdef WOLFMQTT_MULTITHREAD
if (wm_SemLock(&client->lockClient) == 0) {
MqttClient_RespList_Remove(client, &publish->pendResp);
wm_SemUnlock(&client->lockClient);
}
#endif
}
#if defined(WOLFMQTT_NONBLOCK) || defined(WOLFMQTT_MULTITHREAD)
if (rc == MQTT_CODE_CONTINUE)
break;
#endif
#ifdef WOLFMQTT_MULTITHREAD
if (wm_SemLock(&client->lockClient) == 0) {
MqttClient_RespList_Remove(client, &publish->pendResp);
wm_SemUnlock(&client->lockClient);
}
#endif
}
break;
}
Expand Down

0 comments on commit ec477df

Please sign in to comment.