eclipse/paho.mqtt.c

Occasional Crash in Main Program Calling paho.mqtt.c Library

JohnChain opened this issue · 2 comments

Describe the bug
An occasional crash occurs in the main program using the paho.mqtt.c library. According to the stack trace, the crash happens at the MQTTProtocol_handleSubacks function. Due to the sporadic nature of the issue, it is challenging to pinpoint the exact reproduction steps.

(gdb) bt
#0  0x0000007f9ca982bc in MQTTProtocol_handleSubacks (pack=0x7f9001a5b0,
    sock=0)
    at /data/VDB1/user_home/lijunqian/dreame/athena_r2412/build/external/paho.mqtt.c/src/paho.mqtt.c/src/MQTTProtocolOut.c:413
#1  0x0000007f9ca8610c in MQTTAsync_receiveThread (n=0x7f94042f40)
    at /data/VDB1/user_home/lijunqian/dreame/athena_r2412/build/external/paho.mqtt.c/src/paho.mqtt.c/src/MQTTAsyncUtils.c:2237
#2  0x0000007f9c8faf4c in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb) p sock
$1 = 0
(gdb) p pack
$2 = (void *) 0x7f9001a5b0
(gdb) p bstate->version
$3 = 0x7f9ca9ed38 "1.3.12"
(gdb) p bstate->clients->count
$4 = 0
(gdb) 

To Reproduce
As the issue occurs sporadically, I cannot provide specific reproduction steps. The crash seems to occur within the MQTTProtocol_handleSubacks function.

Environment Information

Operating System: arm linux
Compiler Version: aarch64-openwrt-linux-gnu-gcc.bin (OpenWrt/Linaro GCC 6.4-2017.11 2017-11) 6.4.1
paho.mqtt.c Version: 1.3.12

Log files
dmiot_trace.log

The thing I can see in handleSubacks is the code it assumes the client object will be found, which normally it will be:

 client = (Clients*)(ListFindItem(bstate->clients, &sock, clientSocketCompare)->content);
 Log(LOG_PROTOCOL, 23, NULL, sock, client->clientID, suback->msgId);

This would better be:

 ListElement* result = ListFindItem(bstate->clients, &sock, clientSocketCompare);
 if (result)
 {
      client = (Clients*)(result->content);
      Log(LOG_PROTOCOL, 23, NULL, sock, client->clientID, suback->msgId);
 }

If you want you can try it out to see if it changes anything for you.

Hi icraggs, I will try it, thanks :)