Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socketserver.TCPSocket leaks socket to garbage collector if server_bind() fails #66625

Closed
vadmium opened this issue Sep 18, 2014 · 6 comments
Closed
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@vadmium
Copy link
Member

vadmium commented Sep 18, 2014

BPO 22435
Nosy @pitrou, @vstinner, @vadmium
Files
  • socketserver_bind_leak.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-02-27.02:50:39.865>
    created_at = <Date 2014-09-18.00:25:22.433>
    labels = ['library', 'performance']
    title = 'socketserver.TCPSocket leaks socket to garbage collector if server_bind() fails'
    updated_at = <Date 2015-02-27.02:50:39.864>
    user = 'https://github.com/vadmium'

    bugs.python.org fields:

    activity = <Date 2015-02-27.02:50:39.864>
    actor = 'berker.peksag'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-02-27.02:50:39.865>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2014-09-18.00:25:22.433>
    creator = 'martin.panter'
    dependencies = []
    files = ['36665']
    hgrepos = []
    issue_num = 22435
    keywords = ['patch']
    message_count = 6.0
    messages = ['227017', '227126', '227234', '229256', '229259', '236723']
    nosy_count = 5.0
    nosy_names = ['pitrou', 'vstinner', 'neologix', 'python-dev', 'martin.panter']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue22435'
    versions = ['Python 3.4', 'Python 3.5']

    @vadmium
    Copy link
    Member Author

    vadmium commented Sep 18, 2014

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

    @vadmium vadmium added stdlib Python modules in the Lib dir performance Performance or resource usage labels Sep 18, 2014
    @neologix
    Copy link
    Mannequin

    neologix mannequin commented Sep 19, 2014

    Patch attached.
    The test wouldn't result in FD exhaustion on CPython because of the reference counting, but should still trigger RessourceWarning.

    @pitrou
    Copy link
    Member

    pitrou commented Sep 21, 2014

    Patch looks of to me.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 13, 2014

    New changeset 437002018d2d by Charles-François Natali in branch '2.7':
    Issue bpo-22435: Fix a file descriptor leak when SocketServer bind fails.
    https://hg.python.org/cpython/rev/437002018d2d

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Oct 13, 2014

    New changeset 9c8016af2ed8 by Charles-François Natali in branch '3.4':
    Issue bpo-22435: Fix a file descriptor leak when SocketServer bind fails.
    https://hg.python.org/cpython/rev/9c8016af2ed8

    New changeset 3bd0f2516445 by Charles-François Natali in branch 'default':
    Issue bpo-22435: Fix a file descriptor leak when SocketServer bind fails.
    https://hg.python.org/cpython/rev/3bd0f2516445

    @vadmium
    Copy link
    Member Author

    vadmium commented Feb 27, 2015

    This is fixed in 3.4.3. I think it can be closed.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    performance Performance or resource usage stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants