[Python-checkins] cpython (2.7): Issue #22435: Fix a file descriptor leak when SocketServer bind fails.

charles-francois.natali python-checkins at python.org
Mon Oct 13 19:43:09 CEST 2014


https://hg.python.org/cpython/rev/437002018d2d
changeset:   93024:437002018d2d
branch:      2.7
parent:      91998:46c7a724b487
user:        Charles-François Natali <cf.natali at gmail.com>
date:        Mon Oct 13 18:39:34 2014 +0100
summary:
  Issue #22435: Fix a file descriptor leak when SocketServer bind fails.

files:
  Lib/SocketServer.py           |   8 ++++++--
  Lib/test/test_socketserver.py |  10 ++++++++++
  Misc/NEWS                     |   2 ++
  3 files changed, 18 insertions(+), 2 deletions(-)


diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py
--- a/Lib/SocketServer.py
+++ b/Lib/SocketServer.py
@@ -416,8 +416,12 @@
         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.
diff --git a/Lib/test/test_socketserver.py b/Lib/test/test_socketserver.py
--- a/Lib/test/test_socketserver.py
+++ b/Lib/test/test_socketserver.py
@@ -314,6 +314,16 @@
         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():
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -19,6 +19,8 @@
 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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list