Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] Thread race between mosquitto_disconnect and mosquitto_loop_stop [EXTERNAL]

Hi,

I'm also interested in clarification on this issue. We use mosquitto_loop_start in our application as it uses an event-driven model. Occasionally, libmosquitto returns "unknown error" and the connection appears dead, however there doesn't seem to be a clear way to reconnect. I read that the mosquitto loop thread would be dead at this point, so I added code to stop the loop, destroy the context, and start again from scratch but this doesn't always work - sometimes returning "Invalid Input" (as seen below), and sometimes locking up the whole application (as evidenced by the systemd watchdog stepping in with extreme prejudice).

Would love to hear some official guidance.

Regards

Rebecca Gellman


-----Original Message-----
From: mosquitto-dev <mosquitto-dev-bounces@xxxxxxxxxxx> On Behalf Of Denis Dowling
Sent: 13 March 2023 03:14
To: mosquitto-dev@xxxxxxxxxxx
Subject: [mosquitto-dev] Thread race between mosquitto_disconnect and mosquitto_loop_stop [EXTERNAL]

WARNING:  This message came from an external source. Please exercise caution and proper judgment when opening any attachments, clicking links or responding to this message.

Hi All,

I am having some difficulty shutting down mosquitto client threads cleanly.

Attached is a small test program that does:

1) mosquitto_new()

2) mosquitto_connect()

3) mosquitto_loop_start()

4) mosquitto_disconnect()

5) sleep(1)

6) mosquitto_loop_stop()

If I run the program I get a MOSQ_ERR_INVAL return from mosquitto_loop_stop(). The mosq->threaded state is mosq_ts_none instead of the expected mosq_ts_self. This state gets changed when the
mosquitto__thread_main() exists. This function changes the state from mosq_ts_self back to mosq_ts_none on thread exit.

I am stuck on how to deal with this in my code. If I ignore the error from mosquitto_loop_stop() then there is a small amount of memory leaked as the pthread_join() call is never called and this shows up on my valgrind reports.

Even without the sleep() call the program will sometimes fail if the
mosquitto__thread_main() manages to exit before the
mosquitto_loop_stop() gets called in the main thread. I have noticed this periodic failure on my unit tests.

I think the code in mosquitto__thread_main() that sets mosq->threaded back to mosq_ts_none should be removed or perhaps it needs an extra state for mosq_ts_self_exit.

Happy for someone to point out a better solution :-)

Regards,

Denis

----------------------------------------------------------------------
This e-mail and any files transmitted with it are proprietary and intended solely for the use of the individual or entity to whom they are addressed. If you have reason to believe that you have received this e-mail in error, please notify the sender and destroy this e-mail and any attached files. Please note that any views or opinions presented in this e-mail are solely those of the author and do not necessarily represent those of the Curtiss-Wright Corporation or any of its subsidiaries. Documents attached hereto may contain technology subject to government export regulations. Recipient is solely responsible for ensuring that any re-export, transfer or disclosure of this information is in accordance with applicable government export regulations. The recipient should check this e-mail and any attachments for the presence of viruses. Curtiss-Wright Corporation and its subsidiaries accept no liability for any damage caused by any virus transmitted by this e-mail.
 
For information about how we process personal data and monitor communications, please visit  https://www.curtisswright.com/privacy-notice/default.aspx

Back to the top