]> granicus.if.org Git - python/commitdiff
Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
authorCharles-François Natali <cf.natali@gmail.com>
Mon, 13 Oct 2014 17:39:34 +0000 (18:39 +0100)
committerCharles-François Natali <cf.natali@gmail.com>
Mon, 13 Oct 2014 17:39:34 +0000 (18:39 +0100)
Lib/SocketServer.py
Lib/test/test_socketserver.py
Misc/NEWS

index afb8686f732824c74c59addae4c806f471e981c1..23ce6fc3012d82a24d00c507fa24ae8b393ba94c 100644 (file)
@@ -416,8 +416,12 @@ class TCPServer(BaseServer):
         self.socket = socket.socket(self.address_family,
                                     self.socket_type)
         if bind_and_activate:
-            self.server_bind()
-            self.server_activate()
+            try:
+                self.server_bind()
+                self.server_activate()
+            except:
+                self.server_close()
+                raise
 
     def server_bind(self):
         """Called by constructor to bind the socket.
index 83f5e3f72074920231ca9313adaf6b9a32611367..8707017f3b25ce824e8bd1e26267248e288bdd71 100644 (file)
@@ -314,6 +314,16 @@ class SocketServerTest(unittest.TestCase):
         for t, s in threads:
             t.join()
 
+    def test_tcpserver_bind_leak(self):
+        # Issue #22435: the server socket wouldn't be closed if bind()/listen()
+        # failed.
+        # Create many servers for which bind() will fail, to see if this result
+        # in FD exhaustion.
+        for i in range(1024):
+            with self.assertRaises(OverflowError):
+                SocketServer.TCPServer((HOST, -1),
+                                       SocketServer.StreamRequestHandler)
+
 
 def test_main():
     if imp.lock_held():
index 27a632213c10972759dba393e24ccdc89189ad23..62f3deb7774edea21315f377091fe3b81fb747bc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
+
 - Issue #21580: Now Tkinter correctly handles binary "data" and "maskdata"
   configure options of tkinter.PhotoImage.