From 684f2327b000a753cc6266c0ea418d7d0f3be4eb Mon Sep 17 00:00:00 2001 From: Brij <97006829+brijkapadia@users.noreply.github.com> Date: Thu, 23 Jul 2026 18:53:52 -0400 Subject: [PATCH 1/2] gh-154523: Fix data-race in `TextIOWrapper.detach()` --- Lib/test/test_free_threading/test_io.py | 17 +++++++++++++++++ ...26-07-23-18-53-48.gh-issue-154523.XUSRBO.rst | 2 ++ Modules/_io/textio.c | 2 +- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst diff --git a/Lib/test/test_free_threading/test_io.py b/Lib/test/test_free_threading/test_io.py index e0bd7e211e73024..8214ae330e25fe5 100644 --- a/Lib/test/test_free_threading/test_io.py +++ b/Lib/test/test_free_threading/test_io.py @@ -204,3 +204,20 @@ def reset_worker(): decoder.reset() run_concurrently([decode_worker] * 2 + [reset_worker] * 2) + + +class TextIOWrapperTest(TestCase): + def test_buffer_detach_race(self): + make = lambda: io.TextIOWrapper(io.BytesIO()) + slot = [make()] + + def reader(): + for _ in range(1000): + slot[0].buffer + + def detacher(): + for _ in range(1000): + slot[0] = make() + slot[0].detach() + + run_concurrently([reader, detacher]) diff --git a/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst b/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst new file mode 100644 index 000000000000000..016505cf08e3dd3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst @@ -0,0 +1,2 @@ +Fixed data-race when calling :meth:`io.TextIOWrapper.detach` in +:term:`free-threaded build`. diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index 5b2a20a30c28cb2..135904c3ab34b95 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -1639,7 +1639,7 @@ _io_TextIOWrapper_detach_impl(textio *self) if (buffer == NULL) { return NULL; } - self->buffer = NULL; + FT_ATOMIC_STORE_PTR_RELAXED(self->buffer, NULL); self->detached = 1; return buffer; } From 828618fdbbe8591fc29d47c008ddd62058e81c9b Mon Sep 17 00:00:00 2001 From: Brij <97006829+brijkapadia@users.noreply.github.com> Date: Thu, 23 Jul 2026 19:03:09 -0400 Subject: [PATCH 2/2] Fix news --- .../next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst b/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst index 016505cf08e3dd3..23b25a26effb642 100644 --- a/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst +++ b/Misc/NEWS.d/next/Library/2026-07-23-18-53-48.gh-issue-154523.XUSRBO.rst @@ -1,2 +1,2 @@ -Fixed data-race when calling :meth:`io.TextIOWrapper.detach` in +Fixed data-race when calling :meth:`io.TextIOBase.detach` in :term:`free-threaded build`.