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

Handling of KeyboardInterrupt #964

Open
milosivanovic opened this issue Jan 5, 2024 · 0 comments
Open

Handling of KeyboardInterrupt #964

milosivanovic opened this issue Jan 5, 2024 · 0 comments

Comments

@milosivanovic
Copy link

milosivanovic commented Jan 5, 2024

The documentation for run_forever() states that run_forever() will return a bool that will be True if there was an exception, or False if KeyboardInterrupt was caught, or if the websocket was closed (I assume from the server side.)

This does not reflect the current code which catches KeyboardInterrupt and Exception together in the same block. In my testing, when I send CTRL+C at the terminal (the parent process), it's also sent to all child processes (of which, in my case, websocket-client is one), and the return value of run_forever() is always True, so I can't differentiate between the KeyboardInterrupt sent from the terminal and an actual Exception that occurred naturally in the child process.

(Additionally, this line should use repr(e) because it does not even log that KeyboardInterrupt was the reason for the teardown since str(e) is blank for KeyboardInterrupt.)

But, in my opinion, websocket-client should not meddle with KeyboardInterrupt at all as that is the main process's concern. The way it is now, I can't catch KeyboardInterrupt and handle it gracefully from the main process (the one actually at the terminal, vs a background process which is what websocket-client is for me) because it's caught at a lower level in this library. I believe this to be bad practice.

I propose that KeyboardInterrupt be removed from the aforementioned line so that it can be caught at the outer level. I don't see the reason for catching KeyboardInterrupt there and it makes the code less modular since now this signal is absorbed by the websocket-client library can't be customised.

As it stands, the following simple example shows how it's not possible to handle KeyboardInterrupt at the main level. It is expected that KeyboardInterrupt can always be handled like this.

try:
    client.run_forever()
except KeyboardInterrupt:
    # never happens, because run_forever() catches KeyboardInterrupt itself
    print("interrupt received, do something...")

In addition, the documentation should be updated to reflect the new situation once an agreement is made on this issue.

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

No branches or pull requests

1 participant