[Python-checkins] bpo-44219: Release the GIL during isatty syscalls (GH-28250) (GH-28255)

ambv webhook-mailer at python.org
Thu Sep 9 12:35:53 EDT 2021


https://github.com/python/cpython/commit/23c46778d65870784cb6d4de30f43aac62d71e73
commit: 23c46778d65870784cb6d4de30f43aac62d71e73
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: ambv <lukasz at langa.pl>
date: 2021-09-09T18:35:43+02:00
summary:

bpo-44219: Release the GIL during isatty syscalls (GH-28250) (GH-28255)

Release the GIL while performing isatty() system calls on arbitrary
file descriptors. In particular, this affects os.isatty(),
os.device_encoding() and io.TextIOWrapper. By extension,
io.open() in text mode is also affected.
(cherry picked from commit 06148b1870fceb1a21738761b8e1ac3bf654319b)

Co-authored-by: Vincent Michel <vxgmichel at gmail.com>

files:
A Misc/NEWS.d/next/Core and Builtins/2021-09-09-10-32-33.bpo-44219.WiYyjz.rst
M Modules/posixmodule.c
M Python/fileutils.c

diff --git a/Misc/NEWS.d/next/Core and Builtins/2021-09-09-10-32-33.bpo-44219.WiYyjz.rst b/Misc/NEWS.d/next/Core and Builtins/2021-09-09-10-32-33.bpo-44219.WiYyjz.rst
new file mode 100644
index 0000000000000..12915ffe3c07c
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2021-09-09-10-32-33.bpo-44219.WiYyjz.rst	
@@ -0,0 +1,4 @@
+Release the GIL while performing ``isatty`` system calls on arbitrary file
+descriptors. In particular, this affects :func:`os.isatty`,
+:func:`os.device_encoding` and :class:`io.TextIOWrapper`. By extension,
+:func:`io.open` in text mode is also affected.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 25ddc82cb893a..18761cc490448 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -10062,9 +10062,11 @@ os_isatty_impl(PyObject *module, int fd)
 /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
 {
     int return_value;
+    Py_BEGIN_ALLOW_THREADS
     _Py_BEGIN_SUPPRESS_IPH
     return_value = isatty(fd);
     _Py_END_SUPPRESS_IPH
+    Py_END_ALLOW_THREADS
     return return_value;
 }
 
diff --git a/Python/fileutils.c b/Python/fileutils.c
index e8a7eda505c7e..9e732ddca55ce 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -67,9 +67,11 @@ PyObject *
 _Py_device_encoding(int fd)
 {
     int valid;
+    Py_BEGIN_ALLOW_THREADS
     _Py_BEGIN_SUPPRESS_IPH
     valid = isatty(fd);
     _Py_END_SUPPRESS_IPH
+    Py_END_ALLOW_THREADS
     if (!valid)
         Py_RETURN_NONE;
 
@@ -1776,12 +1778,22 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
 
     _Py_BEGIN_SUPPRESS_IPH
 #ifdef MS_WINDOWS
-    if (count > 32767 && isatty(fd)) {
+    if (count > 32767) {
         /* Issue #11395: the Windows console returns an error (12: not
            enough space error) on writing into stdout if stdout mode is
            binary and the length is greater than 66,000 bytes (or less,
            depending on heap usage). */
-        count = 32767;
+        if (gil_held) {
+            Py_BEGIN_ALLOW_THREADS
+            if (isatty(fd)) {
+                count = 32767;
+            }
+            Py_END_ALLOW_THREADS
+        } else {
+            if (isatty(fd)) {
+                count = 32767;
+            }
+        }
     }
 #endif
     if (count > _PY_WRITE_MAX) {



More information about the Python-checkins mailing list