mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

mosquitto_loop_stop(force=0) hangs and the mosquitto thread blocks in connect() call forever

Open gopicisco opened this issue 4 years ago • 2 comments

Mosquitto version: 1.6.15 Tested with latest mosquitto client library: 2.0.12 Platform: Linux

We are using the mosquitto client library with the mosquitto_loop_start/stop mechanism for our implementation. When mosquitto_connect_async is called with the "unreachable broker address" and keepalive=60 secs, The connect fails after 60 secs and disconnect callback is invoked with rc=MOSQ_ERR_KEEPALIVE (19) After some time, when mosquitto_disconnect is called, it fails with MOSQ_ERR_NO_CONN (in v1.6.15) and MOSQ_ERR_SUCCESS(in v2.0.12). The success error code in the latest version, I understand may be due to the bug fix (6ebbb4d)

After this, the mosquitto_loop_stop(force=0) call hangs forever. The backtrace of two threads (application thread which invoked the loop_stop and the mosquitto thread)

mosquitto thread #0 0x00007f109e27253b in __libc_connect (fd=13, addr=..., len=16) at ../sysdeps/unix/sysv/linux/connect.c:26 #1 0x00007f10a270ec60 in net.try_connect () from /home/gogopals/deps-edge-components/lib/libmosquitto.so.1 #2 0x00007f10a270ed97 in net.socket_connect () from /home/gogopals/deps-edge-components/lib/libmosquitto.so.1 #3 0x00007f10a27098bc in mosquitto_reconnect () from /home/gogopals/deps-edge-components/lib/libmosquitto.so.1 --Type for more, q to quit, c to continue without paging-- #4 0x00007f10a270baf8 in mosquitto_loop_forever () from /home/gogopals/deps-edge-components/lib/libmosquitto.so.1 #5 0x00007f10a2713af3 in mosquitto.thread_main () from /home/gogopals/deps-edge-components/lib/libmosquitto.so.1 #6 0x00007f109e267609 in start_thread (arg=) at pthread_create.c:477 #7 0x00007f109dc2c293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Application thread #0 __pthread_clockjoin_ex (threadid=139708726155008, thread_return=0x0, clockid=, abstime=, block=) at pthread_join_common.c:145 #1 0x00007f10a2713be2 in mosquitto_loop_stop () from /home/gogopals/deps-edge-components/lib/libmosquitto.so.1 #2 0x0000559596064ad7 in MosquittoSDK::mosquitto_loop_stop(mosquitto*, bool) const ()

From the above backtrace, I understand the mosquitto thread hangs in socket connect() call forever. I added some logs before and after mosquitto_reconnect in loop.c:mosquitto_loop_forever log__printf(mosq, MOSQ_LOG_DEBUG, "Before reconnect. state=%d", state); rc = mosquitto_reconnect(mosq); log__printf(mosq, MOSQ_LOG_DEBUG, "After reconnect. state=%d", state);

The After reconnect log print is never seen. When the mosquitto_reconnect (blocking) is called the first time, then after that it never unblocks. Unfortunately, we are facing some issues with loop_end(force=1) which uses pthread_cancel. Appreciate your help on how to unblock the mosquitto thread from the connect() call gracefully using any timeout or disconnect option?

gopicisco avatar Oct 01 '21 04:10 gopicisco

Sorry to hear you're having problems. Is there any chance you could provide me with a minimal compilable example that shows how you are using the mosquitto library? I could spend time trying to come up with an example based on your description, but there's a good chance I'd be doing something differently so it would be wasting time rather than trying to find the actual problem.

As an example, I was able to help out with https://github.com/eclipse/mosquitto/issues/2335 quite quickly because of the full code example.

ralight avatar Oct 06 '21 10:10 ralight

@ralight hello,I encountered the same problem(mosquito lib version 2.0.22), and I think the problem lies here

ImageThe current code returns 0

Image But 0 won't stop the loop, but mosquitto_loop_stop is waiting thread exits. Since this thread has never ended, the mosquitto_loop_stop interface is blocked

APandaes avatar Nov 17 '25 02:11 APandaes