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.

classification
Title: tempfile.NamedTemporaryFile leaks file descriptor when fdopen fails
Type: resource usage Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: python-dev, tholzer, vajrasky, vstinner
Priority: normal Keywords: patch

Created on 2014-03-25 04:47 by tholzer, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fix_typo_21058.patch vajrasky, 2014-03-25 15:15 review
Messages (7)
msg214778 - (view) Author: (tholzer) Date: 2014-03-25 04:47
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':
msg214779 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-03-25 08:08
New changeset f2f0eec4a556 by Victor Stinner in branch '2.7':
Issue #21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
http://hg.python.org/cpython/rev/f2f0eec4a556
msg214780 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-03-25 08:11
New changeset 182f08c0dd45 by Victor Stinner in branch '2.7':
Issue #21058: NamedTemporaryFile() closes the FD on any error, not only Exception
http://hg.python.org/cpython/rev/182f08c0dd45
msg214783 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-03-25 08:19
New changeset 7f87c26f59ab by Victor Stinner in branch '3.4':
Issue #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 #21058: Fix a leak of file descriptor in
http://hg.python.org/cpython/rev/b1e3035216f8
msg214786 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2014-03-25 08:22
Thanks for the report, the issue is now fixed.
msg214830 - (view) Author: Vajrasky Kok (vajrasky) * Date: 2014-03-25 15:15
There is a typo.

s/io.pen/io.open/
msg214841 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-03-25 17:19
New changeset aa2a05fe46ae by Victor Stinner in branch '3.4':
Issue #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 #21058: fix typo in a comment. Patch written by Vajrasky Kok.
http://hg.python.org/cpython/rev/4e3c76cb0e8a
History
Date User Action Args
2022-04-11 14:58:00adminsetgithub: 65257
2014-03-25 17:19:59python-devsetmessages: + msg214841
2014-03-25 15:15:05vajraskysetfiles: + fix_typo_21058.patch

nosy: + vajrasky
messages: + msg214830

keywords: + patch
2014-03-25 08:22:10vstinnersetstatus: open -> closed

nosy: + vstinner
messages: + msg214786

resolution: fixed
2014-03-25 08:19:53python-devsetmessages: + msg214783
2014-03-25 08:11:09python-devsetmessages: + msg214780
2014-03-25 08:08:38python-devsetnosy: + python-dev
messages: + msg214779
2014-03-25 04:47:27tholzercreate