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 tholzer
Recipients tholzer
Date 2014-03-25.04:47:26
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1395722847.75.0.725562206789.issue21058@psf.upfronthosting.co.za>
In-reply-to
Content
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':
History
Date User Action Args
2014-03-25 04:47:27tholzersetrecipients: + tholzer
2014-03-25 04:47:27tholzersetmessageid: <1395722847.75.0.725562206789.issue21058@psf.upfronthosting.co.za>
2014-03-25 04:47:27tholzerlinkissue21058 messages
2014-03-25 04:47:26tholzercreate