Skip to content

Commit b041c7d

Browse files
committed
gh-162087: Record the first mutex instead of unlocking it early
As suggested in review: when the world is stopped and the inline fast path has already locked m1, record m1 in the critical section, so that _PyCriticalSection2_End() unlocks it, instead of unlocking it in _PyCriticalSection2_BeginSlow().
1 parent 9e7f901 commit b041c7d

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

‎Python/critical_section.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,14 @@ _PyCriticalSection2_BeginSlow(PyThreadState *tstate, PyCriticalSection2 *c, PyMu
6868
#ifdef Py_GIL_DISABLED
6969
if (tstate->interp->stoptheworld.world_stopped) {
7070
if (is_m1_locked) {
71-
// The inline fast path locked m1 for this section, which we skip.
72-
PyMutex_Unlock(m1);
71+
// The inline fast path has locked m1: record it in this section,
72+
// so that _PyCriticalSection2_End() unlocks it.
73+
c->_cs_base._cs_mutex = m1;
74+
c->_cs_mutex2 = NULL;
75+
c->_cs_base._cs_prev = tstate->critical_section;
76+
tstate->critical_section =
77+
(uintptr_t)c | _Py_CRITICAL_SECTION_TWO_MUTEXES;
78+
return;
7379
}
7480
c->_cs_base._cs_mutex = NULL;
7581
c->_cs_mutex2 = NULL;

0 commit comments

Comments
 (0)