Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully closing the cli when KeyboardInterrupt signal is sent #1599

Closed
wants to merge 2 commits into from

Conversation

a-alak
Copy link
Contributor

@a-alak a-alak commented Feb 25, 2025

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?

…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.
@aaugustin
Copy link
Member

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 :-)

@aaugustin aaugustin closed this in 5fa24bb Feb 26, 2025
@aaugustin
Copy link
Member

Thank you, this is the correct fix indeed. I added more comments and committed the fix.

@a-alak
Copy link
Contributor Author

a-alak commented Feb 26, 2025

Amazing! Happy to help 🙌🏽 Thank you for quick response!
Just curious, how is your release cycle? Looking forward to run the cli with uvx 😇

@aaugustin
Copy link
Member

how is your release cycle?

Honestly, it's very random...

I'm considering publishing a bugfix release 15.0.1 just for this regression.

@aaugustin
Copy link
Member

I just released 15.0.1. It will be on PyPI as soon as https://github.com/python-websockets/websockets/actions/runs/13684207641 completes.

@a-alak
Copy link
Contributor Author

a-alak commented Mar 6, 2025

Amazing! Thanks 🙏🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants