Content-Length: 277361 | pFad | http://github.com/eclipse-paho/paho.mqtt.python/issues/870

36 on_disconnect Callback Triggered Twice When Connection is Lost · Issue #870 · eclipse-paho/paho.mqtt.python · GitHub
Skip to content

on_disconnect Callback Triggered Twice When Connection is Lost #870

Open
@sfczsolti

Description

@sfczsolti

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.

import paho.mqtt.client as mqtt

def on_connect(mqttc, obj, flags, reason_code, properties):
    print("Session present: " + str(flags.session_present))
    print("Connection result: " + str(reason_code))

def on_disconnect(mqttc, obj, flags, reason_code, properties):
    print('Disconnected')

def on_log(mqttc, obj, level, string):
    print(string)

mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="asdfj", clean_session=False)
mqttc.on_connect = on_connect
mqttc.on_disconnect = on_disconnect
# Uncomment to enable debug messages
# mqttc.on_log = on_log
mqttc.user_data_set(0)
mqttc.connect("mqtt.eclipseprojects.io", 1883, 10)

mqttc.loop_forever()

When I simulate a network disconnection, the following output is generated:

Session present: True
Connection result: Success
Disconnected
Disconnected
Session present: True
Connection result: Success

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: AvailableNo one has claimed responsibility for resolving this issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









      ApplySandwichStrip

      pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


      --- a PPN by Garber Painting Akron. With Image Size Reduction included!

      Fetched URL: http://github.com/eclipse-paho/paho.mqtt.python/issues/870

      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy