Title: Asyncio does not handle nested KeyboardInterrupt correctly · Issue #146194 · python/cpython · GitHub
Open Graph Title: Asyncio does not handle nested KeyboardInterrupt correctly · Issue #146194 · python/cpython
X Title: Asyncio does not handle nested KeyboardInterrupt correctly · Issue #146194 · python/cpython
Description: Bug report Bug description: Consider this code import sys import logging import asyncio import signal from time import* logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) async def f(): if 1: main_task = asyncio...
Open Graph Description: Bug report Bug description: Consider this code import sys import logging import asyncio import signal from time import* logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) ...
X Description: Bug report Bug description: Consider this code import sys import logging import asyncio import signal from time import* logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) ...
Opengraph URL: https://github.com/python/cpython/issues/146194
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Asyncio does not handle nested KeyboardInterrupt correctly","articleBody":"# Bug report\n\n### Bug description:\n\nConsider this code\n\n```python\nimport sys\nimport logging\nimport asyncio\nimport signal\nfrom time import*\n\nlogger = logging.getLogger(__name__)\n\nlogging.basicConfig(level=logging.INFO)\n\nasync def f():\n if 1:\n main_task = asyncio.current_task()\n def on_sigint() -\u003e None:\n logger.error(\"Received SIGINT\")\n main_task.cancel(KeyboardInterrupt)\n asyncio.get_running_loop().add_signal_handler(signal.SIGINT, on_sigint)\n try:\n while True:\n await asyncio.sleep(0.1)\n print(\"*\")\n except asyncio.CancelledError as e:\n print(e)\n try:\n while True:\n await asyncio.sleep(0.1)\n print(\"#\")\n except asyncio.CancelledError:\n try:\n while True:\n await asyncio.sleep(0.1)\n print(\"!\")\n except asyncio.CancelledError:\n while True:\n await asyncio.sleep(0.1)\n print(\"@\")\n\nasyncio.run(f())\n```\n\nIntention: each time I press Ctrl-C, it repeatedly print `*`, `#`, `!`, `@` respectively.\n\nAs is, it does do that correctly.\n\nHowever, if I don't manually add the SIGINT signal handler, it will print `*`, {first Ctrl-C} `#`, {second Ctrl-C} `!`, {third Ctrl-C} then forcefully stop.\n\nI don't think this behavior is desirable. Or at least it probably shouldn't log a scary error `Task was destroyed but it is pending!`?\n\n### CPython versions tested on:\n\n3.13\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-146203\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/user202729","@type":"Person","name":"user202729"},"datePublished":"2026-03-20T08:09:00.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/146194/cpython/issues/146194"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e7449892-55cf-5054-2d83-02b0455dc5e4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EBFC:2F87BF:85941C:B5F92A:6A51F9DC |
| html-safe-nonce | 801b81555fdbd41e222124d7e21616834f7e8cc05581a2e8ac46c577bdc177fc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQkZDOjJGODdCRjo4NTk0MUM6QjVGOTJBOjZBNTFGOURDIiwidmlzaXRvcl9pZCI6IjQ1NjkyMDQwOTkyNTQ0NTA2NTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | eb4717cd1fb67b40fb4c4580ac1e4803bb0e5b807a593c0deebe58497266e35d |
| hovercard-subject-tag | issue:4106495966 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/146194/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f379a00edfffc1a935a55285b3901de947fc06b9304e52c1b7b06f47b37468af/python/cpython/issues/146194 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f379a00edfffc1a935a55285b3901de947fc06b9304e52c1b7b06f47b37468af/python/cpython/issues/146194 |
| og:image:alt | Bug report Bug description: Consider this code import sys import logging import asyncio import signal from time import* logger = logging.getLogger(__name__) logging.basicConfig(level=logging.INFO) ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | user202729 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7aed05249554b889eb33d002851a973eebcc7e91 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width