eclipse-paho / eclipse-paho/paho.mqtt.python
Subscription could be lost if loop is started before connecting to server. (inconsistiency in docs)
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
# Prerequisites
*Note: You may remove this section prior to submitting your report.*
A small team of volunteers monitors issues. Please help us to help you by making it simple to understand and, if possible,
replicate your issue. Prior to reporting a bug please:
- [x] Test the latest release of the library.
- [x] Search existing issues.
- [x] Read the relevant documentation.
- [x] Review your server configuration and logs.
- [ ] Consider testing against a different server (e.g. [mqtt.eclipseprojects.io](https://mqtt.eclipseprojects.io/) or [test.mosquitto.org](https://test.mosquitto.org/))
- [x] If possible, test using another tool (e.g. [MQTTX](https://mqttx.app/) / [mosquitto_sub](https://mosquitto.org/man/mosquitto_sub-1.html))
to confirm the issue is specific to this client.
- [ ] If you are unsure if you have found a bug, please consider asking on [stackoverflow](https://stackoverflow.com/) for a quicker response.
# Bug Description
According do [docs](https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html#paho.mqtt.client.Client.connect):
It is acceptable to firstly launch event loop before connecting.
> Connect to a remote broker. This is a blocking call that establishes the underlying connection and transmits a CONNECT packet. Note that the connection status will not be updated until a CONNACK is received and processed (**this requires a running network loop**, see [loop_start](https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html#paho.mqtt.client.Client.loop_start), [loop_forever](https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html#paho.mqtt.client.Client.loop_forever), [loop](https://eclipse.dev/paho/files/paho.mqtt.python/html/client.html#paho.mqtt.client.Client.loop)…).
But according to examples (like [client_sub](https://github.com/eclipse/paho.mqtt.python/blob/master/examples/client_sub.py) ) the order is opposite.
```py
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
# mqttc.on_log = on_log
mqttc.connect("mqtt.eclipseprojects.io", 1883, 60)
mqttc.subscribe("$SYS/#")
```
When I tried to start loop before connecting I had problems with connection and with subscription.
# Reproduction
1. Run simple MQTT broker
```
docker run -d --name emqx -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx:5.0.20
```
2. Run code
```py
import time
import paho.mqtt.client as mqtt
def on_connect(mqttc, obj, flags, reason_code, properties):
print("reason_code: " + str(reason_code))
def on_message(mqttc, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_subscribe(mqttc, obj, mid, reason_code_list, properties):
print("Subscribed: " + str(mid) + " " + str(reason_code_list))
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
mqttc.loop_start() ### LOOP STARTED BEFORE CONNECTION
mqttc.connect("localhost", 1883, 60)
print("Is connected: ", mqttc.is_connected())
mqttc.subscribe("/#")
while True:
time.sleep(1)
```
3. Send something to on thread `/logs` using tool like MQTTX
4. Subscription does not work (messages are not printed)
```
$ python3 ./example.py
Is connected: False
reason_code: Success
```
Replacing order in this code
```py
mqttc.connect("localhost", 1883, 60)
mqttc.loop_start() ### LOOP STARTED AFTER CONNECTION
```
Fixes the issue:
```
$ python3 ./example.py
Is connected: False
reason_code: Success
Subscribed: 1 [ReasonCode(Suback, 'Granted QoS 0')]
/logs 0 b'{\n "msg": "xxxxxxxxx"\n}'
```
# Environment
* Python version: 3.10
* Library version: 2.0
* Operating system (including version): Ubuntu 22.04
* MQTT server (name, version, configuration, hosting details): (check repro)
# Logs
Contributor guide
Assessment
This issue has not been assessed yet.