Skip to content

Commit

Permalink
Fix is_connected property when not using loop_forever
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreF committed Jan 20, 2024
1 parent 2c73adb commit cead6af
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ v2.0.0 - 2023-xx-xx
- Fix loading too weak TLS CA file but setting allowed ciphers before loading CA. Closes #676.
- Allow to manually ack QoS > 0 messages. Closes #753 & #348.
- Improve tests & linters. Modernize build (drop setup.py, use pyproject.toml)
- Fix is_connected property to correctly return False when connection is lost
and loop_start/loop_forever isn't used. Closes #525.



v1.6.1 - 2021-10-21
===================
Expand Down
15 changes: 15 additions & 0 deletions src/paho/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1290,10 +1290,19 @@ def _loop(self, timeout: float = 1.0) -> MQTTErrorCode:
socklist = select.select(rlist, wlist, [], timeout)
except TypeError:
# Socket isn't correct type, in likelihood connection is lost
# ... or we called disconnect(). In that case the socket will
# be closed but some loop (like loop_forever) will continue to
# call _loop(). We still want to break that loop by returning an
# rc != MQTT_ERR_SUCCESS and we don't want state to change from
# mqtt_cs_disconnecting.
if self._state != ConnectionState.MQTT_CS_DISCONNECTING:
self._state = ConnectionState.MQTT_CS_CONNECTION_LOST
return MQTTErrorCode.MQTT_ERR_CONN_LOST
except ValueError:
# Can occur if we just reconnected but rlist/wlist contain a -1 for
# some reason.
if self._state != ConnectionState.MQTT_CS_DISCONNECTING:
self._state = ConnectionState.MQTT_CS_CONNECTION_LOST
return MQTTErrorCode.MQTT_ERR_CONN_LOST
except Exception:
# Note that KeyboardInterrupt, etc. can still terminate since they
Expand Down Expand Up @@ -1768,6 +1777,8 @@ def loop_misc(self) -> MQTTErrorCode:
if self._state == mqtt_cs_disconnecting:
rc = MQTTErrorCode.MQTT_ERR_SUCCESS
else:
self._state = ConnectionState.MQTT_CS_CONNECTION_LOST
self._easy_log(MQTT_LOG_DEBUG, "... 2")
rc = MQTTErrorCode.MQTT_ERR_KEEPALIVE

self._do_on_disconnect(rc)
Expand Down Expand Up @@ -2580,6 +2591,10 @@ def _loop_rc_handle(

self._do_on_disconnect(rc, properties)

if rc == MQTT_ERR_CONN_LOST:
self._easy_log(MQTT_LOG_DEBUG, "... 3")
self._state = ConnectionState.MQTT_CS_CONNECTION_LOST

return rc

def _packet_read(self) -> MQTTErrorCode:
Expand Down
1 change: 1 addition & 0 deletions src/paho/mqtt/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ConnectionState(enum.IntEnum):
MQTT_CS_CONNECTED = 1
MQTT_CS_DISCONNECTING = 2
MQTT_CS_CONNECT_ASYNC = 3
MQTT_CS_CONNECTION_LOST = 4


class MessageState(enum.IntEnum):
Expand Down

0 comments on commit cead6af

Please sign in to comment.