]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-37811: FreeBSD, OSX: fix poll(2) usage in sockets module (GH-15202)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 14 Aug 2019 21:48:03 +0000 (14:48 -0700)
committerGitHub <noreply@github.com>
Wed, 14 Aug 2019 21:48:03 +0000 (14:48 -0700)
FreeBSD implementation of poll(2) restricts the timeout argument to be
either zero, or positive, or equal to INFTIM (-1).

Unless otherwise overridden, socket timeout defaults to -1. This value
is then converted to milliseconds (-1000) and used as argument to the
poll syscall. poll returns EINVAL (22), and the connection fails.

This bug was discovered during the EINTR handling testing, and the
reproduction code can be found in
https://bugs.python.org/issue23618 (see connect_eintr.py,
attached). On GNU/Linux, the example runs as expected.

This change is trivial:
If the supplied timeout value is negative, truncate it to -1.
(cherry picked from commit 28146206578ebe1b84b48e6f255738a227058c04)

Co-authored-by: Artem Khramov <akhramov@pm.me>
Misc/ACKS
Misc/NEWS.d/next/Library/2019-08-14-21-41-07.bpo-37811.d1xYj7.rst [new file with mode: 0644]
Modules/socketmodule.c

index 29b6690f2174a7fab4e3d7afe967ff33658d8cfd..4d8f96acfe301e6e7111ec40f49431856f8b981c 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -815,6 +815,7 @@ Lawrence Kesteloot
 Garvit Khatri
 Vivek Khera
 Dhiru Kholia
+Artem Khramov
 Akshit Khurana
 Sanyam Khurana
 Mads Kiilerich
diff --git a/Misc/NEWS.d/next/Library/2019-08-14-21-41-07.bpo-37811.d1xYj7.rst b/Misc/NEWS.d/next/Library/2019-08-14-21-41-07.bpo-37811.d1xYj7.rst
new file mode 100644 (file)
index 0000000..662e7dc
--- /dev/null
@@ -0,0 +1,4 @@
+Fix ``socket`` module's ``socket.connect(address)`` function being unable to
+establish connection in case of interrupted system call. The problem was
+observed on all OSes which ``poll(2)`` system call can take only
+non-negative integers and -1 as a timeout value.
index 65385e8974cf9cdc8ae6cbd6c4a9cd826bc08032..a9ef7e2083d58d20488f5f2a9f3ed5019f2d6854 100644 (file)
@@ -741,6 +741,17 @@ internal_select(PySocketSockObject *s, int writing, _PyTime_t interval,
     ms = _PyTime_AsMilliseconds(interval, _PyTime_ROUND_CEILING);
     assert(ms <= INT_MAX);
 
+    /* On some OSes, typically BSD-based ones, the timeout parameter of the
+       poll() syscall, when negative, must be exactly INFTIM, where defined,
+       or -1. See issue 37811. */
+    if (ms < 0) {
+#ifdef INFTIM
+        ms = INFTIM;
+#else
+        ms = -1;
+#endif
+    }
+
     Py_BEGIN_ALLOW_THREADS;
     n = poll(&pollfd, 1, (int)ms);
     Py_END_ALLOW_THREADS;