Skip to content

Commit c66e831

Browse files
authored
Merge pull request #225 from ch4nsuk3/unsuback-race-fix
Resolve race condition for UNSUBACK
2 parents c9ac0f8 + 64638cf commit c66e831

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

+10-6
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)
@@ -801,7 +803,7 @@ def subscribe( # noqa: PLR0912, PLR0915, Too many branches, Too many statements
801803
f"No data received from broker for {self._recv_timeout} seconds."
802804
)
803805
else:
804-
if op == 0x90:
806+
if op == MQTT_SUBACK:
805807
remaining_len = self._decode_remaining_length()
806808
assert remaining_len > 0
807809
rc = self._sock_exact_recv(2)
@@ -879,7 +881,7 @@ def unsubscribe( # noqa: PLR0912, Too many branches
879881
f"No data received from broker for {self._recv_timeout} seconds."
880882
)
881883
else:
882-
if op == 176:
884+
if op == MQTT_UNSUBACK:
883885
rc = self._sock_exact_recv(3)
884886
assert rc[0] == 0x02
885887
# [MQTT-3.32]
@@ -889,10 +891,12 @@ def unsubscribe( # noqa: PLR0912, Too many branches
889891
self.on_unsubscribe(self, self.user_data, t, self._pid)
890892
self._subscribed_topics.remove(t)
891893
return
892-
893-
raise MMQTTException(
894-
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
895-
)
894+
if op != MQTT_PUBLISH:
895+
# [3.10.4] The Server may continue to deliver existing messages buffered
896+
# for delivery to the client prior to sending the UNSUBACK Packet.
897+
raise MMQTTException(
898+
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
899+
)
896900

897901
def _recompute_reconnect_backoff(self) -> None:
898902
"""

0 commit comments

Comments
 (0)