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

Python: don't catch KeyboardInterrupt and SystemExit #4333

Merged
merged 5 commits into from
Nov 27, 2023

Conversation

Dvad
Copy link
Contributor

@Dvad Dvad commented Nov 26, 2023

What

Currently, the python sdk will catch any exception raised inside a rerun log function and surface it as a printed warning rather than an exception to the client. This makes sense to avoid crashing the app for a mistake in the argument of the log function. This can be disabled with the "strict mode" through an environment variables.

However, there are little reasons to emit a warning and keep-on going when a KeyboardInterrupt exception is received. The official doc states even that this kind of exception are better not being caught at all:

Note Catching a KeyboardInterrupt requires special consideration. Because it can be raised at unpredictable points, it may, in some circumstances, leave the running program in an inconsistent state. It is generally best to allow KeyboardInterrupt to end the program as quickly as possible or avoid raising it entirely. (See Note on Signal Handlers and Exceptions.)

I propose to have a special case for this type of exception, and throwing them back to the client even in non strict mode.

I tested, that Ctrl-C now interrupts some test program instantly instead of being unreliable before (because sometime catches inside rerun log).

Checklist

  • I have read and agree to Contributor Guide and the Code of Conduct
  • The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG

There are little reasons to emit a warning and keep-on going when a KeyboardInterrupt exception is received (i.e. was sent by the user). 
I propose to let this one be passed to the client even in non strict mode
@Dvad Dvad changed the title Always throw back KeyboardInterrupt to the user when emitted in log function. Python: always throw back KeyboardInterrupt to the user when emitted in log function. Nov 26, 2023
Copy link
Member

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - thanks for the PR!

emilk
emilk previously requested changes Nov 26, 2023
@@ -205,7 +205,7 @@ def __exit__(
exc_tb: TracebackType | None,
) -> bool:
try:
if exc_type is not None and not strict_mode():
if exc_type is not None and exc_type is not KeyboardInterrupt and not strict_mode():
Copy link
Member

@emilk emilk Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is the better fix - this will ignore both KeyboardInterrupt and SystemExit

Suggested change
if exc_type is not None and exc_type is not KeyboardInterrupt and not strict_mode():
if exc_type is Exception and not strict_mode():

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the intention, perhaps worth adding a comment to the effect as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emilk well spotted (I had forgotten about that Exception vs. BaseException thing). I fixed the code with the correct issubclass check + a comment.

@emilk emilk added this to the 0.11 milestone Nov 27, 2023
@emilk emilk changed the title Python: always throw back KeyboardInterrupt to the user when emitted in log function. Python: don't catch KeyboardInterrupt and SystemExit Nov 27, 2023
@abey79 abey79 dismissed emilk’s stale review November 27, 2023 14:49

Changes were made, but needed to unblock merge.

@abey79 abey79 merged commit aca6976 into rerun-io:main Nov 27, 2023
34 of 35 checks passed
@Dvad Dvad deleted the patch-1 branch November 27, 2023 19:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants