Index: Misc/ACKS =================================================================== --- Misc/ACKS (revision 87589) +++ Misc/ACKS (working copy) @@ -131,6 +131,7 @@ Daniel Calvelo Tony Campbell Brett Cannon +Justin Cappos Mike Carlton Terry Carroll Lorenzo M. Catucci Index: Misc/NEWS =================================================================== --- Misc/NEWS (revision 87589) +++ Misc/NEWS (working copy) @@ -20,6 +20,10 @@ Library ------- +- Issue 7995: Fix on BSD & OS X that the socket returned from socket.accept() + must not inherit the listening socket's file status flags. Initial Patch by + Justin Cappos, unit tests by Ross Lagerwall. + - Issue 10786: unittest.TextTestRunner default stream no longer bound at import time. `sys.stderr` now looked up at instantiation time. Fix contributed by Mark Roddy. Index: Lib/test/test_socket.py =================================================================== --- Lib/test/test_socket.py (revision 87589) +++ Lib/test/test_socket.py (working copy) @@ -976,6 +976,21 @@ def _testInitNonBlocking(self): pass + def testInheritFlags(self): + 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: Modules/socketmodule.c =================================================================== --- Modules/socketmodule.c (revision 87589) +++ Modules/socketmodule.c (working copy) @@ -1700,6 +1700,16 @@ if (newfd == INVALID_SOCKET) return s->errorhandler(); +#if defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \ + __NetBSD__ + /* Try unset O_NONBLOCK and O_ASYNC if they are inherited. */ + int st_flag = fcntl(newfd, F_GETFL, 0); + if (st_flag != -1) { + st_flag &= ~(O_NONBLOCK | O_ASYNC); + fcntl(newfd, F_SETFL, st_flag); + } +#endif + sock = PyLong_FromSocket_t(newfd); if (sock == NULL) { SOCKETCLOSE(newfd);