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
Changes from 1 commit
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
Next Next commit
Fix thread safety in StringIO.
  • Loading branch information
ZeroIntensity committed Jun 11, 2025
commit 5f3825b8baface39cb40038a0b81f13fa6d6601b
18 changes: 17 additions & 1 deletion Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static int _io_StringIO___init__(PyObject *self, PyObject *args, PyObject *kwarg
static int
resize_buffer(stringio *self, size_t size)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
/* Here, unsigned types are used to avoid dealing with signed integer
overflow, which is undefined in C. */
size_t alloc = self->buf_size;
Expand Down Expand Up @@ -131,6 +132,7 @@ resize_buffer(stringio *self, size_t size)
static PyObject *
make_intermediate(stringio *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
PyObject *intermediate = PyUnicodeWriter_Finish(self->writer);
self->writer = NULL;
self->state = STATE_REALIZED;
Expand All @@ -153,6 +155,7 @@ make_intermediate(stringio *self)
static int
realize(stringio *self)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
Py_ssize_t len;
PyObject *intermediate;

Expand Down Expand Up @@ -188,6 +191,7 @@ realize(stringio *self)
static Py_ssize_t
write_str(stringio *self, PyObject *obj)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
Py_ssize_t len;
PyObject *decoded = NULL;

Expand Down Expand Up @@ -355,6 +359,7 @@ _io_StringIO_read_impl(stringio *self, Py_ssize_t size)
static PyObject *
_stringio_readline(stringio *self, Py_ssize_t limit)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self);
Py_UCS4 *start, *end, old_char;
Py_ssize_t len, consumed;

Expand Down Expand Up @@ -404,8 +409,9 @@ _io_StringIO_readline_impl(stringio *self, Py_ssize_t size)
}

static PyObject *
stringio_iternext(PyObject *op)
stringio_iternext_lock_held(PyObject *op)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op);
PyObject *line;
stringio *self = stringio_CAST(op);

Expand Down Expand Up @@ -441,6 +447,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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy