Skip to content

Commit

Permalink
Fix wait_for_publish that could hang for QoS=0 message
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreF committed Jan 7, 2024
1 parent b1a7347 commit 4223c51
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ v2.0.0 - 2023-xx-xx
Minimum tested version is Python 3.7
- Add on_pre_connect() callback, which is called immediately before a
connection attempt is made.
- Fix wait_for_publish that could hang with QoS == 0 message on reconnection
or publish during connection. Closes #549.


v1.6.1 - 2021-10-21
Expand Down
12 changes: 12 additions & 0 deletions src/paho/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,10 @@ def timed_out():
while not self._published and not timed_out():
self._condition.wait(timeout_tenth)

if self.rc > 0:
raise RuntimeError(f'Message publish failed: {error_string(self.rc)}')


def is_published(self):
"""Returns True if the message associated with this object has been
published, else returns False."""
Expand Down Expand Up @@ -1018,8 +1022,16 @@ def reconnect(self):
"to_process": 0,
"pos": 0}

# Before dropping all out_packet, ensure any QoS == 0 message info get
# marked as MQTT_ERR_CONN_LOST or the wait_for_publish() could hang forever
old_queue = self._out_packet
self._out_packet = collections.deque()

for pkt in old_queue:
if pkt["command"] & 0xF0 == PUBLISH and pkt["qos"] == 0:
pkt["info"].rc = MQTT_ERR_CONN_LOST
pkt["info"]._set_as_published()

with self._msgtime_mutex:
self._last_msg_in = time_func()
self._last_msg_out = time_func()
Expand Down

0 comments on commit 4223c51

Please sign in to comment.