diff mbox series

asyncrpc: Handle websockets exceptions

Message ID 20241230123547.2593913-1-philip.lorenz@bmw.de
State Accepted, archived
Commit 41d62911a480283287265fe063696d2acd5904aa
Headers show
Series asyncrpc: Handle websockets exceptions | expand

Commit Message

Philip Lorenz Dec. 30, 2024, 12:35 p.m. UTC
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(-)

Comments

Ross Burton Jan. 8, 2025, 11:36 a.m. UTC | #1
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
Philip Lorenz Jan. 8, 2025, 11:44 a.m. UTC | #2
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 mbox series

Patch

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