Skip to content

[3.14] gh-134908: Protect textiowrapper_iternext with critical section (gh-134910) #135039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Lib/test/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,37 @@ def flush(self):
# Silence destructor error
R.flush = lambda self: None

@threading_helper.requires_working_threading()
def test_write_readline_races(self):
# gh-134908: Concurrent iteration over a file caused races
thread_count = 2
write_count = 100
read_count = 100

def writer(file, barrier):
barrier.wait()
for _ in range(write_count):
file.write("x")

def reader(file, barrier):
barrier.wait()
for _ in range(read_count):
for line in file:
self.assertEqual(line, "")

with self.open(os_helper.TESTFN, "w+") as f:
barrier = threading.Barrier(thread_count + 1)
reader = threading.Thread(target=reader, args=(f, barrier))
writers = [threading.Thread(target=writer, args=(f, barrier))
for _ in range(thread_count)]
with threading_helper.catch_threading_exception() as cm:
with threading_helper.start_threads(writers + [reader]):
pass
self.assertIsNone(cm.exc_type)

self.assertEqual(os.stat(os_helper.TESTFN).st_size,
write_count * thread_count)


class CIOTest(IOTest):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash when iterating over lines in a text file on the :term:`free threaded <free threading>` build.
15 changes: 14 additions & 1 deletion Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,8 @@ _io_TextIOWrapper_detach_impl(textio *self)
static int
_textiowrapper_writeflush(textio *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);

if (self->pending_bytes == NULL)
return 0;

Expand Down Expand Up @@ -3173,8 +3175,9 @@ _io_TextIOWrapper_close_impl(textio *self)
}

static PyObject *
textiowrapper_iternext(PyObject *op)
textiowrapper_iternext_lock_held(PyObject *op)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
PyObject *line;
textio *self = textio_CAST(op);

Expand Down Expand Up @@ -3210,6 +3213,16 @@ textiowrapper_iternext(PyObject *op)
return line;
}

static PyObject *
textiowrapper_iternext(PyObject *op)
{
PyObject *result;
Py_BEGIN_CRITICAL_SECTION(op);
result = textiowrapper_iternext_lock_held(op);
Py_END_CRITICAL_SECTION();
return result;
}

/*[clinic input]
@critical_section
@getter
Expand Down
Loading