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 less effective at catching this sort of bug.
Deadlock
Normally, 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.
Proposal
The 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".
I 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().
I'm going to backport the PR to 3.14. I'm worried that without it, I'm going to accidentally introduce a 3.14 deadlock by using a critical section during a stop the world pause.
Activity
pythongh-144513: Skip critical section locking during stop-the-world
gh-144513: Skip critical section locking during stop-the-world (gh-14…
kumaraditya303 commented on Feb 6, 2026
@colesbury Should this be backported to 3.14?
colesbury commented on Feb 6, 2026
I wasn't planning to backport it, but I don't feel strongly either way.
pythongh-144513: Skip critical section locking during stop-the-world (p…
colesbury commented on Mar 5, 2026
I'm going to backport the PR to 3.14. I'm worried that without it, I'm going to accidentally introduce a 3.14 deadlock by using a critical section during a stop the world pause.
pythongh-144513: Skip critical section locking during stop-the-world (p…
[3.14] pythongh-144513: Skip critical section locking during stop-the…
[3.14] gh-144513: Skip critical section locking during stop-the-world (…
pythongh-144513: Skip critical section locking during stop-the-world (p…
setup.py: Add explicit warning on lack of support for 3.13 freethreading