| Message ID | 20241230123547.2593913-1-philip.lorenz@bmw.de |
|---|---|
| State | Accepted, archived |
| Commit | 41d62911a480283287265fe063696d2acd5904aa |
| Headers | show |
| Series | asyncrpc: Handle websockets exceptions | expand |
On 30 Dec 2024, at 12:35, Philip Lorenz via lists.openembedded.org <philip.lorenz=bmw.de@lists.openembedded.org> wrote: > + 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
Hi Ross, On 08.01.25 12:36, Ross Burton wrote: > On 30 Dec 2024, at 12:35, Philip Lorenz via lists.openembedded.org <philip.lorenz=bmw.de@lists.openembedded.org> wrote: >> + 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): > 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. > >>> import asyncio > >>> str(asyncio.exceptions.TimeoutError()) > '' > >>> Br, Philip
diff --git a/lib/bb/asyncrpc/client.py b/lib/bb/asyncrpc/client.py index 9be49261c..17b72033b 100644 --- a/lib/bb/asyncrpc/client.py +++ b/lib/bb/asyncrpc/client.py @@ -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
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`. Signed-off-by: Philip Lorenz <philip.lorenz@bmw.de> --- lib/bb/asyncrpc/client.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)