Skip to content

Handle KeyboardInterrupt gracefully in ConcurrencyGroup#901

Open
qi-imbue wants to merge 1 commit intomainfrom
mng/sigint
Open

Handle KeyboardInterrupt gracefully in ConcurrencyGroup#901
qi-imbue wants to merge 1 commit intomainfrom
mng/sigint

Conversation

@qi-imbue
Copy link
Contributor

When a user presses Ctrl+C, ConcurrencyGroup now sets the shutdown event and suppresses cleanup errors (strand timeouts, process failures) instead of producing scary ConcurrencyExceptionGroup tracebacks. The original KeyboardInterrupt propagates cleanly.

Changes:

  • ConcurrencyGroup.exit: detect KeyboardInterrupt, call shutdown(), and suppress cleanup exceptions from _exit()
  • setup_command_context: detect active KeyboardInterrupt via sys.exc_info() and pass it to exit (Click's call_on_close passes None by default)

When a user presses Ctrl+C, ConcurrencyGroup now sets the shutdown event
and suppresses cleanup errors (strand timeouts, process failures) instead
of producing scary ConcurrencyExceptionGroup tracebacks. The original
KeyboardInterrupt propagates cleanly.

Changes:
- ConcurrencyGroup.__exit__: detect KeyboardInterrupt, call shutdown(),
  and suppress cleanup exceptions from _exit()
- setup_command_context: detect active KeyboardInterrupt via sys.exc_info()
  and pass it to __exit__ (Click's call_on_close passes None by default)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@qi-imbue qi-imbue changed the base branch from mng/modal-testing-shims to main March 17, 2026 18:21
Comment on lines 141 to +145
except BaseException as exit_exception:
if isinstance(exc_value, KeyboardInterrupt):
# When the user presses Ctrl+C, suppress cleanup errors (strand timeouts, process
# failures, etc.) and let the original KeyboardInterrupt propagate cleanly.
return
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this suspicious? like, couldn't this have been caused by the user hitting ctrl c a bunch of times, eg, while inside of the exit() function above?

seems like we kinda need to do this a little bit differently / more robustly in practice...

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