Title: Data races on type slots and `tp_subclasses` between `type_setattro` and `type_new` / `type_dealloc` · Issue #151377 · python/cpython · GitHub
Open Graph Title: Data races on type slots and `tp_subclasses` between `type_setattro` and `type_new` / `type_dealloc` · Issue #151377 · python/cpython
X Title: Data races on type slots and `tp_subclasses` between `type_setattro` and `type_new` / `type_dealloc` · Issue #151377 · python/cpython
Description: Bug report Bug description: On the type_setattro path, update_one_slot first reads the slot in queue_slot_update, and then defers the slot write to a stop-the-world phase. https://github.com/python/cpython/blob/b18168cb32d545ed976b760983...
Open Graph Description: Bug report Bug description: On the type_setattro path, update_one_slot first reads the slot in queue_slot_update, and then defers the slot write to a stop-the-world phase. https://github.com/python...
X Description: Bug report Bug description: On the type_setattro path, update_one_slot first reads the slot in queue_slot_update, and then defers the slot write to a stop-the-world phase. https://github.com/python...
Opengraph URL: https://github.com/python/cpython/issues/151377
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data races on type slots and `tp_subclasses` between `type_setattro` and `type_new` / `type_dealloc`","articleBody":"# Bug report\n\n### Bug description:\n\n\nOn the `type_setattro` path, `update_one_slot` first reads the slot in `queue_slot_update`, and then defers the slot write to a `stop-the-world` phase.\n\nhttps://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5 dde5bdf/Objects/typeobject.c#L3859-L3863\n\nbut on the `type_new` path it writes the slot immediately, with no `stop-the-world`\n\nhttps://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Objects/typeobject.c#L12032-L12041\n\nThe same two paths also race on the base's `tp_subclasses` dict. \n\n`recurse_down_subclasses` iterates it with `PyDict_Next`, \nhttps://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Objects/typeobject.c#L12351-L12366\n\nwhile `add_subclass` inserts and `remove_subclass` deletes with no coordinating lock.\nhttps://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Objects/typeobject.c#L9700-L9710\nhttps://github.com/python/cpython/blob/b18168cb32d545ed976b760983478cbd5dde5bdf/Objects/typeobject.c#L9762-L9772\n\n\nReproducer:\n```python\nimport threading\n\nclass Base:\n pass\n\ndef setter():\n for _ in range(10000):\n Base.__repr__ = lambda self: \"x\"\n\ndef subclasser():\n for _ in range(10000):\n type('Sub', (Base,), {})\n\nthreads = [threading.Thread(target=setter) for _ in range(2)]\nthreads += [threading.Thread(target=subclasser) for _ in range(4)]\nfor t in threads: t.start()\nfor t in threads: t.join()\n```\n\nTSAN Report:\n```\n==================\nWARNING: ThreadSanitizer: data race (pid=1674015)\n Read of size 8 at 0x7fffc2100468 by thread T2:\n #0 queue_slot_update /cpython/Objects/typeobject.c:3858:9 \n #1 update_one_slot /cpython/Objects/typeobject.c:12035:13 \n #2 update_slots_callback /cpython/Objects/typeobject.c:12064:13 \n #3 update_subclasses /cpython/Objects/typeobject.c:12340:9 \n #4 recurse_down_subclasses /cpython/Objects/typeobject.c:12381:13 \n #5 update_subclasses /cpython/Objects/typeobject.c:12343:12\n #6 update_slot /cpython/Objects/typeobject.c:12108:12 \n #7 update_slot_after_setattr /cpython/Objects/typeobject.c:6719:9 \n #8 type_setattro /cpython/Objects/typeobject.c:6820:19 \n #9 PyObject_SetAttr /cpython/Objects/object.c:1533:15 \n #10 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:11536:27 \n...\n\n Previous write of size 8 at 0x7fffc2100468 by thread T4:\n #0 update_one_slot /cpython/Objects/typeobject.c:12040:14 \n #1 fixup_slot_dispatchers /cpython/Objects/typeobject.c:12120:9 \n #2 type_new_impl /cpython/Objects/typeobject.c:4952:5 \n #3 type_new /cpython/Objects/typeobject.c:5100:12 \n #4 type_call /cpython/Objects/typeobject.c:2467:11 \n #5 _PyObject_MakeTpCall /cpython/Objects/call.c:242:18 \n #6 type_vectorcall /cpython/Objects/typeobject.c:5120:12 \n #7 _Py_CallBuiltinClass_StackRef /cpython/Python/ceval.c:899:11 \n #8 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2333:35 \n...\n\nSUMMARY: ThreadSanitizer: data race /cpython/Objects/typeobject.c:3858:9 in queue_slot_update\n==================\n==================\nWARNING: ThreadSanitizer: data race (pid=1674015)\n Atomic write of size 8 at 0x7fffc62210f8 by thread T6:\n #0 delitem_common /cpython/Objects/dictobject.c \n #1 _PyDict_DelItem_KnownHash_LockHeld /cpython/Objects/dictobject.c:2966:5 \n #2 _PyDict_DelItem_KnownHash /cpython/Objects/dictobject.c:2975:11 \n #3 PyDict_DelItem /cpython/Objects/dictobject.c:2934:12 \n #4 remove_subclass /cpython/Objects/typeobject.c:9767:24 \n #5 remove_all_subclasses /cpython/Objects/typeobject.c:9790:13 \n #6 type_dealloc_common /cpython/Objects/typeobject.c:6839:9 \n #7 type_dealloc /cpython/Objects/typeobject.c:6998:5 \n #8 _Py_Dealloc /cpython/Objects/object.c:3312:5 \n #9 _Py_DecRefSharedDebug /cpython/Objects/object.c:426:9 \n #10 _Py_DecRefShared /cpython/Objects/object.c:433:5 \n #11 Py_DECREF /cpython/./Include/refcount.h:385:9 \n #12 Py_XDECREF /cpython/./Include/refcount.h:520:9 \n #13 tuple_dealloc /cpython/Objects/tupleobject.c:277:9 \n #14 _Py_Dealloc /cpython/Objects/object.c:3312:5 \n #15 _Py_DecRefSharedDebug /cpython/Objects/object.c:426:9 \n #16 _Py_DecRefShared /cpython/Objects/object.c:433:5 \n #17 Py_DECREF /cpython/./Include/refcount.h:385:9 \n #18 delete_garbage /cpython/Python/gc_free_threading.c \n #19 gc_collect_internal /cpython/Python/gc_free_threading.c:2176:5 \n #20 gc_collect_main /cpython/Python/gc_free_threading.c:2257:5 \n #21 _Py_RunGC /cpython/Python/gc_free_threading.c:2716:5 \n #22 _Py_HandlePending /cpython/Python/ceval_gil.c:1399:9 \n #23 check_periodics /cpython/Python/ceval_macros.h:524:16 \n #24 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:2367:27 \n...\n\n Previous read of size 8 at 0x7fffc62210f8 by thread T1:\n #0 _PyDict_Next /cpython/Objects/dictobject.c:3163:31 \n #1 PyDict_Next /cpython/Objects/dictobject.c:3198:12 \n #2 recurse_down_subclasses /cpython/Objects/typeobject.c:12361:12 \n #3 update_subclasses /cpython/Objects/typeobject.c:12343:12 \n #4 update_slot /cpython/Objects/typeobject.c:12108:12\n #5 update_slot_after_setattr /cpython/Objects/typeobject.c:6719:9 \n #6 type_setattro /cpython/Objects/typeobject.c:6820:19 \n #7 PyObject_SetAttr /cpython/Objects/object.c:1533:15 \n #8 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:11536:27 \n...\n\nSUMMARY: ThreadSanitizer: data race /cpython/Objects/dictobject.c in delitem_common\n==================\n```\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-151394\n* gh-155370\n* gh-155821\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Naserume","@type":"Person","name":"Naserume"},"datePublished":"2026-06-11T18:11:28Z","url":"https://github.com/python/cpython/issues/151377"}
| 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:46c08acd-87ab-99cc-e408-3a86bda64928 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DFAC:3E4205:3486C60:462BE77:6B020780 |
| html-safe-nonce | feffff0edd6592a7814fc1fa22d3432dbf6f7f8c4b52b8cbd370ad1a83716ab9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERkFDOjNFNDIwNTozNDg2QzYwOjQ2MkJFNzc6NkIwMjA3ODAiLCJ2aXNpdG9yX2lkIjoiODA2OTAzMDk2NTc0NzUxNzMxMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d0ebd27fecee4b24b9bb6447411111e7c880f1ea902f9f1cbf4b2ccaf06006d3 |
| hovercard-subject-tag | issue:4643042645 |
| 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/151377/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7df76f4199f4e9c60c62bce10e4e6e8d0b07a28b24ce11f27a7d0702985a9ea8/python/cpython/issues/151377 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7df76f4199f4e9c60c62bce10e4e6e8d0b07a28b24ce11f27a7d0702985a9ea8/python/cpython/issues/151377 |
| og:image:alt | Bug report Bug description: On the type_setattro path, update_one_slot first reads the slot in queue_slot_update, and then defers the slot write to a stop-the-world phase. https://github.com/python... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Naserume |
| 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