Skip to content

Commit 684f232

Browse files
committed
gh-154523: Fix data-race in TextIOWrapper.detach()
1 parent 66e313f commit 684f232

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

Lib/test/test_free_threading/test_io.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,20 @@ def reset_worker():
204204
decoder.reset()
205205

206206
run_concurrently([decode_worker] * 2 + [reset_worker] * 2)
207+
208+
209+
class TextIOWrapperTest(TestCase):
210+
def test_buffer_detach_race(self):
211+
make = lambda: io.TextIOWrapper(io.BytesIO())
212+
slot = [make()]
213+
214+
def reader():
215+
for _ in range(1000):
216+
slot[0].buffer
217+
218+
def detacher():
219+
for _ in range(1000):
220+
slot[0] = make()
221+
slot[0].detach()
222+
223+
run_concurrently([reader, detacher])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed data-race when calling :meth:`io.TextIOWrapper.detach` in
2+
:term:`free-threaded build`.

Modules/_io/textio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,7 @@ _io_TextIOWrapper_detach_impl(textio *self)
16391639
if (buffer == NULL) {
16401640
return NULL;
16411641
}
1642-
self->buffer = NULL;
1642+
FT_ATOMIC_STORE_PTR_RELAXED(self->buffer, NULL);
16431643
self->detached = 1;
16441644
return buffer;
16451645
}

0 commit comments

Comments
 (0)