This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: socketserver.TCPSocket leaks socket to garbage collector if server_bind() fails
Type: resource usage Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: martin.panter, neologix, pitrou, python-dev, vstinner
Priority: normal Keywords: patch

Created on 2014-09-18 00:25 by martin.panter, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
socketserver_bind_leak.diff neologix, 2014-09-19 21:21 review
Messages (6)
msg227017 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-09-18 00:25
Bind method may easily fail on Unix if there is no permission to bind to a privileged port:

>>> try: TCPServer(("", 80), ...)
... except Exception as err: err
PermissionError(13, 'Permission denied')
>>> gc.collect()
__main__:1: ResourceWarning: unclosed <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('', 0)>

This problem is inherited by HTTPServer and WSGIServer. My current workaround includes this code in a BaseServer fixup mixin, invoking server_close() if __init__() fails:

class Server(BaseServer, Context):
    def __init__(self, ...):
            super().__init__((host, port), RequestHandlerClass)
        except:  # Workaround for socketserver.TCPServer leaking socket
    def close(self):
        return self.server_close()
msg227126 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2014-09-19 21:21
Patch attached.
The test wouldn't result in FD exhaustion on CPython because of the reference counting, but should still trigger RessourceWarning.
msg227234 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-09-21 20:19
Patch looks of to me.
msg229256 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-13 17:43
New changeset 437002018d2d by Charles-François Natali in branch '2.7':
Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
msg229259 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-13 18:33
New changeset 9c8016af2ed8 by Charles-François Natali in branch '3.4':
Issue #22435: Fix a file descriptor leak when SocketServer bind fails.

New changeset 3bd0f2516445 by Charles-François Natali in branch 'default':
Issue #22435: Fix a file descriptor leak when SocketServer bind fails.
msg236723 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2015-02-27 02:01
This is fixed in 3.4.3. I think it can be closed.
Date User Action Args
2022-04-11 14:58:08adminsetgithub: 66625
2015-02-27 02:50:39berker.peksagsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2015-02-27 02:01:42martin.pantersetmessages: + msg236723
2014-10-13 18:33:10python-devsetmessages: + msg229259
2014-10-13 17:43:17python-devsetnosy: + python-dev
messages: + msg229256
2014-09-21 20:19:38pitrousetmessages: + msg227234
versions: + Python 3.5
2014-09-19 21:21:36neologixsetfiles: + socketserver_bind_leak.diff

nosy: + pitrou, vstinner
messages: + msg227126

keywords: + patch
stage: patch review
2014-09-18 13:17:17pitrousetnosy: + neologix
2014-09-18 00:25:22martin.pantercreate