Title: Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic · Issue #101436 · python/cpython · GitHub
Open Graph Title: Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic · Issue #101436 · python/cpython
X Title: Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic · Issue #101436 · python/cpython
Description: In PyThreadState_Clear() and PyInterpreterState_Clear() the code expects a "current" thread state (i.e. set by PyThreadState_Swap()) to be set. Presumably this is for the following: to ensure the GIL is held to use for logging audit even...
Open Graph Description: In PyThreadState_Clear() and PyInterpreterState_Clear() the code expects a "current" thread state (i.e. set by PyThreadState_Swap()) to be set. Presumably this is for the following: to ensure the G...
X Description: In PyThreadState_Clear() and PyInterpreterState_Clear() the code expects a "current" thread state (i.e. set by PyThreadState_Swap()) to be set. Presumably this is for the following: to en...
Opengraph URL: https://github.com/python/cpython/issues/101436
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Requiring A Current Thread State for PyThreadState_Clear(), etc. is Problematic","articleBody":"In `PyThreadState_Clear()` and `PyInterpreterState_Clear()` the code expects a \"current\" thread state (i.e. set by `PyThreadState_Swap()`) to be set. Presumably this is for the following:\r\n\r\n* to ensure the GIL is held\r\n* to use for logging audit events (actually only needs the current interpreter?)\r\n* to use in object finalizers for objects held by the target thread/interpreter state\r\n\r\nFor `PyThreadState_Clear()`, this is a problem when the current thread state is also the target thread state, since the function is progressively rendering the thread state unusable. For `PyInterpreterState_Clear()` the \"current\" thread state must belong to the target interpreter, but will be rendered unusable before it's potentially needed.\r\n\r\n(FWIW, it isn't clear to me yet why we don't see frequent crashes from this already (at least when we're clearing the main interpreter during finalization). I do suspect that this contributes to some of the occasional CI failures we see relative to finalization.)\r\n\r\nIdeally auditing and the various object finalizers could work without having a current thread state, especially in the case where the current thread state could only possibly be the one getting cleared. Could we split up `PyThreadState` so at least the problematic part is guaranteed not be used in `PyThreadState_Clear()` or `PyInterpreterState_Clear()`? Are there other complexities we have to deal with?\r\n\r\n(Note that we'd still require that the GIL be held by the current OS thread when `PyThreadState_Clear()` or `PyInterpreterState_Clear()` are called.)\r\n\r\n\u003cdetails\u003e\r\n\u003csummary\u003eFTR, in gh-101308 I noted various cases where the lifecycle of thread states has an unexpected status. Pretty much all of them relate `PyThreadState_Clear()` requiring a current thread state.\u003c/summary\u003e\r\n\r\n\u003cBR/\u003e\r\nDealing with the main issue above would make it trivial to address the following:\r\n\r\n* an \"active\" thread state (i.e. \"current\", holding the GIL) must be \"bound\" (to an OS thread) already\r\n* a \"bound\" thread state must be \"alive\" (initialized but not finalizing)\r\n* binding a thread state should be done separately from `PyThreadState_New()`\r\n* the thread state passed to `PyThreadState_Clear()` should no longer be \"active\" or \"bound\"\r\n* the current thread state must belong to the target interpreter of `PyThreadState_Clear()`\r\n* the current thread state must belong to the target interpreter of `PyInterpreterState_Clear()` (for the main interpreter it must be the main thread)\r\n\r\n\u003c/details\u003e","author":{"url":"https://github.com/ericsnowcurrently","@type":"Person","name":"ericsnowcurrently"},"datePublished":"2023-01-30T20:27:15.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/101436/cpython/issues/101436"}
| 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:0f621ed2-6a74-755c-bbdb-dfd2a0025ad5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E3E0:12A9AF:A04BB0:CDECA8:696B20D0 |
| html-safe-nonce | 7f7fdcc43d10a4ea9aeb6dfdca201ac9dd5d59fe07b9a8652080fb2d7db6820f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFM0UwOjEyQTlBRjpBMDRCQjA6Q0RFQ0E4OjY5NkIyMEQwIiwidmlzaXRvcl9pZCI6IjUxNDA5MTczNTc5MDI1MDQxNDQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 371acf172a5a15e4cd87ddbc86670497df86f48a103a10c3c50d9f01bfcff5b5 |
| hovercard-subject-tag | issue:1563152541 |
| 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/101436/issue_layout |
| twitter:image | https://opengraph.githubassets.com/fb2eeb3c65e1d7e52a1837d114f6d0cadaef86c9f1395f2c2f1bfc8edc6f19c8/python/cpython/issues/101436 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/fb2eeb3c65e1d7e52a1837d114f6d0cadaef86c9f1395f2c2f1bfc8edc6f19c8/python/cpython/issues/101436 |
| og:image:alt | In PyThreadState_Clear() and PyInterpreterState_Clear() the code expects a "current" thread state (i.e. set by PyThreadState_Swap()) to be set. Presumably this is for the following: to ensure the G... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ericsnowcurrently |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width