]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-143008: Fix race re-initializing TextIOWrapper (GH-151203) (#151226)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 10 Jun 2026 09:41:47 +0000 (11:41 +0200)
committerGitHub <noreply@github.com>
Wed, 10 Jun 2026 09:41:47 +0000 (09:41 +0000)
[3.15] gh-143008: Fix race re-initializing TextIOWrapper (GH-151203)

__init__() changes multiple variables and may be called more than once
from multiple threads.
(cherry picked from commit 0318867acf72e3acf78f480db73a69982573263a)

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Misc/NEWS.d/next/Library/2026-06-04-18-22-56.gh-issue-143008.z5tw-J.rst [new file with mode: 0644]
Modules/_io/clinic/textio.c.h
Modules/_io/textio.c

diff --git a/Misc/NEWS.d/next/Library/2026-06-04-18-22-56.gh-issue-143008.z5tw-J.rst b/Misc/NEWS.d/next/Library/2026-06-04-18-22-56.gh-issue-143008.z5tw-J.rst
new file mode 100644 (file)
index 0000000..e99bc39
--- /dev/null
@@ -0,0 +1 @@
+Fix race conditions when re-initializing a :class:`io.TextIOWrapper` object.
index 9407076b850cee904be0a3f26ddffefe42b91c9e..8d59bda5f74b3861a75d8cb54ad229e4cdc4da81 100644 (file)
@@ -666,7 +666,9 @@ _io_TextIOWrapper___init__(PyObject *self, PyObject *args, PyObject *kwargs)
         goto exit;
     }
 skip_optional_pos:
+    Py_BEGIN_CRITICAL_SECTION(self);
     return_value = _io_TextIOWrapper___init___impl((textio *)self, buffer, encoding, errors, newline, line_buffering, write_through);
+    Py_END_CRITICAL_SECTION();
 
 exit:
     return return_value;
@@ -1329,4 +1331,4 @@ _io_TextIOWrapper__CHUNK_SIZE_set(PyObject *self, PyObject *value, void *Py_UNUS
 
     return return_value;
 }
-/*[clinic end generated code: output=f900b42090c9781c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=8c571c9dba87d2b1 input=a9049054013a1b77]*/
index b992184a232c6ac3e9b0fc89bbc81d1b0c7877b2..30e973575898ddb2a51c1bc477292eec2a596de7 100644 (file)
@@ -1061,6 +1061,7 @@ io_check_errors(PyObject *errors)
 
 
 /*[clinic input]
+@critical_section
 _io.TextIOWrapper.__init__
     buffer: object
     encoding: str(accept={str, NoneType}) = None
@@ -1104,7 +1105,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
                                 const char *encoding, PyObject *errors,
                                 const char *newline, int line_buffering,
                                 int write_through)
-/*[clinic end generated code: output=72267c0c01032ed2 input=e6cfaaaf6059d4f5]*/
+/*[clinic end generated code: output=72267c0c01032ed2 input=0f077220214c40a4]*/
 {
     PyObject *raw, *codec_info = NULL;
     PyObject *res;