eclipse-paho / eclipse-paho/paho.mqtt.python
connect() always returns 0, even if connection fails.
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
## Summary
The `paho.mqtt.client.Client.connect()` function call always returns zero, even on failed connections.
If the intention is to use the `on_connect` and `on_disconnect`callbacks, then surely the `connect` method should not return anything? This behavior is needlessly confusing.
## Software
* **paho-mqtt version:** 1.4.0
* **python version:** 3.7.1
* **mosquitto version:** 1.4.15
## Test setup
* Locally running instance of mosquitto
* Test script `mqtt_tester.py`
Mosquitto configuration:
```
$ cat mosquitto.conf
password_file mosquitto.passwd
allow_anonymous false
$ mosquitto_passwd -b mosquitto.passwd testuser test
```
Contents of `mqtt_tester.py`:
```python3
import paho.mqtt.client as mqtt
client = mqtt.Client("mqtt_tester")
client.username_pw_set("testuser", "test")
rc = client.connect("127.0.0.1", 1883)
print("connect() return code:", rc)
client.publish("TEST", "test")
try:
client.loop_forever()
except KeyboardInterrupt:
client.disconnect()
exit(0)
```
### Method
1. Start `mosquitto -v -c mosquitto.conf`
2. In another terminal, start `mosquitto_sub -t "#" -v -u testuser -P test`
3. in a third terminal, start `python3 mqtt_tester.py`
## Test
### Case 1: correct credentials
`mosquitto -v -c mosquitto.conf` output:
```
mosquitto version 1.4.15 (build date Wed, 13 Feb 2019 00:27:01 +0000) starting
Config loaded from mosquitto.conf.
Opening ipv4 listen socket on port 1883.
Opening ipv6 listen socket on port 1883.
New connection from 127.0.0.1 on port 1883.
New client connected from 127.0.0.1 as mosqsub (c1, k60, u'testuser').
Sending CONNACK to mosqsub (0, 0)
Received SUBSCRIBE from mosqsub
# (QoS 0)
mosqsub 0 #
Sending SUBACK to mosqsub
New connection from 127.0.0.1 on port 1883.
New client connected from 127.0.0.1 as mqtt_tester (c1, k60, u'testuser').
Sending CONNACK to mqtt_tester (0, 0)
Received PUBLISH from mqtt_tester (d0, q0, r0, m0, 'TEST', ... (4 bytes))
Sending PUBLISH to mosqsub (d0, q0, r0, m0, 'TEST', ... (4 bytes))
Received DISCONNECT from mqtt_tester
Client mqtt_tester disconnected.
Socket error on client mosqsub, disconnecting.
```
`python3 mqtt_tester.py` output:
```
connect() return code: 0
```
`mosquitto_sub -t "#" -v -u testuser -P test` output:
```
TEST test
```
### Case 2: incorrect credentials
for this case, line 5 of `mqtt_tester.py` was changed from
```python
client.username_pw_set("testuser", "test")
```
to
```python
client.username_pw_set("testuser", "wrong")
```
No other changes were made.
`mosquitto -v -c mosquitto.conf` output:
```
mosquitto version 1.4.15 (build date Wed, 13 Feb 2019 00:27:01 +0000) starting
Config loaded from mosquitto.conf.
Opening ipv4 listen socket on port 1883.
Opening ipv6 listen socket on port 1883.
New connection from 127.0.0.1 on port 1883.
New client connected from 127.0.0.1 as mosqsub (c1, k60, u'testuser').
Sending CONNACK to mosqsub (0, 0)
Received SUBSCRIBE from mosqsub
# (QoS 0)
mosqsub 0 #
Sending SUBACK to mosqsub
New connection from 127.0.0.1 on port 1883.
Sending CONNACK to 127.0.0.1 (0, 5)
Socket error on client , disconnecting.
Socket error on client mosqsub, disconnecting.
```
`mosquitto_sub -t "#" -v -u testuser -P test` output:
```
```
`python3 mqtt_tester.py` output:
```
connect() return code: 0
```
Contributor guide
Assessment
This issue has not been assessed yet.