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

The `textiowrapper_iternext` function called `_textiowrapper_writeflush`, but did not
use a critical section, making it racy in free-threaded builds.
(cherry picked from commit 44fb7c361c)

Co-authored-by: Duane Griffin <duaneg@dghda.com>
This commit is contained in:
Miss Islington (bot) 2025-06-02 21:16:54 +02:00 committed by GitHub
parent 7ac461883d
commit 428b0ca114
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 46 additions and 1 deletions

View file

@ -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;
@ -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);
@ -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