# Disconnecting the broker leads to an error

**URL:** <https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736>\
**Category:** Libraries\
**Created:** [October 29, 2024, 3:26pm UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736 "2024-10-29T15:26:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pistoletov1974](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pistoletov1974/32/4554_2.png) [@pistoletov1974](https://forums.freertos.org/u/pistoletov1974)\
**Post date:** [October 29, 2024, 3:26pm UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736/1 "2024-10-29T15:26:16Z")

</div>

```
The situation arises when there was a connection to the broker and then the broker disconnected. After about 15 minutes, program stops in the assert function. Tell me the reason for the error. Stopping the broker should not lead to a shutdown. I attach parts of the log and the corresponding code

```

```auto
> [11:42:56:532] < [ERROR] [MQTT] [Agent_GetCommand:118] No command structure available.
> [11:42:56:532] < assert prvVendSubscribePublishTask 287

```

```auto
      ulNotification = ~ulValueToNotify;
        telemetry_published = pdFALSE; 
        xCommandAdded = MQTTAgent_Publish( &xGlobalMqttAgentContext,
                                           &xPublishInfo,
                                           &xCommandParams );
287 configASSERT( xCommandAdded == MQTTSuccess );

```

```auto
    structRetrieved = Agent_MessageReceive( &commandStructMessageCtx, &( structToUse ), blockTimeMs );
    
    if( !structRetrieved )
    {
118 LogError( ( "No command structure available." ) );

```

---

<div class="post-metadata">

**Author:** ![jfx](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jfx](https://forums.freertos.org/u/jfx)\
**Post date:** [October 29, 2024, 3:47pm UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736/2 "2024-10-29T15:47:01Z")

</div>

What’s the size of your command queue and pool?

Are you still running `MQTTAgent_CommandLoop` even whilst disconnected? It returns an error on connection loss, so you need to make sure you’re re-running it even whilst not connected if you want to keep pushing commands to it.

If the agent isn’t consuming commands, the queue is going to fill up and depending on the size of the queue and pool, you’re may run out of commands too (which is what “No command structure available.” means).

---

<div class="post-metadata">

**Author:** ![pistoletov1974](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pistoletov1974/32/4554_2.png) [@pistoletov1974](https://forums.freertos.org/u/pistoletov1974)\
**Post date:** [October 29, 2024, 4:03pm UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736/3 "2024-10-29T16:03:32Z")

</div>

> [@jfx](#):
>
> Are you still running `MQTTAgent_CommandLoop` even whilst disconnected?

Thank you for reply.  
The size is - #define MQTT\_COMMAND\_CONTEXTS\_POOL\_SIZE ( 10U )  
I used the MQTTagent example code as a basis. As I understand it, there is a problem with determining the state. As I wrote[FORUM POST](https://forums.freertos.org/t/global-varaiable-of-mqtt-broker-state/21569/11) earlier, the MQTT\_CheckConnectStatus(pMQTTContext ) function does not work correctly if the link to the Internet remains, but it is the broker’s server that is disconnected. The problem does not occur if you physically pull out the ethernet cable, then the cycle stops

---

<div class="post-metadata">

**Author:** ![pistoletov1974](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/pistoletov1974/32/4554_2.png) [@pistoletov1974](https://forums.freertos.org/u/pistoletov1974)\
**Post date:** [October 30, 2024, 3:13pm UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736/4 "2024-10-30T15:13:05Z")

</div>

The size of the command queue is set by this constant MQTT\_COMMAND\_CONTEXTS\_POOL\_SIZE and when it ends the function throws an error?

```auto

    structRetrieved = Agent_MessageReceive( &commandStructMessageCtx, &( structToUse ), blockTimeMs );
    
    if( !structRetrieved )
    {
        LogError( ( "No command structure available." ) );

```

---

<div class="post-metadata">

**Author:** ![ActoryOu](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/actoryou/32/2880_2.png) [@ActoryOu](https://forums.freertos.org/u/ActoryOu)\
**Post date:** [October 31, 2024, 5:47am UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736/5 "2024-10-31T05:47:28Z")

</div>

> [@pistoletov1974](#):
>
> ```auto
> ulNotification = ~ulValueToNotify;
> telemetry_published = pdFALSE; 
> xCommandAdded = MQTTAgent_Publish( &xGlobalMqttAgentContext,
> &xPublishInfo,
> &xCommandParams );
> 287 configASSERT( xCommandAdded == MQTTSuccess );
> 
> ```

From my understanding, this aspect appears to be more closely related to the application layer of the system. Given that context, I believe it would be appropriate to proceed with updating this part of code.

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [November 4, 2024, 5:56am UTC](https://forums.freertos.org/t/disconnecting-the-broker-leads-to-an-error/21736/6 "2024-11-04T05:56:00Z")

</div>

> [@jfx](#):
>
> If the agent isn’t consuming commands, the queue is going to fill up and depending on the size of the queue and pool, you’re may run out of commands too (which is what “No command structure available.” means).

@pistoletov1974 Check this also i.e. ensure that the agent is still draining commands.
