adafruit / Adafruit_CircuitPython_MiniMQTT

MQTT Client Library for CircuitPython
Other
72 stars 50 forks source link

client.loop if the MQTT Broker goes down the loop crashes with OSError: [Errno 104] ECONNRESET #194

Open SRMAX opened 6 months ago

SRMAX commented 6 months ago

In the mqtt_client.loop if the MQTT Broker goes down the loop crashes with OSError: [Errno 104] ECONNRESET. This leaves no way to handle the error in the program. It should return with regular error handling so the program can either wait for the broker to come back online or exit gracefully.

Running: Adafruit CircuitPython 8.2.9 on 2023-12-06; Adafruit QT Py ESP32-S3 no psram with ESP32S3 Library : adafruit-circuitpython-bundle-8.x-mpy-20231215

Error: Published to test/qtpy32/status/publish with PID 1 OK Message sent to MQTT Server!

Traceback (most recent call last): File "code.py", line 201, in File "adafruit_minimqtt/adafruit_minimqtt.py", line 788, in publish OSError: [Errno 104] ECONNRESET

See attached txt file for code.py code.txt

vladak commented 5 months ago

I assume that line 788 that leads to the exception is one of the self._sock.send() calls inside publish(). The error can be handled like this:

try:
    mqtt_client.publish(...)
except OSError as e:
    mqtt_client.reconnect()
    mqtt_client.publish(...)

What do you propose ? Wrap the OSError inside MMQTTException or something else ?

SRMAX commented 5 months ago

I did have a MQTTException that wasn't handled that I believe should have been. If it had returned and exception I could have handled it. Because it wasn't the result is very unstable code. If I'm overlooking something please tell me.

vladak commented 5 months ago

I don't follow. The original comment says OSError which is exception however distinct from MMQTTException.