Title: Deadlock caused by type_lock_prevent_release() · Issue #155400 · python/cpython · GitHub
Open Graph Title: Deadlock caused by type_lock_prevent_release() · Issue #155400 · python/cpython
X Title: Deadlock caused by type_lock_prevent_release() · Issue #155400 · python/cpython
Description: Bug report Bug description: The current implementation of type_lock_prevent_release() has a flaw. If the critical section holds both TYPE_LOCK and the type dict mutex (this happens with BEGIN_TYPE_DICT_LOCK), then we can end up deadlocki...
Open Graph Description: Bug report Bug description: The current implementation of type_lock_prevent_release() has a flaw. If the critical section holds both TYPE_LOCK and the type dict mutex (this happens with BEGIN_TYPE_...
X Description: Bug report Bug description: The current implementation of type_lock_prevent_release() has a flaw. If the critical section holds both TYPE_LOCK and the type dict mutex (this happens with BEGIN_TYPE_...
Opengraph URL: https://github.com/python/cpython/issues/155400
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Deadlock caused by type_lock_prevent_release()","articleBody":"# Bug report\n\n### Bug description:\n\nThe current implementation of `type_lock_prevent_release()` has a flaw. If the critical section holds both TYPE_LOCK and the type dict mutex (this happens with BEGIN_TYPE_DICT_LOCK), then we can end up deadlocking. This is because only the type lock is prevented from being released and the dict mutex *is* released. This results in a lock inversion.\n\nThe suggested fix is to prevent release of both held by the top critical section. \n\nReproducer (note it depends on the memory addresses of the mutexes so not 100% reliable).\n```python\ndef test_concurrent_setattr_deadlock():\n # two threads assigning to a special method of the same\n # class could deadlock. One thread held the type lock and waited for\n # the type dict mutex, which its critical section had released when it\n # blocked on the stop-the-world mutex, while the other held the type\n # dict mutex and waited for the type lock.\n class Base:\n pass\n\n N = 2000\n done = False\n\n def setter():\n func = lambda self: \"x\"\n while not done:\n Base.__repr__ = func\n try:\n del Base.__repr__\n except AttributeError:\n pass\n\n def subclasser():\n while not done:\n type('Sub', (Base,), {})()\n\n def lister():\n while not done:\n Base.__subclasses__()\n\n def basesetter():\n nonlocal done\n for i in range(N):\n if i % 100 == 0:\n print(i)\n\n class A:\n pass\n\n class C:\n pass\n\n class B(A):\n pass\n\n B.__bases__ = (C,)\n done = True\n\n # The setter threads are the ones that deadlock. The others are there\n # to keep the type lock and the stop-the-world mutex contended, which\n # is what gets the setters into the window where it happens.\n targets = (\n setter,\n setter,\n subclasser,\n subclasser,\n lister,\n lister,\n basesetter,\n )\n threads = [Thread(target=target) for target in targets]\n for t in threads:\n t.start()\n for t in threads:\n t.join()\n```\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-155401\n* gh-155822\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/nascheme","@type":"Person","name":"nascheme"},"datePublished":"2026-08-08T22:39:22Z","url":"https://github.com/python/cpython/issues/155400"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:ce0926fe-ea87-ce15-35bc-a4b97b585e53 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AC40:2BE220:34613E0:45D851F:6B0202A5 |
| html-safe-nonce | 7c46653c1ca85090a0c81bc7812030cfbbb8b6c81a16d25d1f7279997b28b2b7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzQwOjJCRTIyMDozNDYxM0UwOjQ1RDg1MUY6NkIwMjAyQTUiLCJ2aXNpdG9yX2lkIjoiNTIzNDgxNjY1NTk2MzU4NzIzNyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2bf99049ac9851860c0c255c980b20a67a424f4f951ae159a4fe93cc3841537f |
| hovercard-subject-tag | issue:5099795788 |
| 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/155400/issue_layout |
| twitter:image | https://opengraph.githubassets.com/25572a3eb46f59858be651987d64d64989b72766ec86dd5e94e12caeed453ba0/python/cpython/issues/155400 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/25572a3eb46f59858be651987d64d64989b72766ec86dd5e94e12caeed453ba0/python/cpython/issues/155400 |
| og:image:alt | Bug report Bug description: The current implementation of type_lock_prevent_release() has a flaw. If the critical section holds both TYPE_LOCK and the type dict mutex (this happens with BEGIN_TYPE_... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | nascheme |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width