[New-bugs-announce] [issue22435] socketserver.TCPSocket leaks socket to garbage collector if server_bind() fails

Martin Panter report at bugs.python.org
Thu Sep 18 02:25:22 CEST 2014


New submission from Martin Panter:

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.0.0.0', 0)>
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, ...):
        try:
            super().__init__((host, port), RequestHandlerClass)
        except:  # Workaround for socketserver.TCPServer leaking socket
            self.close()
            raise
    
    def close(self):
        return self.server_close()

----------
components: Library (Lib)
messages: 227017
nosy: vadmium
priority: normal
severity: normal
status: open
title: socketserver.TCPSocket leaks socket to garbage collector if server_bind() fails
type: resource usage
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue22435>
_______________________________________


More information about the New-bugs-announce mailing list