Title: Thread.start() retains an unstarted thread when an audit hook raises BaseException · Issue #160864 · python/cpython · GitHub
Open Graph Title: Thread.start() retains an unstarted thread when an audit hook raises BaseException · Issue #160864 · python/cpython
X Title: Thread.start() retains an unstarted thread when an audit hook raises BaseException · Issue #160864 · python/cpython
Description: Bug report Thread.start() leaves a thread in _limbo when the native-start audit hook raises KeyboardInterrupt or SystemExit. No OS thread was created, but threading.enumerate() keeps returning the thread. Raising RuntimeError from the sa...
Open Graph Description: Bug report Thread.start() leaves a thread in _limbo when the native-start audit hook raises KeyboardInterrupt or SystemExit. No OS thread was created, but threading.enumerate() keeps returning the ...
X Description: Bug report Thread.start() leaves a thread in _limbo when the native-start audit hook raises KeyboardInterrupt or SystemExit. No OS thread was created, but threading.enumerate() keeps returning the ...
Opengraph URL: https://github.com/python/cpython/issues/160864
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Thread.start() retains an unstarted thread when an audit hook raises BaseException","articleBody":"### Bug report\n\n`Thread.start()` leaves a thread in `_limbo` when the native-start audit hook raises `KeyboardInterrupt` or `SystemExit`. No OS thread was created, but `threading.enumerate()` keeps returning the thread. Raising `RuntimeError` from the same hook already cleans up correctly.\n\n```python\nimport sys\nimport threading\n\ndef refuse(event, args):\n if event == \"_thread.start_joinable_thread\":\n raise KeyboardInterrupt\n\nsys.addaudithook(refuse)\nthread = threading.Thread()\ntry:\n thread.start()\nexcept KeyboardInterrupt:\n pass\n\nprint(thread._os_thread_handle.ident) # 0: no OS thread was created\nprint(thread.is_alive()) # False\nprint(thread in threading.enumerate()) # True\n```\n\nI expect the failed thread to be removed from the registry. [`threading.enumerate()` excludes threads that have not started](https://docs.python.org/3/library/threading.html#threading.enumerate).\n\nThe native-start call is wrapped in `except Exception`, which misses these exceptions. Cleanup must also distinguish refusal from an exception arriving as the native call returns after creating an OS thread; that thread still needs its registration.\n\nThis differs from #140746 and #153776, where an OS thread is created and then fails during bootstrap. The earlier cleanup in #51730 covers `ThreadError`.\n\n### CPython versions tested on\n\nCPython main, `f7c39d2316e59f17148d93f9ec2d6c4d2b610029`, debug build of 3.16.0a1+dev.\n\n### Operating systems tested on\n\nmacOS 26.6, arm64.\n\nAI tools helped investigate this report and prepare its reproducer.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-160865\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/GriffinCanCode","@type":"Person","name":"GriffinCanCode"},"datePublished":"2026-10-29T04:12:33Z","url":"https://github.com/python/cpython/issues/160864"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:a4a1d437-1de7-b3b8-60b1-c62cb4991598 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A5A0:785A9:4415888:564E0B2:6B022F75 |
| html-safe-nonce | e4c7c30f3bbbc6ef7403c262905f1c2c07f39a58199bfd6f0c7c62888c3c77c9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNUEwOjc4NUE5OjQ0MTU4ODg6NTY0RTBCMjo2QjAyMkY3NSIsInZpc2l0b3JfaWQiOiI5MTM2Mzg2MTU2MTk4NTA2MzU3IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4c8ebdb2d44c1b86f94fbb785fdcde31c6a70b5681300effd802adae08228e48 |
| hovercard-subject-tag | issue:6092617728 |
| 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/160864/issue_layout |
| twitter:image | https://opengraph.githubassets.com/39cc73c31f5008ac2d9f23be95b8fb721ab85ecc55be7f0affcd23f48c2c11d1/python/cpython/issues/160864 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/39cc73c31f5008ac2d9f23be95b8fb721ab85ecc55be7f0affcd23f48c2c11d1/python/cpython/issues/160864 |
| og:image:alt | Bug report Thread.start() leaves a thread in _limbo when the native-start audit hook raises KeyboardInterrupt or SystemExit. No OS thread was created, but threading.enumerate() keeps returning the ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | GriffinCanCode |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width