eclipse / paho.mqtt.c

An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/
https://eclipse.org/paho
Other
1.96k stars 1.1k forks source link

Segmentation faults during disconnect and failed reconnect attempt #385

Closed JuergenKosel closed 6 years ago

JuergenKosel commented 6 years ago

Hello,

while continuing testing for issue #373 a segmentation fault was caused in the following line: https://github.com/eclipse/paho.mqtt.c/blob/c85e4a2fd6e96ebc35caef99a024d94c9b664ba0/src/MQTTAsync.c#L1099

Output from gdb:

Program terminated with signal 11, Segmentation fault.
#0  0x0fed5b98 in MQTTAsync_freeCommand1 (command=0x0) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1024
1024        if (command->command.type == SUBSCRIBE)
(gdb) up
#1  0x0fed5d50 in MQTTAsync_freeCommand (command=0x0) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1054
1054        MQTTAsync_freeCommand1(command);
(gdb) up
#2  0x0fed5f68 in MQTTAsync_writeComplete (socket=26) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1105
1105                MQTTAsync_freeCommand(com);
(gdb) bt
#0  0x0fed5b98 in MQTTAsync_freeCommand1 (command=0x0) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1024
#1  0x0fed5d50 in MQTTAsync_freeCommand (command=0x0) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1054
#2  0x0fed5f68 in MQTTAsync_writeComplete (socket=26) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1105
#3  0x0feed364 in Socket_continueWrites (pwset=0x54084bcc) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/Socket.c:822
#4  0x0feeba00 in Socket_getReadySocket (more_work=0, tp=0x54084c8c) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/Socket.c:264
#5  0x0fedb7dc in MQTTAsync_cycle (sock=0x54084d28, timeout=1000, rc=0x54084d24) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:2916
#6  0x0fed7cb0 in MQTTAsync_receiveThread (n=0x102bda84) at /home/klj/echo-sources-git-svn-full/paho.mqtt.c/src/MQTTAsync.c:1740
#7  0x0e842a6c in ?? ()
#8  0x0e4ec8a0 in ?? ()
icraggs commented 6 years ago

Optimally, we could unlock the socket mutex in Socket_getReadySocket() around the first select call:

if ((rc = select(s.maxfdp1, &(s.rset), &pwset, NULL, &timeout)) == SOCKET_ERROR)

as this is where the time is spent waiting for socket activity. This would entail passing a pointer to the socket mutex into the getReadySocket call.

JuergenKosel commented 6 years ago

Hello Ian,

I have done this in commit 341037269e00da935602c34c6aa4efe4d295e3bd and it looks like that this improves the performance again. (Not measured yet)

Greetings Juergen