Title: An interpreter's initial thread can be accessed while finalizing · Issue #126914 · python/cpython · GitHub
Open Graph Title: An interpreter's initial thread can be accessed while finalizing · Issue #126914 · python/cpython
X Title: An interpreter's initial thread can be accessed while finalizing · Issue #126914 · python/cpython
Description: Crash report What happened? While working on gh-126644, I came across an elusive bug that seems to occur when a lot of threads are trying to create a thread state for the same interpreter. Initially, I thought it was an issue with free-t...
Open Graph Description: Crash report What happened? While working on gh-126644, I came across an elusive bug that seems to occur when a lot of threads are trying to create a thread state for the same interpreter. Initiall...
X Description: Crash report What happened? While working on gh-126644, I came across an elusive bug that seems to occur when a lot of threads are trying to create a thread state for the same interpreter. Initiall...
Opengraph URL: https://github.com/python/cpython/issues/126914
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"An interpreter's initial thread can be accessed while finalizing","articleBody":"# Crash report\n\n### What happened?\n\nWhile working on gh-126644, I came across an elusive bug that seems to occur when a lot of threads are trying to create a thread state for the same interpreter. Initially, I thought it was an issue with free-threading, but it occurs on the default build as well.\n\nHere's a small reproducer:\n\n```py\nfrom threading import Thread\nimport _interpreters\n\ninterp = _interpreters.create()\n\ndef run():\n this_interp = _interpreters.create()\n _interpreters.run_string(this_interp, f\"import _interpreters; _interpreters.run_string({interp}, '1')\")\n\nthreads = [Thread(target=run) for _ in range(1000)]\nfor thread in threads:\n thread.start()\n\nfor thread in threads:\n thread.join()\n```\n\nAs it turns out, this is because of this check: https://github.com/python/cpython/blob/main/Python/pystate.c#L1531\n\nInside `tstate_delete_common`, the runtime lock is released after the `threads.head` has already been set to `NULL`, so other threads think erroneously think it's OK to try and use `_initial_thread` while it's still getting finalized. Possibly related, `free_threadstate` tries to reset `_initial_thread` back to the default settings without the runtime lock, which is possibly racy.\n\nThere's a few ways to fix this, but I think the easiest (and has the least amount of risk for backporting) is to just hold the runtime lock for all of thread state deletion. This will introduce a very slight slowdown due to lock contention, but that should get better once #114940 lands. I have a PR ready.\n\n### CPython versions tested on:\n\n3.12, 3.13, 3.14, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-126915\n* gh-126989\n* gh-127114\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/ZeroIntensity","@type":"Person","name":"ZeroIntensity"},"datePublished":"2024-11-16T22:41:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/126914/cpython/issues/126914"}
| 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:9602a9a7-a3d2-75ae-df1d-3032456cb609 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE92:35E4DB:354C070:494E283:6A571449 |
| html-safe-nonce | f391d8440d0545a8334cb4eb4e0bd0811fb691ac0dea48f73aa4eb2072546a01 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTkyOjM1RTREQjozNTRDMDcwOjQ5NEUyODM6NkE1NzE0NDkiLCJ2aXNpdG9yX2lkIjoiODkyODQ1MTYzNzI3NDczMzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ae570d4e688bca115e099da8af754253eba29178e19fea302b32c21355f5bc06 |
| hovercard-subject-tag | issue:2665042861 |
| 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/126914/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2aebd47f32bcad3ac891e5de8e2988d0a79c582f1e9058aad02bbaef2ca9729c/python/cpython/issues/126914 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2aebd47f32bcad3ac891e5de8e2988d0a79c582f1e9058aad02bbaef2ca9729c/python/cpython/issues/126914 |
| og:image:alt | Crash report What happened? While working on gh-126644, I came across an elusive bug that seems to occur when a lot of threads are trying to create a thread state for the same interpreter. Initiall... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ZeroIntensity |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4e7a7296a3830877cf21a6ad2a972c9e618a48915e03966cf0c53eb08e5aad98 |
| 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 | 2576d1f0198cf1588faf2edf27f1ed120903be10 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width