From c2af6af0925d25f61e7c71a18524891eed89743f Mon Sep 17 00:00:00 2001 From: saidrhs Date: Wed, 23 Aug 2023 15:27:21 -0700 Subject: [PATCH] Fix race condition in handleKeepAlive --- source/core_mqtt.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/source/core_mqtt.c b/source/core_mqtt.c index 1efb3520a..340321636 100644 --- a/source/core_mqtt.c +++ b/source/core_mqtt.c @@ -1344,6 +1344,7 @@ static MQTTStatus_t handleKeepAlive( MQTTContext_t * pContext ) MQTTStatus_t status = MQTTSuccess; uint32_t now = 0U; uint32_t packetTxTimeoutMs = 0U; + uint32_t lastPacketTxTime = 0U; assert( pContext != NULL ); assert( pContext->getTime != NULL ); @@ -1369,7 +1370,11 @@ static MQTTStatus_t handleKeepAlive( MQTTContext_t * pContext ) } else { - if( ( packetTxTimeoutMs != 0U ) && ( calculateElapsedTime( now, pContext->lastPacketTxTime ) >= packetTxTimeoutMs ) ) + MQTT_PRE_STATE_UPDATE_HOOK( pContext ); + lastPacketTxTime = pContext->lastPacketTxTime; + MQTT_POST_STATE_UPDATE_HOOK( pContext ); + + if( ( packetTxTimeoutMs != 0U ) && ( calculateElapsedTime( now, lastPacketTxTime ) >= packetTxTimeoutMs ) ) { status = MQTT_Ping( pContext ); }