Open
Description
Bug Description
Setting the connect_timeout
member to x
does not make connect
fail after x
seconds with some faulty servers
Reproduction
mqttClient = mqtt.Client(
callback_api_version=CallbackAPIVersion.VERSION2,
transport = 'websockets',
protocol = 5,
)
mqttClient.connect_timeout = 1.0
mqttClient.tls_set()
mqttClient.connect(
host = ...,
port = 8084,
keepalive = 10,
)
With the host as google.com
, the program stops after 1s with the following logs:
Traceback (most recent call last):
File "./myTest2.py", line 15, in <module>
mqttClient.connect(
~~~~~~~~~~~~~~~~~~^
...<2 lines>...
keepalive = 10,
^^^^^^^^^^^^^^^
)
^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 1435, in connect
return self.reconnect()
~~~~~~~~~~~~~~^^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 1598, in reconnect
self._sock = self._create_socket()
~~~~~~~~~~~~~~~~~~~^^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 4609, in _create_socket
sock = self._create_socket_connection()
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 4640, in _create_socket_connection
return socket.create_connection(addr, timeout=self._connect_timeout, source_address=source)
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/homebrew/Cellar/python@3.13/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/socket.py", line 864, in create_connection
raise exceptions[0]
File "/opt/homebrew/Cellar/python@3.13/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/socket.py", line 849, in create_connection
sock.connect(sa)
~~~~~~~~~~~~^^^^
TimeoutError: timed out
With the host as the faulty server described below, we get a 10s (keepalive) timeout with the following logs:
Traceback (most recent call last):
File "./myTest2.py", line 15, in <module>
mqttClient.connect(
~~~~~~~~~~~~~~~~~~^
...<2 lines>...
keepalive = 10,
^^^^^^^^^^^^^^^
)
^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 1435, in connect
return self.reconnect()
~~~~~~~~~~~~~~^^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 1598, in reconnect
self._sock = self._create_socket()
~~~~~~~~~~~~~~~~~~~^^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 4616, in _create_socket
return _WebsocketWrapper(
socket=sock,
...<4 lines>...
extra_headers=self._websocket_extra_headers,
)
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 4713, in __init__
self._do_handshake(extra_headers)
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
File "./venv/lib/python3.13/site-packages/paho/mqtt/client.py", line 4767, in _do_handshake
byte = self._socket.recv(1)
File "/opt/homebrew/Cellar/python@3.13/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line 1285, in recv
return self.read(buflen)
~~~~~~~~~^^^^^^^^
File "/opt/homebrew/Cellar/python@3.13/3.13.3/Frameworks/Python.framework/Versions/3.13/lib/python3.13/ssl.py", line 1140, in read
return self._sslobj.read(len)
~~~~~~~~~~~~~~~~~^^^^^
TimeoutError: The read operation timed out
Environment
- Python version: 3.13.3
- Library version: 2.1.0
- Operating system (including version): macOS Sonoma 14.6
- MQTT server (name, version, configuration, hosting details):
To simulate the failure, we host a EMQX server with an empty emqx.conf
, this makes it crash in a loop
Possibly related to #876