From: Benjamin Peterson Date: Sun, 25 Oct 2015 03:06:04 +0000 (-0700) Subject: accepted sockets shouldn't inherit the SOCK_NONBLOCK flag (closes #25471) X-Git-Tag: v3.5.1rc1~127^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d9dbf493837b798757ed68dbd97dd55cd0c6efa5;p=python accepted sockets shouldn't inherit the SOCK_NONBLOCK flag (closes #25471) --- diff --git a/Lib/socket.py b/Lib/socket.py index 004588671c..ff2f087678 100644 --- a/Lib/socket.py +++ b/Lib/socket.py @@ -185,7 +185,11 @@ class socket(_socket.socket): For IP sockets, the address info is a pair (hostaddr, port). """ fd, addr = self._accept() - sock = socket(self.family, self.type, self.proto, fileno=fd) + # If our type has the SOCK_NONBLOCK flag, we shouldn't pass it onto the + # new socket. We do not currently allow passing SOCK_NONBLOCK to + # accept4, so the returned socket is always blocking. + type = self.type & ~globals().get("SOCK_NONBLOCK", 0) + sock = socket(self.family, type, self.proto, fileno=fd) # Issue #7995: if no default timeout is set and the listening # socket had a (non-zero) timeout, force the new socket in blocking # mode to override platform-specific socket flags inheritance. diff --git a/Lib/test/test_socket.py b/Lib/test/test_socket.py index d3191121b5..a92265d9e5 100644 --- a/Lib/test/test_socket.py +++ b/Lib/test/test_socket.py @@ -3863,6 +3863,7 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest): read, write, err = select.select([self.serv], [], []) if self.serv in read: conn, addr = self.serv.accept() + self.assertIsNone(conn.gettimeout()) conn.close() else: self.fail("Error trying to do accept after select.") diff --git a/Misc/NEWS b/Misc/NEWS index 01fbf1786c..7d673b2cc2 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -96,6 +96,9 @@ Core and Builtins Library ------- +- Issue #25471: Sockets returned from accept() shouldn't appear to be + nonblocking. + - Issue #25441: asyncio: Raise error from drain() when socket is closed. - Issue #25411: Improved Unicode support in SMTPHandler through better use of