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

tempfile.NamedTemporaryFile leaks file descriptor when fdopen fails #65257

Closed
tholzer mannequin opened this issue Mar 25, 2014 · 7 comments
Closed

tempfile.NamedTemporaryFile leaks file descriptor when fdopen fails #65257

tholzer mannequin opened this issue Mar 25, 2014 · 7 comments
Labels
performance Performance or resource usage stdlib Python modules in the Lib dir

Comments

@tholzer
Copy link
Mannequin

tholzer mannequin commented Mar 25, 2014

BPO 21058
Nosy @vstinner, @vajrasky
Files
  • fix_typo_21058.patch
  • 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-03-25.08:22:10.393>
    created_at = <Date 2014-03-25.04:47:27.720>
    labels = ['library', 'performance']
    title = 'tempfile.NamedTemporaryFile leaks file descriptor when fdopen fails'
    updated_at = <Date 2014-03-25.17:19:59.155>
    user = 'https://bugs.python.org/tholzer'

    bugs.python.org fields:

    activity = <Date 2014-03-25.17:19:59.155>
    actor = 'python-dev'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-03-25.08:22:10.393>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2014-03-25.04:47:27.720>
    creator = 'tholzer'
    dependencies = []
    files = ['34617']
    hgrepos = []
    issue_num = 21058
    keywords = ['patch']
    message_count = 7.0
    messages = ['214778', '214779', '214780', '214783', '214786', '214830', '214841']
    nosy_count = 4.0
    nosy_names = ['vstinner', 'tholzer', 'python-dev', 'vajrasky']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = None
    status = 'closed'
    superseder = None
    type = 'resource usage'
    url = 'https://bugs.python.org/issue21058'
    versions = ['Python 2.7']

    @tholzer
    Copy link
    Mannequin Author

    tholzer mannequin commented Mar 25, 2014

    The NamedTemporaryFile inside the standard tempfile library leaks an open file descriptor when fdopen fails.

    Test case:

    # ulimit -SHn 50
    # python test1.py

    from tempfile import NamedTemporaryFile
    
    while 1:
        try:
            NamedTemporaryFile(mode='x')
        except ValueError as ex:
            pass

    Output:

    Traceback (most recent call last):
      File "./a2.py", line 7, in <module>
      File "/usr/lib/python2.7/tempfile.py", line 454, in NamedTemporaryFile
      File "/usr/lib/python2.7/tempfile.py", line 235, in _mkstemp_inner
    OSError: [Errno 24] Too many open files: '/tmp/tmpI0MIEW'
    Error in sys.excepthook:
    Traceback (most recent call last):
      File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
    ImportError: No module named fileutils
    
    Original exception was:
    Traceback (most recent call last):
      File "./a2.py", line 7, in <module>
      File "/usr/lib/python2.7/tempfile.py", line 454, in NamedTemporaryFile
      File "/usr/lib/python2.7/tempfile.py", line 235, in _mkstemp_inner
    OSError: [Errno 24] Too many open files: '/tmp/tmpI0MIEW'

    Patch:

    @@ -452,7 +452,11 @@
             flags |= _os.O_TEMPORARY
     
         (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
    -    file = _os.fdopen(fd, mode, bufsize)
    +    try:
    +        file = _os.fdopen(fd, mode, bufsize)
    +    except Exception as ex:
    +        _os.close(fd)
    +        raise
         return _TemporaryFileWrapper(file, name, delete)
     
     if _os.name != 'posix' or _os.sys.platform == 'cygwin':

    @tholzer tholzer mannequin added stdlib Python modules in the Lib dir performance Performance or resource usage labels Mar 25, 2014
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 25, 2014

    New changeset f2f0eec4a556 by Victor Stinner in branch '2.7':
    Issue bpo-21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
    http://hg.python.org/cpython/rev/f2f0eec4a556

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 25, 2014

    New changeset 182f08c0dd45 by Victor Stinner in branch '2.7':
    Issue bpo-21058: NamedTemporaryFile() closes the FD on any error, not only Exception
    http://hg.python.org/cpython/rev/182f08c0dd45

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 25, 2014

    New changeset 7f87c26f59ab by Victor Stinner in branch '3.4':
    Issue bpo-21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
    http://hg.python.org/cpython/rev/7f87c26f59ab

    New changeset b1e3035216f8 by Victor Stinner in branch 'default':
    (Merge 3.4) Issue bpo-21058: Fix a leak of file descriptor in
    http://hg.python.org/cpython/rev/b1e3035216f8

    @vstinner
    Copy link
    Member

    Thanks for the report, the issue is now fixed.

    @vajrasky
    Copy link
    Mannequin

    vajrasky mannequin commented Mar 25, 2014

    There is a typo.

    s/io.pen/io.open/

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 25, 2014

    New changeset aa2a05fe46ae by Victor Stinner in branch '3.4':
    Issue bpo-21058: fix typo in a comment. Patch written by Vajrasky Kok.
    http://hg.python.org/cpython/rev/aa2a05fe46ae

    New changeset 4e3c76cb0e8a by Victor Stinner in branch 'default':
    (Merge 3.4) Issue bpo-21058: fix typo in a comment. Patch written by Vajrasky Kok.
    http://hg.python.org/cpython/rev/4e3c76cb0e8a

    @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