Skip to content

Commit 4dc2d5e

Browse files
ian-llewellyndpgeorge
authored andcommitted
umqtt.robust: Fix check_msg blocking after reconnect.
After `reconnect()`, MQTTClient.socket is blocking by default, and check_msg() can block. This commit aims to fix that behaviour by reimplementing `check_msg()` for umqtt.robust and setting the socket to non-blocking. Fixes issue #192.
1 parent b50d346 commit 4dc2d5e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import umqtt.robust
2+
import time
3+
4+
# Instantiate an MQTTClient with a keepalive time of 5 seconds (to help us test
5+
# what happens to check_msg() with a broken connection)
6+
m = umqtt.robust.MQTTClient(host="localhost", debug=True, keepalive=5)
7+
8+
m.connect()
9+
10+
# Wait for the broker to consider us dead
11+
time.sleep(6)
12+
13+
# This should initiate a reconnect() and return immediately
14+
m.check_msg()

micropython/umqtt.robust/manifest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
metadata(
2-
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.1"
2+
description='Lightweight MQTT client for MicroPython ("robust" version).', version="1.0.2"
33
)
44

55
# Originally written by Paul Sokolovsky.

micropython/umqtt.robust/umqtt/robust.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ def wait_msg(self):
4242
except OSError as e:
4343
self.log(False, e)
4444
self.reconnect()
45+
46+
def check_msg(self, attempts=2):
47+
while attempts:
48+
self.sock.setblocking(False)
49+
try:
50+
return super().wait_msg()
51+
except OSError as e:
52+
self.log(False, e)
53+
self.reconnect()
54+
attempts -= 1

0 commit comments

Comments
 (0)