Gracefully closing the cli when KeyboardInterrupt signal is sent #1599
Gracefully closing the cli when KeyboardInterrupt signal is sent #1599a-alak wants to merge 2 commits intopython-websockets:mainfrom
Conversation
…rror. 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.
|
Ah, I should have tested it, I'm getting the same 🤦♂️ Looks like I always exit with ^D (just a habit to exit an interactive shell) and never with ^C so I didn't notice. Previously there was a 10s delay; now there's a traceback; we're almost there :-) |
|
Thank you, this is the correct fix indeed. I added more comments and committed the fix. |
|
Amazing! Happy to help 🙌🏽 Thank you for quick response! |
Honestly, it's very random... I'm considering publishing a bugfix release 15.0.1 just for this regression. |
|
I just released 15.0.1. It will be on PyPI as soon as https://github.com/python-websockets/websockets/actions/runs/13684207641 completes. |
|
Amazing! Thanks 🙏🏽 |
While setting up entry point for the cli (#1598) we saw that the cli does not properly close the connection on KeyboardInterrupt.
Doing a bit of research it seems that asyncio will catch KeyboardInterrupt outside of the task and then cancel the task. From python 3.11 asyncio will raise a CancelledError in the task instead. Catching this in our tasks enable the cli to gracefully exit with no error. In python 3.9 and 3.10 this will still properly close the websocket connection, but you will get a KeyboardInterrupt error (still outside our task, so catching this error inside the task does not change anything).
Should we also silent the error in python 3.9 and python 3.10?