Skip to content
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.TemporaryFile() emits a ResourceWarning that does not trace back to user code #103070

Closed
mmerickel opened this issue Mar 28, 2023 · 2 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@mmerickel
Copy link

Feature or enhancement

When using tempfile.TemporaryFile a ResourceWarning should be emitted with the source tracing back to user code. Instead it currently traces back to the _io.open invocation inside the tempfile module and is useless for tracking down the code that created the tempfile.

Pitch

Take the following foo.py:

import tempfile


def main():
    fp = tempfile.TemporaryFile()


if __name__ == '__main__':
    main()

I would expect the ResourceWarning to point to the line fp = tempfile.TemporaryFile(). Instead I see the following:

❯ PYTHONTRACEMALLOC=1 PYTHONWARNINGS=default python3 -m foo
/Users/michael/work/foo.py:9: ResourceWarning: unclosed file <_io.BufferedRandom name=3>
  main()
Object allocated at (most recent call last):
  File "/opt/homebrew/Cellar/python@3.11/3.11.2_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/tempfile.py", lineno 657
    file = _io.open(dir, mode, buffering=buffering,

Note that it's pointing at the tempfile module itself which is worthless other than to tell me it's a tempfile that's causing the issue - however for me it's in library code and I have no idea which library!

Please modify tempfile to properly track the source and emit a better warning.

@mmerickel mmerickel added the type-feature A feature request or enhancement label Mar 28, 2023
@arhadthedev arhadthedev added stdlib Python modules in the Lib dir extension-modules C modules in the Modules dir and removed stdlib Python modules in the Lib dir labels Mar 30, 2023
@tomasr8
Copy link
Contributor

tomasr8 commented May 7, 2023

The documentation says that you can pass a number to PYTHONTRACEMALLOC which is the maximum number of frames it stores. Using PYTHONTRACEMALLOC=1 stores just 1 frame.

With PYTHONTRACEMALLOC=3 (or higher), you should see the full trace:

foo.py:9: ResourceWarning: unclosed file <_io.BufferedRandom name=3>
  main()
Object allocated at (most recent call last):
  File "foo.py", lineno 9
    main()
  File "foo.py", lineno 5
    fp = tempfile.TemporaryFile()
  File "/home/tomas/.pyenv/versions/3.11.2/lib/python3.11/tempfile.py", lineno 622
    file = _io.open(dir, mode, buffering=buffering,

@mmerickel
Copy link
Author

Confirmed this is working for me to track down those leaks - I had no idea that this was possible with the tracemalloc config. I'm going to close this issue. Thank you @tomasr8!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
Status: Done
Development

No branches or pull requests

3 participants