From 9e7f901f985dc9dac63a3d83b3cdd04fd9a1f6c9 Mon Sep 17 00:00:00 2001 From: Maik Schulze Date: Mon, 16 Nov 2026 06:49:44 +0100 Subject: [PATCH] gh-162087: Unlock the first mutex when a two-mutex critical section is skipped during stop-the-world _PyCriticalSection2_BeginSlow() returns early while the world is stopped and records no mutexes, so _PyCriticalSection2_End() unlocks nothing. The inline fast path has already locked the first mutex when it calls the slow path with is_m1_locked set, and that mutex stayed locked after the section. Unlock it in the early return, and add a test. --- ...-11-16-09-00-00.gh-issue-162087.c2StwL.rst | 4 ++ .../test_critical_sections.c | 39 +++++++++++++++++++ Python/critical_section.c | 4 ++ 3 files changed, 47 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-16-09-00-00.gh-issue-162087.c2StwL.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-16-09-00-00.gh-issue-162087.c2StwL.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-16-09-00-00.gh-issue-162087.c2StwL.rst new file mode 100644 index 000000000000000..b40abf999620f06 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-16-09-00-00.gh-issue-162087.c2StwL.rst @@ -0,0 +1,4 @@ +Fix a lock that could stay locked in the :term:`free-threaded build`: when a +critical section on two objects began during a stop-the-world pause and the +second object's lock was busy, the first object's lock was not released at the +end of the section. diff --git a/Modules/_testinternalcapi/test_critical_sections.c b/Modules/_testinternalcapi/test_critical_sections.c index 72a1fa2cdc7224d..884b13ea82c6bed 100644 --- a/Modules/_testinternalcapi/test_critical_sections.c +++ b/Modules/_testinternalcapi/test_critical_sections.c @@ -464,7 +464,46 @@ test_critical_sections_stw(PyObject *self, PyObject *Py_UNUSED(args)) #endif // Py_CAN_START_THREADS +#ifdef Py_GIL_DISABLED +static PyObject * +test_critical_section2_stw_first_lock(PyObject *self, PyObject *Py_UNUSED(args)) +{ + // gh-144513 made the slow paths skip locking while the world is stopped. + // The inline fast path of a two-object critical section has already + // locked the first mutex when it calls the slow path for the second one. + PyObject *d1 = PyDict_New(); + PyObject *d2 = PyDict_New(); + if (d1 == NULL || d2 == NULL) { + Py_XDECREF(d1); + Py_XDECREF(d2); + return NULL; + } + PyObject *lo = (uintptr_t)d1 < (uintptr_t)d2 ? d1 : d2; // locked first + PyObject *hi = (lo == d1) ? d2 : d1; + + // Stands for a mutex that was handed off to a thread which is suspended. + PyMutex_Lock(&hi->ob_mutex); + + PyInterpreterState *interp = PyInterpreterState_Get(); + _PyEval_StopTheWorld(interp); + Py_BEGIN_CRITICAL_SECTION2(lo, hi); + Py_END_CRITICAL_SECTION2(); + _PyEval_StartTheWorld(interp); + + // The section has ended: its first mutex must not be locked any more. + assert(!PyMutex_IsLocked(&lo->ob_mutex)); + + PyMutex_Unlock(&hi->ob_mutex); + Py_DECREF(d1); + Py_DECREF(d2); + Py_RETURN_NONE; +} +#endif + static PyMethodDef test_methods[] = { +#ifdef Py_GIL_DISABLED + {"test_critical_section2_stw_first_lock", test_critical_section2_stw_first_lock, METH_NOARGS}, +#endif {"test_critical_sections", test_critical_sections, METH_NOARGS}, {"test_critical_sections_nest", test_critical_sections_nest, METH_NOARGS}, {"test_critical_sections_suspend", test_critical_sections_suspend, METH_NOARGS}, diff --git a/Python/critical_section.c b/Python/critical_section.c index dbee6f236a73bea..15cf315224de8d1 100644 --- a/Python/critical_section.c +++ b/Python/critical_section.c @@ -67,6 +67,10 @@ _PyCriticalSection2_BeginSlow(PyThreadState *tstate, PyCriticalSection2 *c, PyMu { #ifdef Py_GIL_DISABLED if (tstate->interp->stoptheworld.world_stopped) { + if (is_m1_locked) { + // The inline fast path locked m1 for this section, which we skip. + PyMutex_Unlock(m1); + } c->_cs_base._cs_mutex = NULL; c->_cs_mutex2 = NULL; c->_cs_base._cs_prev = 0;