eclipse / paho.mqtt.python

paho.mqtt.python
Other
2.13k stars 723 forks source link

Paho MQTT Client Fails to Reconnect After Prolonged Network Downtime #785

Open surfskidude opened 6 months ago

surfskidude commented 6 months ago

The Paho MQTT client fails to automatically reconnect after the network has been down for a prolonged period. While client.loop_start() should handle reconnections, it isn't effective in cases of long-term network disconnections.

Changing client.loop_start() to client.loop_forever(retry_first_connection=True) resolves the issue, suggesting that the client's default reconnection mechanism might not handle prolonged network outages effectively.

MattBrittan commented 6 months ago

loop_start effectively just calls loop_forever(retry_first_connection=True) in a thread. Are you able to provide any additional information? (what is a "prolonged period", is anything logged etc).

surfskidude commented 6 months ago

When I changed the code from the commented out to the current, things started working, and it reconnected on prolonged periods:

client.connect(MQTT_BROKER, MQTT_PORT, 60)
#client.loop_start()
client.loop_forever(retry_first_connection=True)

#try:
#    while True:
#        time.sleep(1)  # Keep the main thread alive.
#except KeyboardInterrupt:
#    print("Exiting...")
#finally:
#    client.loop_stop()
#    client.disconnect()

I am running this on a Raspberry Pi and have an issue with the network. Here is the log from dmesg:

[Wed Dec 27 08:56:15 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Wed Dec 27 08:56:19 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[Wed Dec 27 08:56:30 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Wed Dec 27 08:56:33 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[Wed Dec 27 10:27:22 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Wed Dec 27 21:06:33 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[Wed Dec 27 21:56:51 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Thu Dec 28 06:51:45 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[Thu Dec 28 12:28:42 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Thu Dec 28 13:19:19 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[Thu Dec 28 16:37:49 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Thu Dec 28 16:59:21 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx
[Thu Dec 28 21:51:38 2023] bcmgenet fd580000.ethernet eth0: Link is Down
[Fri Dec 29 06:30:03 2023] bcmgenet fd580000.ethernet eth0: Link is Up - 1Gbps/Full - flow control rx/tx

As you can see from the above log, the network is down for up to 9 hours.