eclipse-paho / eclipse-paho/paho.mqtt.python

on_connect not called after reconnection after server restart

Open
#871 2 comments 0 reactions 0 assignees View on GitHub
Status: Available
Dominant language
Python
Stars
2.4k
Forks
742
Avg merge
12d 48m
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.