From f7624c4afd92584e0f003da503c658d9c019af92 Mon Sep 17 00:00:00 2001 From: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com> Date: Mon, 4 Dec 2023 12:19:43 -0800 Subject: [PATCH] Handled persistent session mutex relinquish correctly --- Common/app/mqtt/mqtt_agent_task.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Common/app/mqtt/mqtt_agent_task.c b/Common/app/mqtt/mqtt_agent_task.c index 23bdc238..9ae8ec99 100644 --- a/Common/app/mqtt/mqtt_agent_task.c +++ b/Common/app/mqtt/mqtt_agent_task.c @@ -587,7 +587,7 @@ static void prvResubscribeCommandCallback( MQTTAgentCommandContext_t * pxCommand static MQTTStatus_t prvHandleResubscribe( MQTTAgentContext_t * pxMqttAgentCtx, SubMgrCtx_t * pxCtx ) { - MQTTStatus_t xStatus = MQTTSuccess; + MQTTStatus_t xStatus; configASSERT( pxCtx ); configASSERT( pxCtx->xMutex ); @@ -597,7 +597,7 @@ static MQTTStatus_t prvHandleResubscribe( MQTTAgentContext_t * pxMqttAgentCtx, pxCtx->pxCallbacks, &( pxCtx->uxSubscriptionCount ) ); - if( ( xStatus == MQTTSuccess ) && ( pxCtx->uxSubscriptionCount > 0U ) ) + if( pxCtx->uxSubscriptionCount > 0U ) { MQTTAgentCommandInfo_t xCommandParams = { @@ -1154,11 +1154,25 @@ void vMQTTAgentTask( void * pvParameters ) xMQTTStatus = MQTTAgent_ResumeSession( &( pxCtx->xAgentContext ), xSessionPresent ); /* Re-subscribe to all the previously subscribed topics if there is no existing session. */ - if( ( xMQTTStatus == MQTTSuccess ) && - ( xSessionPresent == false ) ) + if( xMQTTStatus == MQTTSuccess ) { - xMQTTStatus = prvHandleResubscribe( &( pxCtx->xAgentContext ), - &( pxCtx->xSubMgrCtx ) ); + if( xSessionPresent == false ) + { + xMQTTStatus = prvHandleResubscribe( &( pxCtx->xAgentContext ), + &( pxCtx->xSubMgrCtx ) ); + } + else + { + /* No need to resubscribe as a session is present and the broker is aware of + * the previous subscriptions. */ + if( MUTEX_IS_OWNED( pxCtx->xSubMgrCtx.xMutex ) ) + { + /* Give the mutex as there is no subscribe command pending. */ + ( void ) xUnlockSubCtx( &( pxCtx->xSubMgrCtx ) ); + } + + LogInfo( "Session already present (no re-subscription)." ); + } } } else if( xMQTTStatus == MQTTSuccess )