]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13][3.15] gh-143008: Fix race re-initializing TextIOWrapper (#151203) (#151227)
authorVictor Stinner <vstinner@python.org>
Wed, 10 Jun 2026 09:40:14 +0000 (11:40 +0200)
committerGitHub <noreply@github.com>
Wed, 10 Jun 2026 09:40:14 +0000 (09:40 +0000)
[3.15] gh-143008: Fix race re-initializing TextIOWrapper (#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 de9216a9e05fe14d6f8da839039e324e2d97f929..f35538ee75fa48afad819fd64b5ed58f1b1f8db2 100644 (file)
@@ -624,7 +624,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;
@@ -1284,4 +1286,4 @@ _io_TextIOWrapper__CHUNK_SIZE_set(textio *self, PyObject *value, void *Py_UNUSED
 
     return return_value;
 }
-/*[clinic end generated code: output=3ac677af6649862d input=a9049054013a1b77]*/
+/*[clinic end generated code: output=2d73d5adb0ce09a8 input=a9049054013a1b77]*/
index ef3b3e98b725f8364e805db31858b2b4e321501c..d093514aa66c6ad2a73298ed0ec7b907ddb2df15 100644 (file)
@@ -1049,6 +1049,7 @@ io_check_errors(PyObject *errors)
 
 
 /*[clinic input]
+@critical_section
 _io.TextIOWrapper.__init__
     buffer: object
     encoding: str(accept={str, NoneType}) = None
@@ -1092,7 +1093,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;