eclipse-paho / paho.mqtt.python

paho.mqtt.python
Other
2.21k stars 722 forks source link

on_connect not called after reconnection after server restart #871

Open Zugschlus opened 2 hours ago

Zugschlus commented 2 hours ago

Hi,

I have an simple program that connects to a server (which is mosquitto running in docker). I have added some error handling so that the client doesn't abort when the server dies or restarts. My program subscribes to a topic in the on_connect method. When the server restarts, the client reconnects to the server, but on_connect is not called.

Reproduction

Start the attached program with --console-loglevel=debug. Let it sit for a few seconds:

# pvenv/bin/python3 mqttkeks --console-loglevel=debug
create mqtt.Client
mqtt.Client created
mqtt.start(
(Re)connected successfully to MQTT
restarted MQTT loop
)
Connected to MQTT broker rc=ReasonCode(Connack, 'Success')
Subscribed to self.subscribe_topic='tele/rain/raw_json'

then restart the server (on my machine: docker compose restart), keeping the reproducer running

Disconnected from MQTT broker with result code Unspecified error
Attempting to reconnect to MQTT broker...
Failed to connect to MQTT broker: (Attempt 1): [Errno 111] Connection refused
Retrying connection in 5 seconds...
(Re)connected successfully to MQTT
restarted MQTT loop

notice that the "Connected to MQTT broker" and the "Subscribed to self.subscribe_topic" is not present.

I don't claim that I am doing things right. But the docs are silent on error handling.

Sorry that my example needs your own MQTT broker, but you can't restart a public broker on request.

Environment

Thanks for your consideration, and for providing paho-mqtt in the first place.

Greetings, Marc

Zugschlus commented 2 hours ago

paho-mqtt-871.zip

$ unzip -v paho-mqtt-871.zip Archive:  paho-mqtt-871.zip
Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
4484  Defl:N     1282  71% 2024-11-16 07:51 607070ad  paho-mqtt-871-reproducer
195  Defl:N      131  33% 2024-11-16 07:51 5b400824  mosquitto.conf
448  Defl:N      239  47% 2024-11-16 07:51 b0b08d1f  docker-compose.yml
--------          -------  ---                            -------
5127             1652  68%                            3 files

Sorry, github didn't allow me to upload those files directly ("unsupported file type")