Title: `intern_common` should use `_Py_LOCK_DONT_DETACH` · Issue #149162 · python/cpython · GitHub
Open Graph Title: `intern_common` should use `_Py_LOCK_DONT_DETACH` · Issue #149162 · python/cpython
X Title: `intern_common` should use `_Py_LOCK_DONT_DETACH` · Issue #149162 · python/cpython
Description: Bug report PyUnicode_InternFromString and various other interning functions call intern_common, which locks an interpreter-wide lock in the free threading build. We should use _Py_LOCK_DONT_DETACH when locking that lock to avoid potentia...
Open Graph Description: Bug report PyUnicode_InternFromString and various other interning functions call intern_common, which locks an interpreter-wide lock in the free threading build. We should use _Py_LOCK_DONT_DETACH ...
X Description: Bug report PyUnicode_InternFromString and various other interning functions call intern_common, which locks an interpreter-wide lock in the free threading build. We should use _Py_LOCK_DONT_DETACH ...
Opengraph URL: https://github.com/python/cpython/issues/149162
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`intern_common` should use `_Py_LOCK_DONT_DETACH`","articleBody":"# Bug report\n\n`PyUnicode_InternFromString` and various other interning functions call `intern_common`, which locks an interpreter-wide lock in the free threading build. We should use `_Py_LOCK_DONT_DETACH` when locking that lock to avoid potential deadlocks in certain extensions.\n\nPyTorch, TensorFlow, and possibly other extensions use C++11 one time initialization via function local statics to intern string. For example:\n\n```c++\nvoid PyNode::compiled_args(CompiledNodeArgs& args) const {\n static PyObject* method_name = PyUnicode_InternFromString(\"_compiled_autograd_key\");\n ...\n}\n```\n\nhttps://github.com/pytorch/pytorch/blob/8804b122355d1aa1cc6bfffd1516f3df39f96c5f/torch/csrc/autograd/python_function.cpp#L369-L370\n\nC++ static locals implement thread-safe one time initialization, similar to `std::call_once` or `pthread_once`. That means that a (hidden) C++ lock is held during the call to `PyUnicode_InternFromString`. If we detach, during `PyUnicode_InternFromString`, we risk a lock ordering deadlock.\n\nHere's a complicated scenario that could deadlock:\n\n* Thread 1: requests a stop the world pause\n* Thread 2: holds the interned mutex (causes lock contention)\n* Thread 3: calls `static PyObject* method_name = PyUnicode_InternFromString(...)`. Blocks on interned mutex and **detaches** for stop the world pause\n* Thread 4: calls `static PyObject* method_name = PyUnicode_InternFromString(...)` and blocks on C++ one time initialization lock **without detaching**.\n\nSo there's a deadlock where Thread 4 - (waiting on) -> Thread 3 - (waiting on) -> Thread 1 - (waiting on) -> Thread 4.\n\nThis isn't a problem in GIL-enabled builds because thread 3 won't block during `PyUnicode_InternFromString`.\n\n`_Py_LOCK_DONT_DETACH` prevents this problem because `PyUnicode_InternFromString()` will no longer block on a stop the world if there's contention on the interned mutex.\n\nNote that if callers used something like pybind11's `gil_safe_call_once_and_store` that would also avoid the deadlock.\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-149164\n* gh-153814\n* gh-153815\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2026-04-29T18:37:07Z","url":"https://github.com/python/cpython/issues/149162"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| 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:04838f1e-1aaf-be95-edeb-597e2672660c |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 88D4:350D1F:33F573:45F889:6AECC8CF |
| html-safe-nonce | 576a9fd0d5f978c0caa9edc4640f5b26ac35bd867714279941230b8e48fec824 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4OEQ0OjM1MEQxRjozM0Y1NzM6NDVGODg5OjZBRUNDOENGIiwidmlzaXRvcl9pZCI6IjQzNjQ5NzE2ODcwMTA5NDUyMzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | eb462185e2163e955f555d05669a780609857eb964fbdefd2381a81967d37261 |
| hovercard-subject-tag | issue:4352850873 |
| 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/149162/issue_layout |
| twitter:image | https://opengraph.githubassets.com/994b43fd1224dc83cf41907c5e2c921594b125f98b4f2f07507179e521549983/python/cpython/issues/149162 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/994b43fd1224dc83cf41907c5e2c921594b125f98b4f2f07507179e521549983/python/cpython/issues/149162 |
| og:image:alt | Bug report PyUnicode_InternFromString and various other interning functions call intern_common, which locks an interpreter-wide lock in the free threading build. We should use _Py_LOCK_DONT_DETACH ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | colesbury |
| hostname | github.com |
| expected-hostname | github.com |
| None | dc0b519c5798dc1150b88dc4c06a8b9634d0069218dc04e469970e76aad74016 |
| 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 | db139f0bf57b59bf9a8c4abb0849924d6c6ca559 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width