René's URL Explorer Experiment


Title: Non-blocking `_PyMutex_LockTimed` spins and may fail unnecessarily in no-GIL builds · Issue #135871 · python/cpython · GitHub

Open Graph Title: Non-blocking `_PyMutex_LockTimed` spins and may fail unnecessarily in no-GIL builds · Issue #135871 · python/cpython

X Title: Non-blocking `_PyMutex_LockTimed` spins and may fail unnecessarily in no-GIL builds · Issue #135871 · python/cpython

Description: Bug report Bug description: Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily (no-GIL build) Branch: main (commit 2793b68f758c10fb63b264787f10d46a71fc8086) Build: configured with --disable-gil (MAX_SPIN_COUNT > 0) OS: all ...

Open Graph Description: Bug report Bug description: Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily (no-GIL build) Branch: main (commit 2793b68f758c10fb63b264787f10d46a71fc8086) Build: configured with --d...

X Description: Bug report Bug description: Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily (no-GIL build) Branch: main (commit 2793b68f758c10fb63b264787f10d46a71fc8086) Build: configured with --d...

Opengraph URL: https://github.com/python/cpython/issues/135871

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Non-blocking `_PyMutex_LockTimed` spins and may fail unnecessarily in no-GIL builds","articleBody":"# Bug report\n\n### Bug description:\n\n### Non-blocking `_PyMutex_LockTimed` spins and may fail unnecessarily (no-GIL build)\n\n**Branch:** `main` (commit `2793b68f758c10fb63b264787f10d46a71fc8086`)  \n**Build:** configured with `--disable-gil` (`MAX_SPIN_COUNT \u003e 0`)  \n**OS:** all\n\n---\n\n## Summary\n\n`_PyMutex_LockTimed()` is supposed to return immediately when called with\n`timeout == 0` (non-blocking).  In a no-GIL build it:\n\n1. **Spins for up to `MAX_SPIN_COUNT` yields** before returning;  \n2. Can still return **`PY_LOCK_FAILURE` even though the lock was released** during that spin.\n\nTimed/blocking calls (`timeout \u003e 0`) also waste CPU because the spin loop\nnever reloads the lock word, so they can’t notice an unlock until after the\nmaximum spin count.\n\nThe bug is invisible in GIL builds because `MAX_SPIN_COUNT` is 0.\n\n---\n\n## Root cause\n\n```c\n/* Python/lock.c — excerpts */\n\nif ((v \u0026 _Py_LOCKED) == 0) {\n    if (_Py_atomic_compare_exchange_uint8(\u0026m-\u003e_bits, \u0026v, v | _Py_LOCKED))\n        return PY_LOCK_ACQUIRED;\n}\nelse if (timeout == 0) {            // executes only if FIRST load saw LOCKED\n    return PY_LOCK_FAILURE;         // non-blocking, OK\n}\n\n/* … later … */\nif (!(v \u0026 _Py_HAS_PARKED) \u0026\u0026 spin_count \u003c MAX_SPIN_COUNT) {\n    _Py_yield();\n    spin_count++;\n    continue;                       // BUG: never refreshes v\n}\n```\n\n* If our fast CAS loses a race, the `else if` guard is skipped, so a\n  *non-blocking* call drops into the spin loop.  \n* Inside that loop `v` is never reloaded, so the thread can’t see that the\n  lock became free.  After `MAX_SPIN_COUNT` iterations it falls through to\n  the same `timeout == 0` guard and fails spuriously.\n\n---\n\n## Proposed fix\n\n```c\n/* 1 — reload v each spin */\nif (!(v \u0026 _Py_HAS_PARKED) \u0026\u0026 spin_count \u003c MAX_SPIN_COUNT) {\n    _Py_yield();\n    spin_count++;\n    v = _Py_atomic_load_uint8_relaxed(\u0026m-\u003e_bits);   // ← added\n    continue;\n}\n\n/* 2 — early-out for timeout == 0 */\nif ((v \u0026 _Py_LOCKED) == 0) {\n    if (_Py_atomic_compare_exchange_uint8(\u0026m-\u003e_bits, \u0026v, v | _Py_LOCKED))\n        return PY_LOCK_ACQUIRED;\n}\nif (timeout == 0) {                               // ← moved outside else\n    return PY_LOCK_FAILURE;\n}\n```\n\n### Result\n\n* **Non-blocking calls** now return immediately: success if the CAS wins,\n  failure if it loses – no spinning, no parking.\n* **Timed/blocking calls** still spin for fairness, but they now reload the\n  lock word each iteration, so they acquire promptly once the lock is free.\n\n\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-135872\n* gh-135946\n* gh-135947\n* gh-146064\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/jtibbertsma","@type":"Person","name":"jtibbertsma"},"datePublished":"2025-06-24T00:39:45.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/135871/cpython/issues/135871"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:bf5a82d1-21c6-a51e-127b-9f5318e3ef5f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD840:3F3AF6:4C534D2:69FA5A6:6A50E74B
html-safe-nonce82cb6e12cccc607a3c8aa8390a1629f6924eebf5e70aa6786f927f02e6270186
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODQwOjNGM0FGNjo0QzUzNEQyOjY5RkE1QTY6NkE1MEU3NEIiLCJ2aXNpdG9yX2lkIjoiMzg2NjExNjIzOTIwNzIyOTI1OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac73889383b3a002aaca10cd5a10a174f1b8ecfdb5d172a979af5b20dbf5d84061
hovercard-subject-tagissue:3169847353
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/135871/issue_layout
twitter:imagehttps://opengraph.githubassets.com/f0d40ebc5d2b8c1cdb679e82ef305494502ae4aba511e1391955756b0bb3e518/python/cpython/issues/135871
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/f0d40ebc5d2b8c1cdb679e82ef305494502ae4aba511e1391955756b0bb3e518/python/cpython/issues/135871
og:image:altBug report Bug description: Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily (no-GIL build) Branch: main (commit 2793b68f758c10fb63b264787f10d46a71fc8086) Build: configured with --d...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamejtibbertsma
hostnamegithub.com
expected-hostnamegithub.com
None72b13e0e8d0319acdd27a145cfe73f4f06c791713d0ac4690e41fb68ad28cac0
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasefb19d617b534959801b4b7453938ecf1dfa22131
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/issues/135871#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F135871
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F135871
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/135871
Reloadhttps://github.com/python/cpython/issues/135871
Reloadhttps://github.com/python/cpython/issues/135871
Please reload this pagehttps://github.com/python/cpython/issues/135871
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/issues/135871
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Non-blocking _PyMutex_LockTimed spins and may fail unnecessarily in no-GIL buildshttps://github.com/python/cpython/issues/135871#top
3.13bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.13%22
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15pre-release feature fixes, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/jtibbertsma
jtibbertsmahttps://github.com/jtibbertsma
on Jun 24, 2025https://github.com/python/cpython/issues/135871#issue-3169847353
gh-135871: Fix needless spinning in _PyMutex_LockTimed (timeout==0) #135872https://github.com/python/cpython/pull/135872
[3.14] gh-135871: Fix needless spinning in _PyMutex_LockTimed with zero timeout (gh-135872) #135946https://github.com/python/cpython/pull/135946
[3.13] gh-135871: Fix needless spinning in _PyMutex_LockTimed with zero timeout (gh-135872) #135947https://github.com/python/cpython/pull/135947
gh-135871: Reload lock internal state while spinning in PyMutex_LockTimed #146064https://github.com/python/cpython/pull/146064
3.13bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.13%22
3.14bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.14%22
3.15pre-release feature fixes, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
topic-free-threadinghttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-free-threading%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.