adafruit / adafruit/Adafruit_CircuitPython_MiniMQTT

`reconnect()` can produce partial resubscriptions

Open
#253 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
81
Forks
52
PR merge metrics
No merged PRs in 30d

Description

In `reconnect()`, subscriptions are re-established:
https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/blob/bf47a0e613235a97346ab0f7b20d727d666c2227/adafruit_minimqtt/adafruit_minimqtt.py#L970-L975

But there's a timing issue here; if the above code fails partway through re-subscribing, there's a silent failure case; witness:

```python
#
mqtt_client.subscribe("foo")
mqtt_client.subscribe("bar")

while True:
try:
mqtt_client.loop()
except MMQTTException:
while True:
mqtt_client.reconnect()
break
except MMQTTException:
# reconnect failed; wait a bit and try again
time.sleep(5)
```

In this scenario, if a reconnect happens and `subscribe("foo")` succeeds but "bar" throws an exception, then `_subscribed_topics` will merrily report `["foo"]`, and the next reconnection attempt will subscribe to "foo" and consider its job complete, returning control to the main loop without ever reattempting "bar".

(Moved from its original home in #252, with h/t to [this comment](https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/252#issuecomment-3872224698).)

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with reconnect() in adafruit_minimqtt/adafruit_minimqtt.py at the linked subscription-resubscription code, and trace how _subscribed_topics changes when subscribe("foo") succeeds but subscribe("bar") raises MMQTTException. Reproduce that sequence from the issue; done means a later reconnect retries every subscription that was not successfully restored, rather than treating the partial result as complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.