-
Notifications
You must be signed in to change notification settings - Fork 722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
on_disconnect Callback Triggered Twice When Connection is Lost #870
Comments
These are the two routes on_disconnect is being called from:
The first call is on this line. the second one is 14 lines below: paho.mqtt.python/src/paho/mqtt/client.py Line 2149 in d45de37
I'm not sure what the best fix is. Adjusted test script:
|
Thank you for your response! In my development environment (currently on Windows), I indeed tested by manually disconnecting WiFi. Without other options, this seemed like the simplest approach—though I suppose I could have unplugged the LAN cable from the router as well. In the final production environment, this code will run on Raspberry Pi devices with Debian OS, where connection issues are likely to happen frequently due to weak cellular signals. If I understand correctly, in the current implementation, the on_disconnect callback is triggered twice when a disconnection occurs. (I hope I’m interpreting this correctly.) My questions are: 1. Could the duplicate on_disconnect calls cause issues?
2. Is there a more official or reliable way to detect disconnections? Since disconnections will likely happen often in the production environment, it’s important for me to have a clear way to detect this state and halt unnecessary publish calls. Any further guidance would be greatly appreciated! |
I don't know for certain they won't cause issues, and I can't speak officially on behalf of anyone. But if this is a concern for you or anyone else, the problem can be solved far more easily and quickly than by waiting for a fix in paho.mqtt, with just a couple of extra lines of Python: make the client code in on_disconnect idempotent. |
When testing the example code client_session_present.py
(from Paho MQTT Python GitHub Repository),
I encountered an issue where the on_disconnect callback is triggered twice when the network connection is lost. This behavior seems unexpected, as it could cause redundancy and potentially unwanted behavior in production applications.
I modified the example minimally to include on_disconnect and on_log callbacks to monitor connection status.
When I simulate a network disconnection, the following output is generated:
As shown above, Disconnected is printed twice, indicating that the on_disconnect callback is called twice.
Environment:
Paho MQTT Python Client Version: 2.1.0
Python Version: 3.10
Request:
Could you please investigate why on_disconnect is being triggered twice? Any insights or fixes would be greatly appreciated.
The text was updated successfully, but these errors were encountered: