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

asyncore.file_wrapper should implement __del__ and call close there to prevent resource leaks and behave like socket.socket does. #55662

Closed
amajorek mannequin opened this issue Mar 9, 2011 · 7 comments
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@amajorek
Copy link
Mannequin

amajorek mannequin commented Mar 9, 2011

BPO 11453
Nosy @vstinner, @giampaolo
Files
  • issue11453.patch: Adding exit asyncore.file_wrapper
  • 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 2014-06-27.21:54:24.509>
    created_at = <Date 2011-03-09.19:29:51.690>
    labels = ['library', 'performance']
    title = 'asyncore.file_wrapper should implement __del__ and call close there to prevent resource leaks and behave like socket.socket does.'
    updated_at = <Date 2014-07-28.23:01:59.868>
    user = 'https://bugs.python.org/amajorek'

    bugs.python.org fields:

    activity = <Date 2014-07-28.23:01:59.868>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-06-27.21:54:24.509>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2011-03-09.19:29:51.690>
    creator = 'amajorek'
    dependencies = []
    files = ['21236']
    hgrepos = []
    issue_num = 11453
    keywords = ['patch']
    message_count = 7.0
    messages = ['130458', '131082', '137457', '221742', '221743', '221745', '224202']
    nosy_count = 5.0
    nosy_names = ['vstinner', 'giampaolo.rodola', 'amajorek', 'beardedp', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue11453'
    versions = ['Python 3.4', 'Python 3.5']

    @amajorek
    Copy link
    Mannequin Author

    amajorek mannequin commented Mar 9, 2011

    asyncore.file_wrapper duplicates file descriptor of given file and closes it in it's close method.

    But unlike socket.socket class it does not automatically call close when object is garbage collected.

    Users of regular sockets and asyncore.dispatcher do not experience resource leaks when they forget to call self.close() in handle_close().

    But people using file_dispatcher do loose file descriptor every time file_wrapper object is garbage collected without calling self.close() first.

    @amajorek amajorek mannequin added stdlib Python modules in the Lib dir performance Performance or resource usage labels Mar 9, 2011
    @beardedp
    Copy link
    Mannequin

    beardedp mannequin commented Mar 16, 2011

    Adding a patch that adds an __exit__ function much like the one that socket.socket implements. Passes the test_asyncore & also doesn't raise a resource warning when I explicitly comment out some close() calls on file wrapper objects in the test.

    @amajorek
    Copy link
    Mannequin Author

    amajorek mannequin commented Jun 1, 2011

    Adding __exit__ will not make asyncore.file_wrapper close file descriptor when garbage collected.

    Here is clone of socket.py solution for the same problem.

      def close(self):
        if self.fd:
          os.close(self.fd)
          self.fd = None # or maybe self.fd = 0 will be better
    
      def __del__(self):
        try:
          self.close()
        except:
          # close() may fail if __init__ didn't complete
          pass

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 27, 2014

    New changeset ae12a926e680 by Victor Stinner in branch '3.4':
    Issue bpo-11453: asyncore: emit a ResourceWarning when an unclosed file_wrapper
    http://hg.python.org/cpython/rev/ae12a926e680

    @vstinner
    Copy link
    Member

    I fixed the issue in Python 3.4 and 3.5, thanks for the report.

    In Python 3.4+, it's safe to add a destructor (del method): even if the object is part of a reference cycle, it will be destroyed. It's not the case in Python 2.7. I prefer to leave Python 2.7 unchanged to limit the risk of regression.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jun 27, 2014

    New changeset 7c9335d97628 by Victor Stinner in branch 'default':
    (Merge 3.4) Issue bpo-11453: asyncore: emit a ResourceWarning when an unclosed
    http://hg.python.org/cpython/rev/7c9335d97628

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Jul 28, 2014

    New changeset 379aad232000 by Victor Stinner in branch '3.4':
    Issue bpo-11453, bpo-18174: Fix leak of file descriptor in test_asyncore
    http://hg.python.org/cpython/rev/379aad232000

    New changeset 0ced2d2325fb by Victor Stinner in branch 'default':
    (Merge 3.4) Issue bpo-11453, bpo-18174: Fix leak of file descriptor in test_asyncore
    http://hg.python.org/cpython/rev/0ced2d2325fb

    @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

    1 participant