Skip to content

Commit

Permalink
Merge pull request #225 from ch4nsuk3/unsuback-race-fix
Browse files Browse the repository at this point in the history
Resolve race condition for UNSUBACK
  • Loading branch information
dhalbert authored Jan 18, 2025
2 parents c9ac0f8 + 64638cf commit c66e831
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@
MQTT_PINGRESP = const(0xD0)
MQTT_PUBLISH = const(0x30)
MQTT_SUB = const(0x82)
MQTT_SUBACK = const(0x90)
MQTT_UNSUB = const(0xA2)
MQTT_UNSUBACK = const(0xB0)
MQTT_DISCONNECT = b"\xe0\0"

MQTT_PKT_TYPE_MASK = const(0xF0)
Expand Down Expand Up @@ -801,7 +803,7 @@ def subscribe( # noqa: PLR0912, PLR0915, Too many branches, Too many statements
f"No data received from broker for {self._recv_timeout} seconds."
)
else:
if op == 0x90:
if op == MQTT_SUBACK:
remaining_len = self._decode_remaining_length()
assert remaining_len > 0
rc = self._sock_exact_recv(2)
Expand Down Expand Up @@ -879,7 +881,7 @@ def unsubscribe( # noqa: PLR0912, Too many branches
f"No data received from broker for {self._recv_timeout} seconds."
)
else:
if op == 176:
if op == MQTT_UNSUBACK:
rc = self._sock_exact_recv(3)
assert rc[0] == 0x02
# [MQTT-3.32]
Expand All @@ -889,10 +891,12 @@ def unsubscribe( # noqa: PLR0912, Too many branches
self.on_unsubscribe(self, self.user_data, t, self._pid)
self._subscribed_topics.remove(t)
return

raise MMQTTException(
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
)
if op != MQTT_PUBLISH:
# [3.10.4] The Server may continue to deliver existing messages buffered
# for delivery to the client prior to sending the UNSUBACK Packet.
raise MMQTTException(
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
)

def _recompute_reconnect_backoff(self) -> None:
"""
Expand Down

0 comments on commit c66e831

Please sign in to comment.