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.

Author martin.panter
Recipients martin.panter
Date 2014-09-18.00:25:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1410999922.66.0.432841070426.issue22435@psf.upfronthosting.co.za>
In-reply-to
Content
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()
History
Date User Action Args
2014-09-18 00:25:22martin.pantersetrecipients: + martin.panter
2014-09-18 00:25:22martin.pantersetmessageid: <1410999922.66.0.432841070426.issue22435@psf.upfronthosting.co.za>
2014-09-18 00:25:22martin.panterlinkissue22435 messages
2014-09-18 00:25:20martin.pantercreate