]> granicus.if.org Git - python/commitdiff
Issue #7995: When calling accept() on a socket with a timeout, the returned
authorAntoine Pitrou <solipsis@pitrou.net>
Wed, 5 Jan 2011 21:03:42 +0000 (21:03 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Wed, 5 Jan 2011 21:03:42 +0000 (21:03 +0000)
socket is now always non-blocking, regardless of the operating system.

Lib/socket.py
Lib/test/test_socket.py
Misc/NEWS

index d0da7409bb059f7181bfe5f2298c5e7a08fdda92..95901aeca1c5342b4d915f04b8df6239137f051b 100644 (file)
@@ -130,7 +130,13 @@ class socket(_socket.socket):
         For IP sockets, the address info is a pair (hostaddr, port).
         """
         fd, addr = self._accept()
-        return socket(self.family, self.type, self.proto, fileno=fd), addr
+        sock = socket(self.family, self.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.
+        if getdefaulttimeout() is None and self.gettimeout():
+            sock.setblocking(True)
+        return sock, addr
 
     def makefile(self, mode="r", buffering=None, *,
                  encoding=None, errors=None, newline=None):
index 8f96fe48a31d57cdfe9803a2b88d79fd58dcb9fe..23d22a8695fe57af3cfe991b6037a05aa012fe65 100644 (file)
@@ -982,6 +982,23 @@ class NonBlockingTCPTests(ThreadedTCPSocketTest):
         def _testInitNonBlocking(self):
             pass
 
+    def testInheritFlags(self):
+        # Issue #7995: when calling accept() on a listening socket with a
+        # timeout, the resulting socket should not be non-blocking.
+        self.serv.settimeout(10)
+        try:
+            conn, addr = self.serv.accept()
+            message = conn.recv(len(MSG))
+        finally:
+            conn.close()
+            self.serv.settimeout(None)
+
+    def _testInheritFlags(self):
+        time.sleep(0.1)
+        self.cli.connect((HOST, self.port))
+        time.sleep(0.5)
+        self.cli.send(MSG)
+
     def testAccept(self):
         # Testing non-blocking accept
         self.serv.setblocking(0)
index 1081fc4d0e68c079fb0d880c8dc8f8b5808b0103..5050ae827599b545b7b0bcbfee8afe6ce1fb247c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #7995: When calling accept() on a socket with a timeout, the returned
+  socket is now always non-blocking, regardless of the operating system.
+
 - Issue #10756: atexit normalizes the exception before displaying it. Patch by
   Andreas Stührk.