Title: Add HEAD_ISLOCKED to wrap GIL check · Issue #125908 · python/cpython · GitHub
Open Graph Title: Add HEAD_ISLOCKED to wrap GIL check · Issue #125908 · python/cpython
X Title: Add HEAD_ISLOCKED to wrap GIL check · Issue #125908 · python/cpython
Description: Feature or enhancement Proposal: This issue is extracted from #125561. When interpretation_clear is changed, the PyThreadState_Clear function needs to be called when HEAD_LOCK is not held, but in fact we cannot be sure, but it existed be...
Open Graph Description: Feature or enhancement Proposal: This issue is extracted from #125561. When interpretation_clear is changed, the PyThreadState_Clear function needs to be called when HEAD_LOCK is not held, but in f...
X Description: Feature or enhancement Proposal: This issue is extracted from #125561. When interpretation_clear is changed, the PyThreadState_Clear function needs to be called when HEAD_LOCK is not held, but in f...
Opengraph URL: https://github.com/python/cpython/issues/125908
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add HEAD_ISLOCKED to wrap GIL check","articleBody":"# Feature or enhancement\r\n\r\n### Proposal:\r\n\r\nThis issue is extracted from https://github.com/python/cpython/pull/125561.\r\nWhen interpretation_clear is changed, the `PyThreadState_Clear` function needs to be called when `HEAD_LOCK` is not held, but in fact we cannot be sure, but it existed before the change. In fact, PR solves the competition, but does not eliminate the risk of deadlock, so I give the following solution.\r\n\r\nFor `PyThreadState_Clear`, I want to add a `HEAD_ISLOCKED` macro to wrap `PyMutex_IsLocked`. This allows a check to be made when UNLOCK is called. Otherwise, if HEAD_LOCK is not called in the context, an is unlock error will occur when HEAD_UNLOCK is called. But in fact, we cannot be sure whether HEAD_LOCK is called in the context, and HEAD_IS_LOCK can be used to check it.\r\n\r\nchanged\r\n```\r\ndiff --git a/Python/pystate.c b/Python/pystate.c\r\nindex 6b4e1d470f8..dc67e50bc27 100644\r\n--- a/Python/pystate.c\r\n+++ b/Python/pystate.c\r\n@@ -796,6 +796,7 @@ interpreter_clear(PyInterpreterState *interp, PyThreadState *tstate)\r\n // See https://github.com/python/cpython/issues/102126\r\n // Must be called without HEAD_LOCK held as it can deadlock\r\n // if any finalizer tries to acquire that lock.\r\n+ HEAD_UNLOCK(\u0026_PyRuntime);\r\n PyThreadState_Clear(p);\r\n p = p-\u003enext;\r\n }\r\n```\r\nThe above code will show **no locked** when the context does not hold HEAD_LOCK, but this is not certain. So I want to add an if statement in front to unlock it when holding HEAD_LOCK. Ensure that there is no deadlock when calling `PyThreadState_Clear`.\r\n```\r\nif (HEAD_ISLOCKED(\u0026_PyRuntime)) {\r\n HEAD_UNLOCK(\u0026_PyRuntime);\r\n}\r\nPyThreadState_Clear(p);\r\n```\r\n### Has this already been discussed elsewhere?\r\n\r\nNo response given\r\n\r\n### Links to previous discussion of this feature:\r\n\r\n_No response_","author":{"url":"https://github.com/rruuaanng","@type":"Person","name":"rruuaanng"},"datePublished":"2024-10-24T01:47:33.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/125908/cpython/issues/125908"}
| 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:ad51248e-0193-71c7-e4dc-af652fad9a27 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8EDE:1B0FEF:EE22AD:14113F4:6A583146 |
| html-safe-nonce | b60249201cfdf901c58e51fede70d126ac46bf065739b05b88623fbfbd3252d2 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RURFOjFCMEZFRjpFRTIyQUQ6MTQxMTNGNDo2QTU4MzE0NiIsInZpc2l0b3JfaWQiOiI1NTQzMDg3MTQzMzQ4ODc1NTkwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 3f155d6e8dfe2617da60d97566af94581a88f111a15673387b633063bf4b9a86 |
| hovercard-subject-tag | issue:2610196307 |
| 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/125908/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f3d67f0feaedb4ddcdb325d5cc0642d868ac62dab502b1d0d68b03754a7f3cc3/python/cpython/issues/125908 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f3d67f0feaedb4ddcdb325d5cc0642d868ac62dab502b1d0d68b03754a7f3cc3/python/cpython/issues/125908 |
| og:image:alt | Feature or enhancement Proposal: This issue is extracted from #125561. When interpretation_clear is changed, the PyThreadState_Clear function needs to be called when HEAD_LOCK is not held, but in f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rruuaanng |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f2a0c7865178af3d91dd9f13b0cdfc3c73a2529c873d2780bb4c01271a57ec6 |
| 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 | 8aae7b8d6caacacf5c66eaeb2702d8dc88d85b4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width