reconnect Exception in threaded loop
I think, paho mqtt client should stay trying to reconnect on error. But in case the mqtt Server is shutdown, the threaded loop stops after throwing a unhandled exception. I did even not manage to use reconnect() on on_disconnect callback.
Simple test case:
$ cat testcase.py
#!/usr/bin/env python2
import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import time
import json
clientname = "pi4"
hostname = '192.168.178.100'
port = 1883
timeout = 60
client = mqtt.Client(client_id = clientname,clean_session = False)
client.connect_async(hostname, port, timeout)
client.loop_start()
while True:
topic = "testcase"
data = { 'test': 1 }
client.publish(topic, json.dumps(data))
time.sleep(1)
Runnning testcase.py. The Exception dumps, when i shut down the mqtt server:
$ ./testcase.py
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/pi/ds18temp/local/lib/python2.7/site-packages/paho/mqtt/client.py", line 3591, in _thread_main
self.loop_forever(retry_first_connection=True)
File "/home/pi/ds18temp/local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
self.reconnect()
File "/home/pi/ds18temp/local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1044, in reconnect
sock = self._create_socket_connection()
File "/home/pi/ds18temp/local/lib/python2.7/site-packages/paho/mqtt/client.py", line 3685, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
File "/usr/lib/python2.7/socket.py", line 575, in create_connection
raise err
error: [Errno 111] Connection refused
I also encounter this problem. Can it be fixed? Or is there a workaround?
This problem doesn't occur on versions before 1.6.0
The next release is dropping support for Python 2.7; as such it's unlikely that this issue will be addressed. My guess would be that this issue is the result of this commit.
Given that Python 2.7 was sunset a few years back I think that this issue should probably be closed (unless anyone has experienced it with a later release). However, for now, I'm going to tag it as "More Info Needed"; the extra info being replication on v3.7+ or volunteers to maintain Python 2 compatibility. If there is no extra info in a month we will look to close it.