Skip to content
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

umqtt.robust: Fix check_msg blocking after reconnect #509

Closed
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions micropython/umqtt.robust/example_check_msg_robust.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import umqtt.robust
import time

# Instantiate an MQTTClient with a keepalive time of 5 seconds (to help us test
# what happens to check_msg() with a broken connection)
m = umqtt.robust.MQTTClient(host="localhost", debug=True, keepalive=5)

m.connect()

# Wait for the broker to consider us dead
time.sleep(6)

# This should initiate a reconnect() and return immediately
m.check_msg()
2 changes: 1 addition & 1 deletion micropython/umqtt.robust/manifest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
metadata(
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.1"
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.2"
)

# Originally written by Paul Sokolovsky.
Expand Down
10 changes: 10 additions & 0 deletions micropython/umqtt.robust/umqtt/robust.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,13 @@ def wait_msg(self):
except OSError as e:
ian-llewellyn marked this conversation as resolved.
Show resolved Hide resolved
self.log(False, e)
self.reconnect()

def check_msg(self, attempts=2):
while attempts:
self.sock.setblocking(False)
try:
return super().wait_msg()
except OSError as e:
self.log(False, e)
self.reconnect()
attempts -= 1