Skip to content

Commit 5c65834

Browse files
bpo-44219: Release the GIL during isatty syscalls (GH-28250)
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 06148b1) Co-authored-by: Vincent Michel <[email protected]>
1 parent dc2e11e commit 5c65834

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Release the GIL while performing ``isatty`` system calls on arbitrary file
2+
descriptors. In particular, this affects :func:`os.isatty`,
3+
:func:`os.device_encoding` and :class:`io.TextIOWrapper`. By extension,
4+
:func:`io.open` in text mode is also affected.

Modules/posixmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10058,9 +10058,11 @@ os_isatty_impl(PyObject *module, int fd)
1005810058
/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
1005910059
{
1006010060
int return_value;
10061+
Py_BEGIN_ALLOW_THREADS
1006110062
_Py_BEGIN_SUPPRESS_IPH
1006210063
return_value = isatty(fd);
1006310064
_Py_END_SUPPRESS_IPH
10065+
Py_END_ALLOW_THREADS
1006410066
return return_value;
1006510067
}
1006610068

Python/fileutils.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ _Py_device_encoding(int fd)
6969
UINT cp;
7070
#endif
7171
int valid;
72+
Py_BEGIN_ALLOW_THREADS
7273
_Py_BEGIN_SUPPRESS_IPH
7374
valid = isatty(fd);
7475
_Py_END_SUPPRESS_IPH
76+
Py_END_ALLOW_THREADS
7577
if (!valid)
7678
Py_RETURN_NONE;
7779

@@ -1737,12 +1739,22 @@ _Py_write_impl(int fd, const void *buf, size_t count, int gil_held)
17371739

17381740
_Py_BEGIN_SUPPRESS_IPH
17391741
#ifdef MS_WINDOWS
1740-
if (count > 32767 && isatty(fd)) {
1742+
if (count > 32767) {
17411743
/* Issue #11395: the Windows console returns an error (12: not
17421744
enough space error) on writing into stdout if stdout mode is
17431745
binary and the length is greater than 66,000 bytes (or less,
17441746
depending on heap usage). */
1745-
count = 32767;
1747+
if (gil_held) {
1748+
Py_BEGIN_ALLOW_THREADS
1749+
if (isatty(fd)) {
1750+
count = 32767;
1751+
}
1752+
Py_END_ALLOW_THREADS
1753+
} else {
1754+
if (isatty(fd)) {
1755+
count = 32767;
1756+
}
1757+
}
17461758
}
17471759
#endif
17481760
if (count > _PY_WRITE_MAX) {

0 commit comments

Comments
 (0)