Skip to content

gh-146194: Fix nested KeyboardInterrupt handling in asyncio#146203

Closed
okiemute04 wants to merge 5 commits intopython:mainfrom
okiemute04:fix-asyncio-nested-keyboardinterrupt
Closed

gh-146194: Fix nested KeyboardInterrupt handling in asyncio#146203
okiemute04 wants to merge 5 commits intopython:mainfrom
okiemute04:fix-asyncio-nested-keyboardinterrupt

Conversation

@okiemute04
Copy link
Contributor

@okiemute04 okiemute04 commented Mar 20, 2026

Fixes #146194

Problem

When multiple Ctrl+C presses occurred in nested asyncio tasks, the
third press would cause a crash with "Task was destroyed but it is pending!"
instead of gracefully cancelling the nested tasks.

Solution

Modified _on_sigint in Lib/asyncio/runners.py to cancel the main task
on every SIGINT, allowing nested cancellations to propagate correctly
through multiple levels.

Testing

  • Added test_nested_keyboardinterrupt_handling to test_runners.py
  • Verified that multiple cancellations work correctly without crashes
  • All existing runner tests pass

Loading
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.

Asyncio does not handle nested KeyboardInterrupt correctly

3 participants