From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 10 Jun 2026 09:41:47 +0000 (+0200) Subject: [3.14] gh-143008: Fix race re-initializing TextIOWrapper (GH-151203) (#151226) X-Git-Tag: v3.14.6~1 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=a4bdc218d5429fc91b9824dfa1efff6e729127f5;p=thirdparty%2FPython%2Fcpython.git [3.14] gh-143008: Fix race re-initializing TextIOWrapper (GH-151203) (#151226) [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 --- 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 index 000000000000..e99bc39c45f9 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-04-18-22-56.gh-issue-143008.z5tw-J.rst @@ -0,0 +1 @@ +Fix race conditions when re-initializing a :class:`io.TextIOWrapper` object. diff --git a/Modules/_io/clinic/textio.c.h b/Modules/_io/clinic/textio.c.h index 9407076b850c..8d59bda5f74b 100644 --- a/Modules/_io/clinic/textio.c.h +++ b/Modules/_io/clinic/textio.c.h @@ -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]*/ diff --git a/Modules/_io/textio.c b/Modules/_io/textio.c index b992184a232c..30e973575898 100644 --- a/Modules/_io/textio.c +++ b/Modules/_io/textio.c @@ -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;