Issue #22435: Fix a file descriptor leak when SocketServer bind fails.

This commit is contained in:
Charles-François Natali 2014-10-13 18:39:34 +01:00
parent c468abafc7
commit 977c424ef6
3 changed files with 18 additions and 2 deletions

View 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.

View 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():

View File

@ -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.