Description
I've been seeing intermittent failures with MQTT, where function calls can hang and never return.
I've been working around it with a watchdog timer to detect the condition and hard reset the device, which is pretty ugly.
I think various things can cause it, but seems to be reproducable with subscribe or unsubscribe
If I run the code below, it typically loops for 10-30s before getting stuck. Sometimes longer.
Potentially another symptom of an underlying problem, see other issues e.g.
#107
#101
I have also tried this with a completely different MQTT broker / service, and had similar results. Quicker to fail if anything.
So I don't think this is down to the Adafruit IO server.
I wondered about throttling, but the time to failure seems too inconsistent
Adafruit CircuitPython 7.3.0 on 2022-05-23; Adafruit Feather ESP32S2 with ESP32S2
Minimal example
import time
import wifi, ssl, socketpool
import adafruit_minimqtt.adafruit_minimqtt as MQTT
from secrets import secrets
def connected_callback(client, userdata, flags, rc):
print("Connected to AIO")
def subscribe_callback(client, userdata, topic, granted_qos):
print(f"Subscribed to {topic}")
def unsubscribe_callback(client, userdata, topic, granted_qos):
print(f"Unsubscribed from {topic}")
def message_callback(client, topic, payload):
print(f"{topic} = {payload}")
ssid = secrets["ssid"]
password = secrets["password"]
wifi.radio.connect(ssid, password)
pool = socketpool.SocketPool(wifi.radio)
client = MQTT.MQTT(
broker="io.adafruit.com",
username=secrets["aio_username"],
password=secrets["aio_key"],
socket_pool=pool,
ssl_context=ssl.create_default_context(),
)
client.on_subscribe = subscribe_callback
client.on_unsubscribe = unsubscribe_callback
client.on_connect = connected_callback
client.on_message = message_callback
client.connect()
start = time.monotonic()
while True:
print(f'time = {round(time.monotonic() - start, 1)}s')
client.subscribe('time/hours')
time.sleep(0.1)
client.unsubscribe('time/hours')
time.sleep(0.1)