Title: Data race on `block->next` in `mi_block_set_nextx` · Issue #129748 · python/cpython · GitHub
Open Graph Title: Data race on `block->next` in `mi_block_set_nextx` · Issue #129748 · python/cpython
X Title: Data race on `block->next` in `mi_block_set_nextx` · Issue #129748 · python/cpython
Description: Bug report I've seen this in non-debug TSan builds. The TSAN report looks like: Write of size 8 at 0x7fffc4043690 by thread T2692: #0 mi_block_set_nextx /raid/sgross/cpython/./Include/internal/mimalloc/mimalloc/internal.h:652:15 (python+...
Open Graph Description: Bug report I've seen this in non-debug TSan builds. The TSAN report looks like: Write of size 8 at 0x7fffc4043690 by thread T2692: #0 mi_block_set_nextx /raid/sgross/cpython/./Include/internal/mima...
X Description: Bug report I've seen this in non-debug TSan builds. The TSAN report looks like: Write of size 8 at 0x7fffc4043690 by thread T2692: #0 mi_block_set_nextx /raid/sgross/cpython/./Include/internal/...
Opengraph URL: https://github.com/python/cpython/issues/129748
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race on `block->next` in `mi_block_set_nextx`","articleBody":"# Bug report\n\nI've seen this in *non-debug* TSan builds. The TSAN report looks like:\n\n```\n Write of size 8 at 0x7fffc4043690 by thread T2692:\n #0 mi_block_set_nextx /raid/sgross/cpython/./Include/internal/mimalloc/mimalloc/internal.h:652:15 (python+0x2ce71e) (BuildId: 2d15b5a5260b454c4f23bd5e53d32d43bfb806c4)\n #1 _mi_free_block_mt /raid/sgross/cpython/Objects/mimalloc/alloc.c:467:9 (python+0x2ce71e)\n #2 _mi_free_block /raid/sgross/cpython/Objects/mimalloc/alloc.c:506:5 (python+0x2a8b9a) (BuildId: 2d15b5a5260b454c4f23bd5e53d32d43bfb806c4)\n #3 _mi_free_generic /raid/sgross/cpython/Objects/mimalloc/alloc.c:524:3 (python+0x2a8b9a)\n #4 mi_free /raid/sgross/cpython/Objects/mimalloc/alloc.c (python+0x2c765b) (BuildId: 2d15b5a5260b454c4f23bd5e53d32d43bfb806c4)\n #5 _PyObject_MiFree /raid/sgross/cpython/Objects/obmalloc.c:284:5 (python+0x2c765b)\n...\n\n Previous atomic read of size 8 at 0x7fffc4043690 by thread T2690:\n #0 _Py_atomic_load_uintptr_relaxed /raid/sgross/cpython/./Include/cpython/pyatomic_gcc.h:375:10 (python+0x4d0341) (BuildId: 2d15b5a5260b454c4f23bd5e53d32d43bfb806c4)\n #1 _Py_IsOwnedByCurrentThread /raid/sgross/cpython/./Include/object.h:252:12 (python+0x4d0341)\n #2 _Py_TryIncrefFast /raid/sgross/cpython/./Include/internal/pycore_object.h:560:9 (python+0x4d0341)\n #3 _Py_TryIncrefCompare /raid/sgross/cpython/./Include/internal/pycore_object.h:599:9 (python+0x4d0341)\n #4 PyMember_GetOne /raid/sgross/cpython/Python/structmember.c:99:18 (python+0x4d0054) (BuildId: 2d15b5a5260b454c4f23bd5e53d32d43bfb806c4)\n #5 member_get /raid/sgross/cpython/Objects/descrobject.c:179:12 (python+0x2056aa) (BuildId: 2d15b5a5260b454c4f23bd5e53d32d43bfb806c4)\n...\n\nSUMMARY: ThreadSanitizer: data race /raid/sgross/cpython/./Include/internal/mimalloc/mimalloc/internal.h:652:15 in mi_block_set_nextx\n```\n\nThis happens when we call `_Py_TryIncrefCompare()` or `_Py_TryXGetRef` or similar on an object that may be concurrently freed. Perhaps surprisingly, this is a supported operation. See https://peps.python.org/pep-0703/#mimalloc-changes-for-optimistic-list-and-dict-access.\n\nThe problem is `mi_block_set_nextx` doesn't use a relaxed store, so this is a data race because the [mimalloc freelist pointer](https://github.com/python/cpython/blob/0d68b14a0d8f493b2f403f64608bcfc055457053/Include/internal/mimalloc/mimalloc/types.h#L237-L239) may overlap the `ob_tid` field. The mimalloc freelist pointer is at the beginning of the freed memory block and `ob_tid` is the first field in `PyObject`.\n\nYou won't see this data race if:\n\n* The object uses `Py_TPFLAGS_MANAGED_DICT`. In that case the beginning the managed dictionary pointer comes before `ob_tid`. That is fine because, unlike `ob_tid`, the managed dictionary pointer is never accessed concurrently with freeing the object.\n* If CPython is built with `--with-pydebug`. The debug allocator sticks two extra words at the beginning of each allocation, so the freelist pointers will overlap with those (this is also fine).\n\nHere are two options:\n\n* Use relaxed stores in mimalloc, such as in `mi_block_set_nextx`. There's about six of these assignments -- not terrible to change -- but I don't love the idea of modifications to mimalloc that don't make sense to upstream, and these only make sense in the context of free threaded CPython.\n* Reorder `PyObject` in the free threading build so that `ob_type` is the first field. This avoids any overlap with `ob_tid`. It's annoying to break ABI or change the PyObject header though.\n \ncc @mpage @Yhg1s \n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-134238\n* gh-134352\n* gh-134353\n* gh-142887\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/colesbury","@type":"Person","name":"colesbury"},"datePublished":"2025-02-06T19:26:20Z","url":"https://github.com/python/cpython/issues/129748"}
| 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:9d9815af-94d8-0a94-6e8c-bcddf7acfeac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CADC:23E48:22CE0:2B30F:6B01F9A8 |
| html-safe-nonce | 0742ae461c82297ac44073560f73196ca7d9d2d19b90ea95e43d3f1dbc066117 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQURDOjIzRTQ4OjIyQ0UwOjJCMzBGOjZCMDFGOUE4IiwidmlzaXRvcl9pZCI6IjIyNjIxMzI3NDk4NTgyMjYzMiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1235d75d87038fbd3c22c7777cb29439b09274194d7d16400eb9f94ff50f9017 |
| hovercard-subject-tag | issue:2836391170 |
| 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/129748/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8aad56befed94adce0a6f16726bf386b5f979590006c8283f3b580ad4fee3177/python/cpython/issues/129748 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8aad56befed94adce0a6f16726bf386b5f979590006c8283f3b580ad4fee3177/python/cpython/issues/129748 |
| og:image:alt | Bug report I've seen this in non-debug TSan builds. The TSAN report looks like: Write of size 8 at 0x7fffc4043690 by thread T2692: #0 mi_block_set_nextx /raid/sgross/cpython/./Include/internal/mima... |
| 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 | 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