adafruit / Adafruit_CircuitPython_MiniMQTT

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

MMQTTException vs partial reads on read timeout #201

Open vladak opened 10 months ago

vladak commented 10 months ago

The common use case of the library is as follows (adapted from https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/163#issue-1689131569):

while True:
    temperature = dps310.temperature
    mqtt_client.publish("foo", temperature)

    try:
        mqtt_client.loop()
    except (MMQTTException) as e:
        print("MQTTException: \n", e)
        time.sleep(300)
        continue

The documentation of the MMQTTException does not give any advice as to what to do if the exception is raised. Usually, the safest thing would be to reconnect() on the exception, notably because as indicated on https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT/issues/175#issuecomment-1902251442 the message data could be partially read and lost when the exception is raised which would result in garbage on the next read.

There are two choices: