Skip to content

NamedTemporaryFile doesn't issue a ResourceWarning when left unclosed on POSIX #126639

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

Closed
graingert opened this issue Nov 10, 2024 · 8 comments
Closed
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@graingert
Copy link
Contributor

graingert commented Nov 10, 2024

Bug report

Bug description:

import sys
import tempfile


def main():
    tempfile.NamedTemporaryFile()

if __name__ == "__main__":
    sys.exit(main())

when run with python -Werror demo.py nothing happens

if you open a file normally you get a ResourceWarning:

import sys
import tempfile


def main():
    open("example", "w")

if __name__ == "__main__":
    sys.exit(main())
 graingert@conscientious  ~/projects/temp_file_resource_warnings  python -Werror demo2.py
Exception ignored in: <_io.FileIO name='example' mode='wb' closefd=True>
Traceback (most recent call last):
  File "/home/graingert/projects/temp_file_resource_warnings/demo2.py", line 6, in main
    open("example", "w")
ResourceWarning: unclosed file <_io.TextIOWrapper name='example' mode='w' encoding='UTF-8'>

it appears that you do get a ResourceWarning on windows, but I've only seen it in CI I havn't reproduced locally yet

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

@graingert graingert added the type-bug An unexpected behavior, bug, or error label Nov 10, 2024
@graingert
Copy link
Contributor Author

I think this __del__ needs to issue the resource warning:

cpython/Lib/tempfile.py

Lines 471 to 472 in 450db61

def __del__(self):
self.cleanup()

@picnixz

This comment was marked as resolved.

@graingert
Copy link
Contributor Author

I don't see how that's related? The resource delete on __del__ is a last resort, and it needs to issue a ResourceWarning so that code that fails to finalise explicitly can be detected

@picnixz
Copy link
Member

picnixz commented Nov 10, 2024

Ah sorry, I misunderstood the "open again" in the sense that you can leave the descriptor hanging. My bad. However, non-named temporary files say:

It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected).

Should implicit close emit a warning in this case? Actually, why is there a warning on Windows?

@graingert
Copy link
Contributor Author

Should implicit close emit a warning in this case?

Yes, all implicit closing of resources on GC should issue a ResourceWarning

@graingert
Copy link
Contributor Author

I suspect the way to go here is to replace _TemporaryFileCloser with a weakref.finalize

@tomasr8
Copy link
Member

tomasr8 commented Nov 10, 2024

I suspect the way to go here is to replace _TemporaryFileCloser with a weakref.finalize

Looks like it, TemporaryDirectory is doing something similar:

cpython/Lib/tempfile.py

Lines 885 to 888 in 450db61

self._finalizer = _weakref.finalize(
self, self._cleanup, self.name,
warn_message="Implicitly cleaning up {!r}".format(self),
ignore_errors=self._ignore_cleanup_errors, delete=self._delete)

graingert added a commit to graingert/cpython that referenced this issue Nov 11, 2024
graingert added a commit to graingert/cpython that referenced this issue Nov 11, 2024
@graingert graingert added 3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Nov 11, 2024
kumaraditya303 added a commit that referenced this issue Dec 18, 2024
@picnixz picnixz added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error 3.12 only security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Dec 19, 2024
@picnixz
Copy link
Member

picnixz commented Dec 19, 2024

Categorizing it as a feature as per #126677 (comment).

srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants