GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout==0) by jtibbertsma · Pull Request #135872 · python/cpython

Conversation

@jtibbertsma

@jtibbertsma jtibbertsma commented Jun 24, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Move the timeout == 0 guard outside the else so a non-blocking call returns immediately after a failed CAS instead of entering the spin loop.
  • Reload v on every spin iteration, allowing timed/blocking callers to notice an unlock promptly.

No-GIL builds now honor the semantics of non-blocking attempts and avoid wasted CPU; GIL builds are unaffected (MAX_SPIN_COUNT == 0).

  • Issue: Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily in no-GIL builds #135871

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@python-cla-bot

python-cla-bot Bot commented Jun 24, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added the awaiting review label Jun 24, 2025
@bedevere-app

This comment was marked as resolved.

Sign in to view
@bedevere-app bedevere-app Bot mentioned this pull request Jun 24, 2025
Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily in no-GIL builds #135871
Closed
@jtibbertsma
jtibbertsma force-pushed the fix-issue-135871 branch from 94f04f5 to 88e8a1c Compare June 24, 2025 01:00
@bedevere-app

This comment was marked as duplicate.

Sign in to view
@jtibbertsma
jtibbertsma force-pushed the fix-issue-135871 branch from 88e8a1c to 45811fd Compare June 24, 2025 01:01
@bedevere-app

This comment was marked as duplicate.

Sign in to view
@jtibbertsma
pythongh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout…
7296a7d
…==0)

* Move the timeout == 0 guard outside the else so a non-blocking call
  returns immediately after a failed CAS instead of entering the spin loop.
* Reload v on every spin iteration, allowing timed/blocking callers to
  notice an unlock promptly.

No-GIL builds now honor the semantics of non-blocking attempts and avoid
wasted CPU; GIL builds are unaffected (MAX_SPIN_COUNT == 0).
@jtibbertsma
jtibbertsma force-pushed the fix-issue-135871 branch from 45811fd to 7296a7d Compare June 24, 2025 01:09
ZeroIntensity
ZeroIntensity reviewed Jun 24, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't force-push, we squash at the end.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jtibbertsma reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread Misc/NEWS.d/next/Core and Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ZeroIntensity
ZeroIntensity requested review from colesbury and pitrou June 24, 2025 01:47
@ZeroIntensity ZeroIntensity added topic-free-threading needs backport to 3.14 bugs and security fixes labels Jun 24, 2025
jtibbertsma added 2 commits June 23, 2025 18:54
@jtibbertsma
Update news entry
4b5123c
@jtibbertsma
Fix filename
8a4e9ad
@ZeroIntensity
ZeroIntensity removed the request for review from pitrou June 24, 2025 01:58
@ZeroIntensity

ZeroIntensity commented Jun 24, 2025

Copy link
Copy Markdown
Member

(Sorry Antoine, hit the wrong button!)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner reviewed Jun 24, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

colesbury
colesbury reviewed Jun 24, 2025
View reviewed changes
Comment thread Python/lock.c Outdated
// Spin for a bit.
_Py_yield();
spin_count++;
v = _Py_atomic_load_uint8_relaxed(&m->_bits);

@colesbury colesbury Jun 24, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it fixes a logic bug but it also hurts performance

See Tools/lockbench/lockbench.py

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jtibbertsma jtibbertsma Jun 25, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review! I'm convinced, this line reduces throughput on my machine by something like 40% for 8 threads. I think changing else if to if still makes sense though. I updated the PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@jtibbertsma
Keep timeout==0 fast-fail, drop spin-loop state reload
3fb8f37
vstinner
vstinner approved these changes Jun 25, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Jun 25, 2025
@vstinner vstinner added the needs backport to 3.13 only security fixes label Jun 25, 2025
colesbury
colesbury approved these changes Jun 25, 2025
View reviewed changes

@colesbury colesbury left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Let's investigate (and fix) the missing load separately

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issue-135871.50C528.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@colesbury
Update Misc/NEWS.d/next/Core_and_Builtins/2025-06-23-18-08-32.gh-issu…
8cbb992
…e-135871.50C528.rst
@colesbury
colesbury enabled auto-merge (squash) June 25, 2025 16:15
@colesbury
colesbury merged commit cbfaf41 into python:main Jun 25, 2025
@miss-islington-app

miss-islington-app Bot commented Jun 25, 2025

Copy link
Copy Markdown

Thanks @jtibbertsma for the PR, and @colesbury for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Jun 25, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 25, 2025
@jtibbertsma @miss-islington
Fix needless spinning in _PyMutex_LockTimed with zero timeout (pyth…
535b8d6
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <[email protected]>
@miss-islington-app

miss-islington-app Bot commented Jun 25, 2025

Copy link
Copy Markdown

Sorry, @jtibbertsma and @colesbury, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker cbfaf41caf135b8598a560854cd59e992a2ccfed 3.13
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app

bedevere-app Bot commented Jun 25, 2025

Copy link
Copy Markdown

GH-135946 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app miss-islington-app Bot assigned colesbury Jun 25, 2025
@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 25, 2025
@bedevere-app

bedevere-app Bot commented Jun 25, 2025

Copy link
Copy Markdown

GH-135946 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury pushed a commit to colesbury/cpython that referenced this pull request Jun 25, 2025
@jtibbertsma @colesbury
[3.13] Fix needless spinning in _PyMutex_LockTimed with zero timeout (
697bdf5
pythongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <[email protected]>
@bedevere-app

bedevere-app Bot commented Jun 25, 2025

Copy link
Copy Markdown

GH-135947 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Jun 25, 2025
@bedevere-app

bedevere-app Bot commented Jun 25, 2025

Copy link
Copy Markdown

GH-135947 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

colesbury pushed a commit that referenced this pull request Jun 25, 2025
@miss-islington @jtibbertsma
[3.14] gh-135871: Fix needless spinning in _PyMutex_LockTimed with …
80fc62f
…zero timeout (gh-135872) (gh-135946)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <[email protected]>
colesbury added a commit that referenced this pull request Jun 25, 2025
@colesbury @jtibbertsma
[3.13] gh-135871: Fix needless spinning in _PyMutex_LockTimed with …
c64df2a
…zero timeout (gh-135872) (gh-135947)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
(cherry picked from commit cbfaf41)

Co-authored-by: Joseph Tibbertsma <[email protected]>
AndPuQing pushed a commit to AndPuQing/cpython that referenced this pull request Jul 11, 2025
@jtibbertsma @AndPuQing
Fix needless spinning in _PyMutex_LockTimed with zero timeout (pyth…
b7de590
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@jtibbertsma @Pranjal095
Fix needless spinning in _PyMutex_LockTimed with zero timeout (pyth…
716ea1b
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@jtibbertsma @taegyunkim
Fix needless spinning in _PyMutex_LockTimed with zero timeout (pyth…
dcca67b
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
@jtibbertsma @Agent-Hellboy
Fix needless spinning in _PyMutex_LockTimed with zero timeout (pyth…
c24fa3a
…ongh-135872)

The free threading build could spin unnecessarily on `_Py_yield()` if the initial
compare and swap failed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ZeroIntensity ZeroIntensity ZeroIntensity left review comments
@vstinner vstinner vstinner approved these changes
@colesbury colesbury colesbury approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@colesbury colesbury

Labels

topic-free-threading

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@jtibbertsma @ZeroIntensity @vstinner @colesbury
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.