paho.mqtt.python icon indicating copy to clipboard operation
paho.mqtt.python copied to clipboard

on_connect not called after reconnection after server restart

Open Zugschlus opened this issue 1 year ago • 2 comments

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

  • Python version: 3.12.7
  • Library version: 2.1.0
  • Operating system (including version): Debian GNU/Linux unstable (in a container). Reproducible on both plain Debian GNU/linux unstable (with paho-mqtt 2.0.0) and on Debian GNU/Linux stable (with paho-mqtt 1.6)
  • MQTT server (name, version, configuration, hosting details): Mosquitto 2.0.8 in docker, compose file and mosquitto.conf attached.

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

Greetings, Marc

Zugschlus avatar Nov 16 '24 06:11 Zugschlus

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")

Zugschlus avatar Nov 16 '24 06:11 Zugschlus

Please post the Python code enclosed in triple back ticks.

How're we supposed to know we're not downloading malware in paho-mqtt-871-reproducer ?.

JamesParrott avatar Nov 16 '24 17:11 JamesParrott