René's URL Explorer Experiment


Title: Free-threaded build: a two-object critical section that begins during a stop-the-world can leave its first mutex locked · Issue #162087 · python/cpython · GitHub

Open Graph Title: Free-threaded build: a two-object critical section that begins during a stop-the-world can leave its first mutex locked · Issue #162087 · python/cpython

X Title: Free-threaded build: a two-object critical section that begins during a stop-the-world can leave its first mutex locked · Issue #162087 · python/cpython

Description: Bug report I was exploring peak RSS gaps of the free-threaded build with mimalloc v3 (#159718) with overnight stress tests and, while looking into lock handoffs, read the fix for #144513 (#144524). One path in it looks like it leaves a m...

Open Graph Description: Bug report I was exploring peak RSS gaps of the free-threaded build with mimalloc v3 (#159718) with overnight stress tests and, while looking into lock handoffs, read the fix for #144513 (#144524)....

X Description: Bug report I was exploring peak RSS gaps of the free-threaded build with mimalloc v3 (#159718) with overnight stress tests and, while looking into lock handoffs, read the fix for #144513 (#144524)....

Opengraph URL: https://github.com/python/cpython/issues/162087

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Free-threaded build: a two-object critical section that begins during a stop-the-world can leave its first mutex locked","articleBody":"### Bug report\n\nI was exploring peak RSS gaps of the free-threaded build with mimalloc v3 (https://github.com/python/cpython/pull/159718) with overnight stress tests and, while looking into lock handoffs, read the fix for https://github.com/python/cpython/issues/144513 (https://github.com/python/cpython/pull/144524). One path in it looks like it leaves a mutex locked. An AI assistant helped with the analysis.\n\n### What happens\n\n[`_PyCriticalSection2_BeginSlow()`](https://github.com/python/cpython/blob/e8c0584877e0/Python/critical_section.c#L65-L75) returns early when the interpreter's world is stopped and records no mutexes, so `_PyCriticalSection2_End()` unlocks nothing. But the inline fast path, [`_PyCriticalSection2_BeginMutex()`](https://github.com/python/cpython/blob/e8c0584877e0/Include/internal/pycore_critical_section.h#L172-L187), has already locked the first mutex when it calls the slow path for the second one (`is_m1_locked == 1`). That first mutex then stays locked after the section has ended, and the next critical section on that object, in any thread, waits forever.\n\nIt needs the second mutex to be busy during a stop-the-world, which is the case https://github.com/python/cpython/issues/144513 is about (a mutex handed off to a thread that is already suspended). I couldn't find code that gets there today: of the stop-the-world regions on main, the ones I traced take a single-object critical section at most (for example `object_set_class_world_stopped()`), and those are fine. So this looks like a trap for the first two-object critical section that runs while the world is stopped, not like something that can be hit now.\n\n### A test that shows it\n\nFor `Modules/_testinternalcapi/test_critical_sections.c` (free-threaded build only); the locked second mutex stands for one that was handed off:\n\n```c\nstatic PyObject *\ntest_critical_section2_stw_first_lock(PyObject *self, PyObject *Py_UNUSED(args))\n{\n    // gh-144513 made the slow paths skip locking while the world is stopped.\n    // The inline fast path of a two-object critical section has already\n    // locked the first mutex when it calls the slow path for the second one.\n    PyObject *d1 = PyDict_New();\n    PyObject *d2 = PyDict_New();\n    if (d1 == NULL || d2 == NULL) {\n        Py_XDECREF(d1);\n        Py_XDECREF(d2);\n        return NULL;\n    }\n    PyObject *lo = (uintptr_t)d1 \u003c (uintptr_t)d2 ? d1 : d2;   // locked first\n    PyObject *hi = (lo == d1) ? d2 : d1;\n\n    // Stands for a mutex that was handed off to a thread which is suspended.\n    PyMutex_Lock(&hi->ob_mutex);\n\n    PyInterpreterState *interp = PyInterpreterState_Get();\n    _PyEval_StopTheWorld(interp);\n    Py_BEGIN_CRITICAL_SECTION2(lo, hi);\n    Py_END_CRITICAL_SECTION2();\n    _PyEval_StartTheWorld(interp);\n\n    // The section has ended: its first mutex must not be locked any more.\n    assert(!PyMutex_IsLocked(&lo->ob_mutex));\n\n    PyMutex_Unlock(&hi->ob_mutex);\n    Py_DECREF(d1);\n    Py_DECREF(d2);\n    Py_RETURN_NONE;\n}\n```\n\n`python -m test test_capi -m test_critical_section2_stw_first_lock`, main at e8c0584877 (2026-11-10), free-threaded:\n\n| | Linux arm64 | macOS arm64 |\n|---|---|---|\n| as it is | aborts on the assertion | aborts on the assertion |\n| with the change below | passes | passes |\n\n### A possible change\n\n```diff\ndiff --git a/Python/critical_section.c b/Python/critical_section.c\nindex dbee6f236a..15cf315224 100644\n--- a/Python/critical_section.c\n+++ b/Python/critical_section.c\n@@ -67,6 +67,10 @@ _PyCriticalSection2_BeginSlow(PyThreadState *tstate, PyCriticalSection2 *c, PyMu\n {\n #ifdef Py_GIL_DISABLED\n     if (tstate->interp->stoptheworld.world_stopped) {\n+        if (is_m1_locked) {\n+            // The inline fast path locked m1 for this section, which we skip.\n+            PyMutex_Unlock(m1);\n+        }\n         c->_cs_base._cs_mutex = NULL;\n         c->_cs_mutex2 = NULL;\n         c->_cs_base._cs_prev = 0;\n```\n\nThis is the only place that needs it: `_PyCriticalSection_BeginSlow()` is never called with a mutex already locked, and the recursion shortcut in `_PyCriticalSection2_BeginSlow()` can't coincide with `is_m1_locked`, since the fast path's `PyMutex_LockFast(m1)` fails on a mutex the thread already holds.\n\nWith the change the full test suite passes on macOS (free-threaded, today's main), and `test_capi`, `test_free_threading`, `test_dict`, `test_set`, `test_list`, `test_gc` and `test_threading` pass on Linux.\n\nI ran main only. The same lines are in 3.15 and in 3.14 since 3.14.4, where the fix was backported.\n\nI'll open a PR with the test and the change.\n\ncc @colesbury\n\n\n### CPython versions tested on:\n\n3.14, 3.15, CPython main branch\n\n### Operating systems tested on:\n\nmacOS, Linux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162088\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/maikschulze","@type":"Person","name":"maikschulze"},"datePublished":"2026-11-16T07:47:30Z","url":"https://github.com/python/cpython/issues/162087"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:31b5affa-49f2-6366-ca46-16a14c62d2a2
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idCFD2:2A7C1C:15DDB6B:1CEC155:6B01FE23
html-safe-nonce053b7601cb472b4cf7ac1aad26beef33456630505df8b64c481093ce5b357a1d
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkQyOjJBN0MxQzoxNUREQjZCOjFDRUMxNTU6NkIwMUZFMjMiLCJ2aXNpdG9yX2lkIjoiNTEwMTE2MzE0MDYzNzM5MjQxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmace51b16cdfbaf67ef439a745bf807d9e10dd666816d1c6c008545f4c59d3c0488
hovercard-subject-tagissue:6416676204
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/162087/issue_layout
twitter:imagehttps://opengraph.githubassets.com/c85325a4ca1f1e92873955ba5bc1d88f83ba50c824232fe035fca61329e89e06/python/cpython/issues/162087
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/c85325a4ca1f1e92873955ba5bc1d88f83ba50c824232fe035fca61329e89e06/python/cpython/issues/162087
og:image:altBug report I was exploring peak RSS gaps of the free-threaded build with mimalloc v3 (#159718) with overnight stress tests and, while looking into lock handoffs, read the fix for #144513 (#144524)....
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamemaikschulze
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/162087#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F162087
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F162087
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/162087
Reloadhttps://github.com/python/cpython/issues/162087
Reloadhttps://github.com/python/cpython/issues/162087
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/162087
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/162087
Free-threaded build: a two-object critical section that begins during a stop-the-world can leave its first mutex lockedhttps://github.com/python/cpython/issues/162087#top
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/maikschulze
maikschulzehttps://github.com/maikschulze
on Nov 16, 2026https://github.com/python/cpython/issues/162087#issue-6416676204
#159718https://github.com/python/cpython/pull/159718
#144513https://github.com/python/cpython/issues/144513
#144524https://github.com/python/cpython/pull/144524
_PyCriticalSection2_BeginSlow()https://github.com/python/cpython/blob/e8c0584877e0/Python/critical_section.c#L65-L75
_PyCriticalSection2_BeginMutex()https://github.com/python/cpython/blob/e8c0584877e0/Include/internal/pycore_critical_section.h#L172-L187
#144513https://github.com/python/cpython/issues/144513
e8c0584https://github.com/python/cpython/commit/e8c0584877e0b0a82ee8e0707336d8df4d68ccb4
@colesburyhttps://github.com/colesbury
gh-162087: Release the first mutex of a two-mutex critical section that begins during stop-the-world #162088https://github.com/python/cpython/pull/162088
maikschulzehttps://github.com/maikschulze
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Nov 16, 2026https://github.com/python/cpython/issues/162087#event-36250605180
aiskhttps://github.com/aisk
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
on Nov 16, 2026https://github.com/python/cpython/issues/162087#event-36250901980
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Nov 16, 2026https://github.com/python/cpython/issues/162087#issuecomment-6917442006
https://github.com/maikschulze
maikschulzehttps://github.com/maikschulze
on Nov 16, 2026https://github.com/python/cpython/issues/162087#issuecomment-6917800982
e8c0584https://github.com/python/cpython/commit/e8c0584877e0b0a82ee8e0707336d8df4d68ccb4
https://github.com/colesbury
colesburyhttps://github.com/colesbury
on Nov 16, 2026https://github.com/python/cpython/issues/162087#issuecomment-6918305317
maikschulzehttps://github.com/maikschulze
on Nov 16, 2026https://github.com/python/cpython/issues/162087#event-36284082559
pythongh-162087: Record the first mutex instead of unlocking it earlyhttps://github.com/maikschulze/cpython/commit/b041c7d0337a650e8c411c2bb04182c2605a11b3
pythongh-162087https://github.com/python/cpython/issues/162087
b041c7dhttps://github.com/maikschulze/cpython/commit/b041c7d0337a650e8c411c2bb04182c2605a11b3
https://github.com/maikschulze
maikschulzehttps://github.com/maikschulze
on Nov 16, 2026https://github.com/python/cpython/issues/162087#issuecomment-6918639667
b041c7dhttps://github.com/python/cpython/commit/b041c7d0337a650e8c411c2bb04182c2605a11b3
#162088https://github.com/python/cpython/pull/162088
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/162087
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/162087
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.