Title: Improve critical sections and stop-the-world interoperability · Issue #144513 · python/cpython · GitHub
Open Graph Title: Improve critical sections and stop-the-world interoperability · Issue #144513 · python/cpython
X Title: Improve critical sections and stop-the-world interoperability · Issue #144513 · python/cpython
Description: Feature or enhancement Currently, it's not safe to use critical sections within stop-the-world (STW) pauses because it risks deadlock. The deadlock risk isn't obvious and actual deadlocks happens infrequently, which makes our unit tests ...
Open Graph Description: Feature or enhancement Currently, it's not safe to use critical sections within stop-the-world (STW) pauses because it risks deadlock. The deadlock risk isn't obvious and actual deadlocks happens i...
X Description: Feature or enhancement Currently, it's not safe to use critical sections within stop-the-world (STW) pauses because it risks deadlock. The deadlock risk isn't obvious and actual deadlocks h...
Opengraph URL: https://github.com/python/cpython/issues/144513
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve critical sections and stop-the-world interoperability","articleBody":"# Feature or enhancement\n\nCurrently, it's not safe to use critical sections within stop-the-world (STW) pauses because it risks deadlock. The deadlock risk isn't obvious and actual deadlocks happens infrequently, which makes our unit tests less effective at catching this sort of bug. \n\n### Deadlock\n\nNormally, when a thread pauses for a stop-the-world event, it releases all the critical sections that it holds when the thread state is detached (`_PyThreadState_Detach`). The problem is that when locks are contended for a certain duration (`TIME_TO_BE_FAIR_NS`), ownership is handed off directly to a waiting thread rather than being released. This can happen *after* the waiting thread detaches and reaches a safe point for a stop-the-world event.\n\n### Proposal\n\nThe slow paths `_PyCriticalSection_BeginSlow` and `_PyCriticalSection2_BeginSlow` should check if the interpreter is in a stop-the-world pause. If so, we should return without acquiring the lock like we do in the \"optimisation for locking the same object recursively\".\n\nI think we only care about per-interpreter stop-the-world pauses here. We only use cross-interpreter STW pauses (`_PyEval_StopTheWorldAll`) in a few places, like before `os.fork()`.\n\ncc @mpage @Yhg1s \n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-144524\n* gh-145570\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2026-02-05T16:35:33Z","url":"https://github.com/python/cpython/issues/144513"}
| 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:20424f30-582d-f5d2-0125-1c2a8ecabade |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9642:317608:3CCD9AA:4CF699A:6B02029A |
| html-safe-nonce | 953eb80c19c2b36124ab57531f2b8dc69c06c1de047dcfb955529616f5a677b8 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NjQyOjMxNzYwODozQ0NEOUFBOjRDRjY5OUE6NkIwMjAyOUEiLCJ2aXNpdG9yX2lkIjoiNDYwNTA4OTM2Mjc3Mjg4NjE3MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 965c2e0b1ab0567d16f17da931653e1ce673980478ae762bb6cf2a0d86b50ae6 |
| hovercard-subject-tag | issue:3902493264 |
| 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/144513/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3a99351180a8f50e3cd99479851e06ef3915b4e118eefaf4fa8fdbb61810f756/python/cpython/issues/144513 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3a99351180a8f50e3cd99479851e06ef3915b4e118eefaf4fa8fdbb61810f756/python/cpython/issues/144513 |
| og:image:alt | Feature or enhancement Currently, it's not safe to use critical sections within stop-the-world (STW) pauses because it risks deadlock. The deadlock risk isn't obvious and actual deadlocks happens i... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | colesbury |
| 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