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
Domain: github.com
{"@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-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:31b5affa-49f2-6366-ca46-16a14c62d2a2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CFD2:2A7C1C:15DDB6B:1CEC155:6B01FE23 |
| html-safe-nonce | 053b7601cb472b4cf7ac1aad26beef33456630505df8b64c481093ce5b357a1d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRkQyOjJBN0MxQzoxNUREQjZCOjFDRUMxNTU6NkIwMUZFMjMiLCJ2aXNpdG9yX2lkIjoiNTEwMTE2MzE0MDYzNzM5MjQxOSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e51b16cdfbaf67ef439a745bf807d9e10dd666816d1c6c008545f4c59d3c0488 |
| hovercard-subject-tag | issue:6416676204 |
| 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/162087/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c85325a4ca1f1e92873955ba5bc1d88f83ba50c824232fe035fca61329e89e06/python/cpython/issues/162087 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c85325a4ca1f1e92873955ba5bc1d88f83ba50c824232fe035fca61329e89e06/python/cpython/issues/162087 |
| og:image:alt | 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).... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maikschulze |
| 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