From 87a7a38450dc616e054ff265ec682d762194e256 Mon Sep 17 00:00:00 2001 From: Mohammed Talha Date: Fri, 4 Dec 2026 00:37:05 +0530 Subject: [PATCH 1/2] gh-162421: Stop the PyMutex spin when yields are slow On the free-threaded build, a contended _PyMutex_LockTimed() calls _Py_yield() up to 40 times before parking, without reloading the lock. On an oversubscribed machine each sched_yield() can give the CPU away for a whole time slice, so a lock hand-off can take hundreds of milliseconds. Time the spin: park once it has lasted longer than 100 us, or at once when a single yield took longer than 500 us, and then skip the spin in this thread for the next 10 ms. On an idle machine the limits are never reached, so the spin is unchanged. The new code only shortens the spin; the lock word, parking and hand-off logic are untouched. Co-Authored-By: Claude Opus 5.5 --- ...-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst | 4 ++ Python/lock.c | 44 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst new file mode 100644 index 000000000000000..857c124ab066d13 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-12-03-12-00-00.gh-issue-162421.kQ7mZp.rst @@ -0,0 +1,4 @@ +On the :term:`free-threaded build`, a thread waiting for a contended +:c:type:`PyMutex` now stops spinning and parks once the spin takes longer than +expected, for example because the machine has more runnable threads than CPUs. +This greatly reduces lock hand-off latency on oversubscribed machines. diff --git a/Python/lock.c b/Python/lock.c index 59dfe772a0365f1..50d76c04c2279b7 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -31,6 +31,24 @@ static const int MAX_SPIN_COUNT = 40; static const int MAX_SPIN_COUNT = 0; #endif +#ifdef Py_GIL_DISABLED +// The spin assumes that _Py_yield() returns almost immediately. That holds on +// an idle machine, but on an oversubscribed one each yield can give the CPU +// to another runnable thread for a whole scheduler time slice. These limits +// only ever shorten the spin; they never change how the lock is acquired. +// +// - A spin that has lasted longer than MAX_SPIN_NS parks. +// - A single yield that lasted longer than SLOW_YIELD_NS means the CPU was +// given away. The thread parks at once and does not spin again for the +// next SPIN_DISABLE_NS. +static const PyTime_t MAX_SPIN_NS = 100*1000; +static const PyTime_t SLOW_YIELD_NS = 500*1000; +static const PyTime_t SPIN_DISABLE_NS = 10*1000*1000; + +// Monotonic time before which this thread parks without spinning. +static _Py_thread_local PyTime_t spin_disabled_until = 0; +#endif + struct mutex_entry { // The time after which the unlocking thread should hand off lock ownership // directly to the waiting thread. Written by the waiting thread. @@ -79,6 +97,15 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) }; Py_ssize_t spin_count = 0; +#ifdef Py_GIL_DISABLED + // Time the spin started, and time of the last clock read during the + // spin (0 if unknown, e.g. right after parking). + PyTime_t spin_start = now; + PyTime_t last_read = now; + if (now < spin_disabled_until) { + spin_count = MAX_SPIN_COUNT; + } +#endif for (;;) { if ((v & _Py_LOCKED) == 0) { // The lock is unlocked. Try to grab it. @@ -92,6 +119,19 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) // Spin for a bit. _Py_yield(); spin_count++; +#ifdef Py_GIL_DISABLED + PyTime_t yielded; + // silently ignore error: a clock of 0 leaves the spin unchanged + (void)PyTime_MonotonicRaw(&yielded); + if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) { + spin_disabled_until = _PyTime_Add(yielded, SPIN_DISABLE_NS); + spin_count = MAX_SPIN_COUNT; + } + else if (yielded - spin_start > MAX_SPIN_NS) { + spin_count = MAX_SPIN_COUNT; + } + last_read = yielded; +#endif continue; } @@ -122,6 +162,10 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) int ret = _PyParkingLot_Park(&m->_bits, &newv, sizeof(newv), timeout, &entry, (flags & _PY_LOCK_DETACH) != 0); +#ifdef Py_GIL_DISABLED + // Time spent parked is not a slow yield. + last_read = 0; +#endif if (ret == Py_PARK_OK) { if (entry.handed_off) { // We own the lock now. thread.Lock allows other threads From 7ddd6e2fbfa81d9b96e7610831e32e9d9803c07b Mon Sep 17 00:00:00 2001 From: Mohammed Talha Date: Fri, 4 Dec 2026 01:03:08 +0530 Subject: [PATCH 2/2] gh-162421: Keep the full spin if the clock failed at the start If PyTime_MonotonicRaw() failed when the wait started, spin_start is 0 and the first successful read made the spin look longer than MAX_SPIN_NS, ending it after one yield. Skip the new limits in that case so the spin stays as it was before them. Co-Authored-By: Claude Opus 5.5 --- Python/lock.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Python/lock.c b/Python/lock.c index 50d76c04c2279b7..46915975df7538e 100644 --- a/Python/lock.c +++ b/Python/lock.c @@ -120,17 +120,22 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags) _Py_yield(); spin_count++; #ifdef Py_GIL_DISABLED - PyTime_t yielded; - // silently ignore error: a clock of 0 leaves the spin unchanged - (void)PyTime_MonotonicRaw(&yielded); - if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) { - spin_disabled_until = _PyTime_Add(yielded, SPIN_DISABLE_NS); - spin_count = MAX_SPIN_COUNT; + // If the clock failed when the wait started (spin_start is 0), + // keep the full spin, as before these limits existed. + if (spin_start != 0) { + PyTime_t yielded; + // silently ignore error: a clock of 0 shortens nothing + (void)PyTime_MonotonicRaw(&yielded); + if (last_read != 0 && yielded - last_read > SLOW_YIELD_NS) { + spin_disabled_until = _PyTime_Add(yielded, + SPIN_DISABLE_NS); + spin_count = MAX_SPIN_COUNT; + } + else if (yielded - spin_start > MAX_SPIN_NS) { + spin_count = MAX_SPIN_COUNT; + } + last_read = yielded; } - else if (yielded - spin_start > MAX_SPIN_NS) { - spin_count = MAX_SPIN_COUNT; - } - last_read = yielded; #endif continue; }