Skip to content

Commit 920e64f

Browse files
committed
Resolve race condition for UNSUBACK
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.
1 parent 25a9218 commit 920e64f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@
6666
MQTT_PINGRESP = const(0xD0)
6767
MQTT_PUBLISH = const(0x30)
6868
MQTT_SUB = const(0x82)
69+
MQTT_SUBACK = const(0x90)
6970
MQTT_UNSUB = const(0xA2)
71+
MQTT_UNSUBACK = const(0xB0)
7072
MQTT_DISCONNECT = b"\xe0\0"
7173

7274
MQTT_PKT_TYPE_MASK = const(0xF0)
@@ -774,7 +776,7 @@ def subscribe( # noqa: PLR0912, PLR0915, Too many branches, Too many statements
774776
f"No data received from broker for {self._recv_timeout} seconds."
775777
)
776778
else:
777-
if op == 0x90:
779+
if op == MQTT_SUBACK:
778780
remaining_len = self._decode_remaining_length()
779781
assert remaining_len > 0
780782
rc = self._sock_exact_recv(2)
@@ -852,7 +854,7 @@ def unsubscribe( # noqa: PLR0912, Too many branches
852854
f"No data received from broker for {self._recv_timeout} seconds."
853855
)
854856
else:
855-
if op == 176:
857+
if op == MQTT_UNSUBACK:
856858
rc = self._sock_exact_recv(3)
857859
assert rc[0] == 0x02
858860
# [MQTT-3.32]
@@ -862,10 +864,10 @@ def unsubscribe( # noqa: PLR0912, Too many branches
862864
self.on_unsubscribe(self, self.user_data, t, self._pid)
863865
self._subscribed_topics.remove(t)
864866
return
865-
866-
raise MMQTTException(
867-
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
868-
)
867+
if op != MQTT_PUBLISH:
868+
raise MMQTTException(
869+
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
870+
)
869871

870872
def _recompute_reconnect_backoff(self) -> None:
871873
"""

0 commit comments

Comments
 (0)