Skip to content

Resolve race condition for UNSUBACK #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,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(
Copy link
Contributor

@vladak vladak Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might want to add a comment similar to the one in subscribe(), explain the rationale.

f"invalid message received as response to UNSUBSCRIBE: {hex(op)}"
)

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