From 383bcef56e89b884c5b35686933033f5b6766d11 Mon Sep 17 00:00:00 2001 From: Ali Al-Alak Date: Tue, 25 Feb 2025 19:55:23 +0100 Subject: [PATCH 1/2] Catching asyncio.CancelledError instead of KeyboardInterrupt and EOFError. Asyncio will catch these errors outside of the task and raise the CancelledError to cancel the task. This makes sure that the websocket client is properly disconnected if the user press control-C or control-D. --- src/websockets/cli.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/websockets/cli.py b/src/websockets/cli.py index 4531d566..93b6854e 100644 --- a/src/websockets/cli.py +++ b/src/websockets/cli.py @@ -2,6 +2,7 @@ import argparse import asyncio +from asyncio import CancelledError import os import sys from typing import Generator @@ -123,7 +124,7 @@ async def interactive_client(uri: str) -> None: [incoming, outgoing], return_when=asyncio.FIRST_COMPLETED, ) - except (KeyboardInterrupt, EOFError): # ^C, ^D # pragma: no cover + except CancelledError: # ^C, ^D # pragma: no cover pass finally: incoming.cancel() From 8256e806738a1178661f4f8ecc391b12e811261b Mon Sep 17 00:00:00 2001 From: Ali Al-Alak Date: Tue, 25 Feb 2025 20:34:07 +0100 Subject: [PATCH 2/2] formatting change --- src/websockets/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/websockets/cli.py b/src/websockets/cli.py index 93b6854e..6157613c 100644 --- a/src/websockets/cli.py +++ b/src/websockets/cli.py @@ -2,9 +2,9 @@ import argparse import asyncio -from asyncio import CancelledError import os import sys +from asyncio import CancelledError from typing import Generator from .asyncio.client import ClientConnection, connect