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
Domain: github.com
{"@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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bf5a82d1-21c6-a51e-127b-9f5318e3ef5f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D840:3F3AF6:4C534D2:69FA5A6:6A50E74B |
| html-safe-nonce | 82cb6e12cccc607a3c8aa8390a1629f6924eebf5e70aa6786f927f02e6270186 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEODQwOjNGM0FGNjo0QzUzNEQyOjY5RkE1QTY6NkE1MEU3NEIiLCJ2aXNpdG9yX2lkIjoiMzg2NjExNjIzOTIwNzIyOTI1OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 73889383b3a002aaca10cd5a10a174f1b8ecfdb5d172a979af5b20dbf5d84061 |
| hovercard-subject-tag | issue:3169847353 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/135871/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f0d40ebc5d2b8c1cdb679e82ef305494502ae4aba511e1391955756b0bb3e518/python/cpython/issues/135871 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f0d40ebc5d2b8c1cdb679e82ef305494502ae4aba511e1391955756b0bb3e518/python/cpython/issues/135871 |
| og:image:alt | Bug 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:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jtibbertsma |
| hostname | github.com |
| expected-hostname | github.com |
| None | 72b13e0e8d0319acdd27a145cfe73f4f06c791713d0ac4690e41fb68ad28cac0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | fb19d617b534959801b4b7453938ecf1dfa22131 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width