Title: Data race: `TextIOWrapper.detach()` stores `self->buffer = NULL` non-atomically · Issue #154523 · python/cpython · GitHub
Open Graph Title: Data race: `TextIOWrapper.detach()` stores `self->buffer = NULL` non-atomically · Issue #154523 · python/cpython
X Title: Data race: `TextIOWrapper.detach()` stores `self->buffer = NULL` non-atomically · Issue #154523 · python/cpython
Description: Bug report Bug description: In the free-threaded build, TextIOWrapper.detach() clears the self->buffer slot with a plain store, while reading tw.buffer goes through a LOAD_ATTR_SLOT specialization that reads the same slot with FT_ATOMIC_...
Open Graph Description: Bug report Bug description: In the free-threaded build, TextIOWrapper.detach() clears the self->buffer slot with a plain store, while reading tw.buffer goes through a LOAD_ATTR_SLOT specialization ...
X Description: Bug report Bug description: In the free-threaded build, TextIOWrapper.detach() clears the self->buffer slot with a plain store, while reading tw.buffer goes through a LOAD_ATTR_SLOT specializati...
Opengraph URL: https://github.com/python/cpython/issues/154523
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Data race: `TextIOWrapper.detach()` stores `self->buffer = NULL` non-atomically","articleBody":"# Bug report\n\n### Bug description:\n\nIn the free-threaded build, `TextIOWrapper.detach()` clears the `self->buffer` slot with a plain store, while reading `tw.buffer` goes through a `LOAD_ATTR_SLOT` specialization that reads the same slot with `FT_ATOMIC_LOAD_PTR`.\n\nWriter — `_io_TextIOWrapper_detach_impl`:\nhttps://github.com/python/cpython/blob/a2a846678e54b1b5defdccd451c573679094ff02/Modules/_io/textio.c#L1630-L1645\n\nReader — `LOAD_ATTR_SLOT` reads the slot atomically:\nhttps://github.com/python/cpython/blob/a2a846678e54b1b5defdccd451c573679094ff02/Python/generated_cases.c.h#L9493-L9503\n\nSo `tw.buffer` racing `tw.detach()` is a plain-store-vs-atomic-load data race on the `buffer` slot. `detach()` should store the slot atomically.\n\nReproducer:\n```python\nimport io\nfrom threading import Thread\n\ndef make():\n return io.TextIOWrapper(io.BytesIO(b'x' * 16))\n\nslot = [make()]\n\ndef reader():\n for _ in range(20000):\n try:\n _ = slot[0].buffer\n except Exception:\n pass\n\ndef detacher():\n for _ in range(20000):\n try:\n slot[0].detach()\n except Exception:\n pass\n slot[0] = make()\n\nthreads = [Thread(target=reader) for _ in range(6)]\nthreads += [Thread(target=detacher) for _ in range(2)]\nfor t in threads: t.start()\nfor t in threads: t.join()\n```\n\nTSAN Report:\n```\nWARNING: ThreadSanitizer: data race (pid=1522499)\n Write of size 8 at 0x7fffd0100110 by thread T7:\n #0 _io_TextIOWrapper_detach_impl /cpython/./Modules/_io/textio.c:1642:18\n #1 _io_TextIOWrapper_detach /cpython/./Modules/_io/clinic/textio.c.h:793:20\n #2 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:4330:35\n\n Previous atomic read of size 8 at 0x7fffd0100110 by thread T6:\n #0 _Py_atomic_load_ptr /cpython/./Include/cpython/pyatomic_gcc.h:300:18 \n #1 _PyEval_EvalFrameDefault /cpython/Python/generated_cases.c.h:9588:36\n #2 _PyEval_EvalFrame /cpython/./Include/internal/pycore_ceval.h:122:16 \n\nSUMMARY: ThreadSanitizer: data race /cpython/./Modules/_io/textio.c:1642:18 in _io_TextIOWrapper_detach_impl\n==================\n```\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nmacOS, Linux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154565\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/Naserume","@type":"Person","name":"Naserume"},"datePublished":"2026-07-23T05:05:25Z","url":"https://github.com/python/cpython/issues/154523"}
| 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:d9b186f4-8009-6abb-f828-425757c63d3f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8A8:38E318:2FE3DFF:3CABBE9:6B01FA43 |
| html-safe-nonce | 26c2743238cde8b663213acfb51cca21110d69952a9d106254e034b2c60e995d |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOEE4OjM4RTMxODoyRkUzREZGOjNDQUJCRTk6NkIwMUZBNDMiLCJ2aXNpdG9yX2lkIjoiNzUwMDkwOTgyMDE2MjIxMDM3MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 959cb87d85caba67263e63969977bb3bc04580a12881abfd0af1b2bad90f531d |
| hovercard-subject-tag | issue:4955635627 |
| 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/154523/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6ab032ebc7b20ffe8227b0fb26e12162e0c71e226e46e70c46c61328870d9f46/python/cpython/issues/154523 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6ab032ebc7b20ffe8227b0fb26e12162e0c71e226e46e70c46c61328870d9f46/python/cpython/issues/154523 |
| og:image:alt | Bug report Bug description: In the free-threaded build, TextIOWrapper.detach() clears the self->buffer slot with a plain store, while reading tw.buffer goes through a LOAD_ATTR_SLOT specialization ... |
| 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