Skip to content

gh-135410: Use a critical section around StringIO.__next__ #135412

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 3 commits into from
Jun 12, 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
19 changes: 19 additions & 0 deletions Lib/test/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

import unittest
from test import support
from test.support import threading_helper

import gc
import io
import _pyio as pyio
import pickle
import sys
import weakref
import threading

class IntLike:
def __init__(self, num):
Expand Down Expand Up @@ -723,6 +725,22 @@ def test_newline_argument(self):
for newline in (None, "", "\n", "\r", "\r\n"):
self.ioclass(newline=newline)

@unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful under free-threading")
@threading_helper.requires_working_threading()
def test_concurrent_use(self):
memio = self.ioclass("")

def use():
memio.write("x" * 10)
memio.readlines()

threads = [threading.Thread(target=use) for _ in range(8)]
with threading_helper.catch_threading_exception() as cm:
with threading_helper.start_threads(threads):
pass

self.assertIsNone(cm.exc_value)


class PyStringIOTest(MemoryTestMixin, MemorySeekTestMixin,
TextIOTestMixin, unittest.TestCase):
Expand Down Expand Up @@ -890,6 +908,7 @@ def test_setstate(self):
self.assertRaises(ValueError, memio.__setstate__, ("closed", "", 0, None))



class CStringIOPickleTest(PyStringIOPickleTest):
UnsupportedOperation = io.UnsupportedOperation

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash when iterating over :class:`io.StringIO` on the :term:`free
threaded <free threading>` build.
12 changes: 11 additions & 1 deletion Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ _io_StringIO_readline_impl(stringio *self, Py_ssize_t size)
}

static PyObject *
stringio_iternext(PyObject *op)
stringio_iternext_lock_held(PyObject *op)
{
PyObject *line;
stringio *self = stringio_CAST(op);
Expand Down Expand Up @@ -441,6 +441,16 @@ stringio_iternext(PyObject *op)
return line;
}

static PyObject *
stringio_iternext(PyObject *op)
{
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(op);
res = stringio_iternext_lock_held(op);
Py_END_CRITICAL_SECTION();
return res;
}

/*[clinic input]
@critical_section
_io.StringIO.truncate
Expand Down
Loading