You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
NamedTemporaryFile could cause double-close on an fd if _TemporaryFileWrapper throws #83499
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='https://github.com/serhiy-storchaka'closed_at=Nonecreated_at=<Date2020-01-13.04:56:47.144>labels= ['3.8', 'type-bug', 'library', '3.9', '3.10']
title='NamedTemporaryFile could cause double-close on an fd if _TemporaryFileWrapper throws'updated_at=<Date2021-03-28.03:37:24.913>user='https://bugs.python.org/nneonneo'
If _TemporaryFileWrapper throws any kind of exception (even KeyboardInterrupt), this closes fd but leaks a valid file pointing to that fd. The file will later attempt to close the fd when it is collected, which can lead to subtle bugs. (This particular issue contributed to this bug: https://nedbatchelder.com/blog/202001/bug_915_please_help.html)
Just a heads up in python 3.11 on Travis CI I am seeing a similar issue with sqlite throwing an I/O error
randomly during testing. Some tests always fail, other tests randomly fail. E.g.
See #93874. It was mostly ready (except a NEWS entry and some polishing) more than 2 years ago. Now I do not remember why it was not finished at that time -- either I just put it off until the next morning, but was distracted by other issues and forgot about it, or I found some issues with it, or I found a better way. So please make a careful review.
Maybe it was deferred because there is a very tiny possibility of a file descriptor leak if you interrupt the program between os.open() and return. It is a part of more general issue and is not solvable at the moment.
Activity
nneonneo commentedon Jan 13, 2020
tempfile.NamedTemporaryFile creates its wrapper like so:
If _TemporaryFileWrapper throws any kind of exception (even KeyboardInterrupt), this closes
fdbut leaks a validfilepointing to that fd. Thefilewill later attempt to close thefdwhen it is collected, which can lead to subtle bugs. (This particular issue contributed to this bug: https://nedbatchelder.com/blog/202001/bug_915_please_help.html)This should probably be rewritten as:
or perhaps use nested try blocks to avoid the _os.unlink duplication.
albertz commentedon Jan 13, 2020
Instead of `except:` and `except BaseException:`, I think better use `except Exception:`.
For further discussion and reference, also see the discussion here: https://news.ycombinator.com/item?id=22028581
serhiy-storchaka commentedon Jan 13, 2020
This issue is more complex. I am working on it.
10 remaining items
rouilj commentedon Jun 15, 2022
Just a heads up in python 3.11 on Travis CI I am seeing a similar issue with sqlite throwing an I/O error
randomly during testing. Some tests always fail, other tests randomly fail. E.g.
https://app.travis-ci.com/github/roundup-tracker/roundup/jobs/573649310#L5779
for a sample failure. Note that the same suite works fine with 3.8 and 3.10.
So 3.11 might be more prone to this. Does it make sense to add 3.11 to the labels on this ticket.
pythongh-83499: Fix closing file descriptors in tempfile
serhiy-storchaka commentedon Jun 17, 2022
See #93874. It was mostly ready (except a NEWS entry and some polishing) more than 2 years ago. Now I do not remember why it was not finished at that time -- either I just put it off until the next morning, but was distracted by other issues and forgot about it, or I found some issues with it, or I found a better way. So please make a careful review.
Maybe it was deferred because there is a very tiny possibility of a file descriptor leak if you interrupt the program between
os.open()andreturn. It is a part of more general issue and is not solvable at the moment.gh-83499: Fix closing file descriptors in tempfile (GH-93874)
pythongh-83499: Fix closing file descriptors in tempfile (pythonGH-93874
[3.11] pythongh-83499: Fix closing file descriptors in tempfile (pyth…
pythongh-83499: Fix closing file descriptors in tempfile (pythonGH-93874
gh-83499: Fix closing file descriptors in tempfile (GH-93874)
gh-83499: Fix closing file descriptors in tempfile (GH-93874)