sbtinstruments / aiomqtt

The idiomatic asyncio MQTT client, wrapped around paho-mqtt
https://sbtinstruments.github.io/aiomqtt
BSD 3-Clause "New" or "Revised" License
392 stars 71 forks source link

Exceptions on __aenter__ not handled. #284

Closed kuriakosejohn closed 3 months ago

kuriakosejohn commented 3 months ago

We are using aiomqtt to connect to AWS IoT Core with client certificate and just in time registration, this means first connection to broker will fail with an Operation Timed out exception from line client.py: __aenter__().await self._wait_for(self._connected, timeout=None). We have the following retry logic, but we continuously get "MqttReentrantError" exception for following iterations since self._lock is not released and self._connected future is not re-initialized for the second call.

while True:
    try:
       async with client:
          ...

The following change in client.py: __aenter__() solved the problem.

-        await self._wait_for(self._connected, timeout=None)
+        try:
+            await self._wait_for(self._connected, timeout=None)
+        except Exception as e:
+            self._lock.release()
+            self._connected = asyncio.Future()
+            raise e
Gunsmithy commented 3 months ago

Following since I was debugging this with John. Feel free to reach out to either of us if we can help with a reproduction!

empicano commented 3 months ago

Thanks both of you for the issue and the solution, good catch! 😊