Skip to content

gh-162421: Stop the PyMutex spin when yields are slow - #163103

Closed
White-Reaper0 wants to merge 3 commits into
python:mainfrom
White-Reaper0:gh-162421-mutex-spin-budget
Closed

White-Reaper0 wants to merge 3 commits into
python:mainfrom
White-Reaper0:gh-162421-mutex-spin-budget

Conversation

@White-Reaper0

@White-Reaper0 White-Reaper0 commented Dec 3, 2026 •

Copy link
Copy Markdown

On the free-threaded build, a contended _PyMutex_LockTimed() calls _Py_yield() 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 one lock hand-off can take hundreds of milliseconds.

This keeps the 40 yields (and no reload, per gh-157914) but times them:

  • park once the spin has lasted longer than 100 µs (so any single slow yield ends the spin);
  • if a single yield took longer than 500 µs, also skip the spin in this thread for the next 10 ms.

If the clock fails when the wait starts, the full 40-yield spin is kept. On an idle x86-64 Linux machine neither limit is reached, so the spin is unchanged there; other platforms are not measured yet.

The change only shortens the spin: the lock word, parking, wake-up and hand-off code are untouched. Behaviour can still shift, since waiters park sooner (more parked waiters, more slow-path unlocks and fairness hand-offs). It is a simplified form of the issue's patch D5 (time-based expiry instead of a 256-acquisition counter, one clock read per yield).

Early numbers (WSL2, x86-64, 2 cores beside 6 busy loops, 2–3 runs): Lock ping-pong 225–336 ms → 25–27 µs; gh-157914 shared dict 1.2–1.3 s → 0.34–0.41 s. Idle results within noise. Thread tests pass on a debug build, idle and oversubscribed.

Draft: benchmarks (lockbench, including more threads than cores, PGO, other platforms) are in progress with @Aaquibsz before this is ready for review.

I used an AI assistant (Claude) to help analyse the code and draft the patch; I have reviewed and understand all of it.

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 <noreply@anthropic.com>
@python-cla-bot

python-cla-bot Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app

bedevere-app Bot commented Dec 3, 2026

Copy link
Copy Markdown

Thank you for your interest in contributing to Python. This is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

Contributors are responsible for reviewing and understanding what they submit, checking its claims, and being able to explain changes in their own words. This helps reviewers focus on the contribution and leaves more time to support other contributors.

This contribution appears to have been authored with AI assistance. Please explain your contribution in your own words and describe how you checked its correctness.

Please read our guidelines for using AI tools.

White-Reaper0 and others added 2 commits December 4, 2026 01:03
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 <noreply@anthropic.com>
@White-Reaper0

Copy link
Copy Markdown
Author

On the free-threaded build, when a thread comes across a locked PyMutex it calls sched_yield() 40 times before going to sleep, and it never re-checks the lock in between. On a small machine it takes about 40 µs and helps throughput. But once there are more runnable threads than cores, every one of those yields can hand the CPU away for a full time slice. That's how a single lock hand-off goes on taking hundreds of milliseconds.

We have kept the 40 yields but times them. If spinning goes past 100 µs, the thread just goes to sleep. One yield alone took more than 500 µs, that's a shows that the machine is overloaded, so the thread also stops spinning for the next 10 ms. If the clock fails for some reason, it falls back to the normal 40 yields. The only thing that it changes is when a thread gives up spinning. Taking, releasing, parking and handing off the lock has same code as before.

To check it, I built release, debug and TSan versions in WSL2 and compared each against an unpatched build of the same commit. The threading, thread, queue, C-API and free-threading tests pass, both on an idle machine and squeezed onto 2 cores next to 6 busy loops. Under that load the unpatched build actually fails test_threading. TSan didn't report anything (I ran it on the first commit). The Lock ping-pong from the issue went from roughly 230–330 ms down to about 25 µs under load, with no change when idle.

That's only a handful of runs on one machine, though. @Aaquibsz is doing the real benchmarking (lockbench, PGO builds, other platforms), and I'll keep this as a draft until we have those numbers.

@picnixz

picnixz commented Dec 3, 2026

Copy link
Copy Markdown
Member

I do not understand why the issue's OP was not given the chance to open a PR. I am closing it.

@picnixz picnixz closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI-needs-explanation AI-assisted work that needs further explanation and follow up from the contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants