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
Comments
The NamedTemporaryFile inside the standard tempfile library leaks an open file descriptor when fdopen fails. Test case: # ulimit -SHn 50 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': |
New changeset f2f0eec4a556 by Victor Stinner in branch '2.7': |
New changeset 182f08c0dd45 by Victor Stinner in branch '2.7': |
New changeset 7f87c26f59ab by Victor Stinner in branch '3.4': New changeset b1e3035216f8 by Victor Stinner in branch 'default': |
Thanks for the report, the issue is now fixed. |
There is a typo. s/io.pen/io.open/ |
New changeset aa2a05fe46ae by Victor Stinner in branch '3.4': New changeset 4e3c76cb0e8a by Victor Stinner in branch 'default': |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: