Skip to content

Commit

Permalink
Resolve race condition for UNSUBACK
Browse files Browse the repository at this point in the history
Corrects the behavior of erroring out while waiting for an UNSUBACK when a publish message from the server arrives before the UNSUBACK does. Also changed op comparisons from using magic numbers to named constants for clarity.
  • Loading branch information
ch4nsuk3 committed Oct 9, 2024
1 parent 25a9218 commit 920e64f
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 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 @@ -774,7 +776,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 @@ -852,7 +854,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 @@ -862,10 +864,10 @@ 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:
raise MMQTTException(
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
)

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

0 comments on commit 920e64f

Please sign in to comment.