MQTT reconnect issue

It is hard to guess but could it be because we are giving up too early? Can you try increasing MQTT_RECV_POLLING_TIMEOUT_MS?

This is a truly complex issue. Increasing this parameter slightly improved stability, but it didn’t solve the problem. As I assume, the problem occurs at the transport level. and for some reason it always happens when trying to reconnect to the broker.

This error is repeatable - the broker sends a CONNACK packet, but the client does not receive it.

Log From broker:

tag: MQTT, clientid: part5_23, msg: mqtt_packet_sent, peername: xxx.xxx.139.38:61570, username: xxxxx, packet: CONNACK(Q0, R0, D0, AckFlags=1, ReasonCode=0)

[16:53:41:395] < [ERROR] [MQTT] [receiveConnack:2465] CONNACK recv failed with status = MQTTNoDataAvailable.

[16:53:22:213] < [DEBUG] [MQ_AG] [prvMQTTAgentTask:180] MQTT com loop: 10
 [16:53:22:213] < [DEBUG] [MQTT] [MQTT_Disconnect:3227] MQTT DISCONNECT packet size is 2.
 [16:53:22:213] < [INFO] [MQTT] [MQTT_Disconnect:3251] Disconnected from the broker.
 [16:53:22:224] < [ERROR] [MQTT] [MQTT_Disconnect:3258] MQTT Connection Disconnected Successfully
 [16:53:22:224] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:2
 [16:53:22:224] < [DEBUG] [MQTT] [sendBuffer:905] sendBuffer: Bytes Sent=2, Bytes Remaining=0
 [16:53:22:224] < [DEBUG] [MQTT] [MQTT_Disconnect:3274] Sent 2 bytes of DISCONNECT packet.
 [16:53:22:224] < [INFO] [MQ_AG] [prvSocketDisconnect:423] Disconnecting TCP connection.
..........



[16:53:31:351] < [DEBUG] [MQTT] [remainingLengthEncodedSize:479] Encoded size for length 46 is 1 bytes.
 [16:53:31:365] < [DEBUG] [MQTT] [MQTT_GetConnectPacketSize:1780] CONNECT packet remaining length=46 and packet size=48.
 [16:53:31:365] < [DEBUG] [MQTT] [MQTT_Connect:2787] CONNECT packet size is 48 and remaining length is 46.
 [16:53:31:365] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:12
 [16:53:31:365] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=12, Bytes Remaining=36
 [16:53:31:365] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:2
 [16:53:31:365] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=2, Bytes Remaining=34
 [16:53:31:379] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:8
 [16:53:31:379] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=8, Bytes Remaining=26
 [16:53:31:379] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:2
 [16:53:31:379] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=2, Bytes Remaining=24
 [16:53:31:379] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:7
 [16:53:31:379] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=7, Bytes Remaining=17
 [16:53:31:395] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:2
 [16:53:31:395] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=2, Bytes Remaining=15
 [16:53:31:395] < [DEBUG] [MQTT] [Plaintext_FreeRTOS_send:137] FTOS Sen state:15
 [16:53:31:395] < [DEBUG] [MQTT] [sendMessageVector:821] sendMessageVector: Bytes Sent=15, Bytes Remaining=0
[16:53:41:395] < [ERROR] [MQTT] [receiveConnack:2465] CONNACK recv failed with status = MQTTNoDataAvailable.
 [16:53:41:395] < [ERROR] [MQTT] [MQTT_Connect:2860] MQTT connection failed with status = MQTTNoDataAvailable.
 [16:53:41:395] < [INFO] [MQ_AG] [prvMQTTConnect:555] Session present: 0
 [16:53:41:395] < 
 [16:53:41:395] < [DEBUG] [MQ_AG] [prvMQTTAgentTask:213] Mqtt prvMQTTConnest state: 7
 [16:53:41:395] < assert prvMQTTAgentTask 214


There are 2 possiblities:

  1. The client does not receive it within time i.e. the client gives up before it gets the CONNACK. The client would have received it if it tried longer.
  2. The connection is broken and the client would never receive CONNACK regardless of how long it waits.

In the first case, waiting longer by increasing MQTT_RECV_POLLING_TIMEOUT_MS may help. In the second one, the only option is to close and re-establish the connection.

Thanks for the advice, in my case this option turned out to be more suitable

#define mqttCONNACK_RECV_TIMEOUT_MS ( 120000U )

This change has improved the issue, but it hasn’t completely eliminated it. Are there any negative consequences from significantly increasing this value?

The only consequence is that the broken connections would be detected later as you would now try longer before giving up.