-
Notifications
You must be signed in to change notification settings - Fork 50
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
Resolve race condition for UNSUBACK #225
base: main
Are you sure you want to change the base?
Conversation
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.
f"invalid message received as response to UNSUBSCRIBE: {hex(op)}" | ||
) | ||
if op != MQTT_PUBLISH: | ||
raise MMQTTException( |
There was a problem hiding this comment.
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.
Looks good and make sense as The MQTT 3.1.1 spec says in section 3.10.4 Response:
So the last bullet applies to this case. |
Added a comment referencing the MQTT specification for why the server may not immediately respond to an UNSUBACK
Adjusted formatting to resolve the ruff E501 Error.
Good call on the comment. Its been added. |
It would be nice if the Adafruit_CircuitPython_MiniMQTT/tests/test_subscribe.py Lines 93 to 137 in 62abc1a
|
I should be able to make these changes soon. |
When attempting to unsubscribe from a topic it was possible to receive the following error:
Traceback (most recent call last): File "code.py", line 79, in <module> File "/lib/adafruit_minimqtt/adafruit_minimqtt.py", line 869, in unsubscribe MMQTTException: ('invalid message received as response to UNSUBSCRIBE: 0x30', None)
This is due to the unsubscribe function assuming the next packet received will either be the UNSUBACK or an error code, rather than accounting for possible PUBLISH packets coming from the MQTT broker that may have already been in flight. Looking at some packet captures taken from the broker side its possible to get this error before the broker even receives the UNSUB request.
This fix essentially sets the unsubscribe function to match the existing behavior in the subscribe function, which ignores publish messages while waiting for the SUBACK.
Additionally I added named constant values for the SUBACK and UNSUBACK opcodes, replacing the 'magic number' values for comparing returned opcodes to increase clarity.