Skip to contentSkip to conversation

asyncrpc: Handle websockets exceptions

PATCH In review Philip Lorenz 2 participants 3 messages Dec 30, 2024 – Jan 8, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 asyncrpc: Handle websockets exceptions

The websockets library throws a number of exceptions which are currently not caught leading to unhandled exceptions in the idle loop.

Fix this by catching them and reexposing them as a ConnectionError which is the exception expected by users of asyncrpc.

---
 lib/bb/asyncrpc/client.py | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
lib/bb/asyncrpc/client.py +10 -5
index 9be49261c..17b72033b 100644
@@ -112,11 +112,16 @@ class AsyncClient(object):
             )
 
         async def connect_sock():
-            websocket = await websockets.connect(
-                uri,
-                ping_interval=None,
-                open_timeout=self.timeout,
-            )
+            try:
+                websocket = await websockets.connect(
+                    uri,
+                    ping_interval=None,
+                    open_timeout=self.timeout,
+                )
+            except asyncio.exceptions.TimeoutError:
+                raise ConnectionError("Timeout while connecting to websocket")
+            except (OSError, websockets.InvalidHandshake, websockets.InvalidURI) as exc:
+                raise ConnectionError(f"Could not connect to websocket: {exc}") from exc
             return WebsocketConnection(websocket, self.timeout)
 
         self._connect_sock = connect_sock
#1 lore raw

lib/bb/asyncrpc/client.py class AsyncClient(object): · line 124

Quoted 4 lines · Philip Lorenz via lists.openembedded.org
lib/bb/asyncrpc/client.py +4 -0
+            except asyncio.exceptions.TimeoutError:
+                raise ConnectionError("Timeout while connecting to websocket")
+            except (OSError, websockets.InvalidHandshake, websockets.InvalidURI) as exc:
+                raise ConnectionError(f"Could not connect to websocket: {exc}") from exc

Is there a reason to not use the original exception in the TimeoutError case, but use it in the OSError case?

Ross

#2 lore raw

Hi Ross,

Quoted 6 lines · Ross Burton

lib/bb/asyncrpc/client.py class AsyncClient(object): · line 124

Quoted 4 lines · Philip Lorenz via lists.openembedded.org
lib/bb/asyncrpc/client.py +4 -0
+            except asyncio.exceptions.TimeoutError:
+                raise ConnectionError("Timeout while connecting to websocket")
+            except (OSError, websockets.InvalidHandshake, websockets.InvalidURI) as exc:
+                raise ConnectionError(f"Could not connect to websocket: {exc}") from exc

Is there a reason to not use the original exception in the TimeoutError case, but use it in the OSError case?

I added an explicit block for the timeout case as unfortunately the string representation of a TimeoutError is the empty string (so the error message for that case would not be very helpful):

Quoted 6 lines

Python 3.13.1 (main, Dec  4 2024, 18:05:56) [GCC 14.2.1 20240910] on linux Type "help", "copyright", "credits" or "license" for more information.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

import asyncio
str(asyncio.exceptions.TimeoutError())

''

Quoted 1 line
Quoted 1 line
Quoted 1 line

Br,

Philip

Signature · 7 lines
Philip Lorenz
BMW Car IT GmbH, Software-Plattform, -Integration Connected Company, Lise-Meitner-Straße 14, 89081 Ulm
-------------------------------------------------------------------------
BMW Car IT GmbH
Management: Chris Brandt, Michael Böttrich, Christian Salzmann
Domicile and Court of Registry: München HRB 134810
-------------------------------------------------------------------------