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
pickle and pickletools cli interface doesn't close input and output file. #85567
Comments
pickle and pickletools use argparse with FileType which is not automatically closed. Other cli interfaces like json [0], ast [1] use context manager to close filetype objects. pickle : Line 1799 in af08db7
mypickle
./python -Wall -m pickle mypickle pickletools : Lines 2850 to 2855 in af08db7
./python -Wall -m pickletools mypickle -o mypickle.py [0] Line 61 in af08db7
[1] Line 1510 in af08db7
|
Hi, First-time contributor here, I've made a patch in follow-up to the discussions that happened in Amir's patch in regards to this. I'd appreciate it if someone would be able to take a look and review it! |
There is a problem with #32257 -- the change prevent closing file object by the garbage collector. More complex solution could work:
|
I'm not a fan of closing files magically at exit. I prefer to close them explicitly. Why not fixing the issue in pickletools, rather than changing the argparse module? Since the Refleaks buildbots are broken for 6 days, I just revert the PR until someone can investigate the issue and fix it: #91771 |
The note about using
An enhanced cleanup check that works the way @serhiy-storchaka suggests (closing all files in a WeakSet, rather than holding a strong reference to all opened files until the program ends) would mean that the files would always be closed at the Python level (even if some subsequent error prevents the affected app from reaching its "normal" file handling code) without preventing the normal GC cleanup. |
I am not completely satisfied with closing files at exit, even if use a WeakSet. It makes us relying on the garbage collector to prevent file descriptor exhausting when The https://github.com/pallets/click library has several FileType-like types and multiple options to control the behavior (it perhaps even overengineered). I see two general ways to solve the problem:
And the problem is not limited to open files. We need a way to specify how to clean up resources with user-defined types (for example a type similar to FileType, but supporting compression, or opening a URL instead of a file). The above two options could be generalized. For now, I think it is easier to revert #32257. |
There is an older and more specific issue for FileType flaws: #58032. Let continue there. |
@serhiy-storchaka The issue seems to be resolved in this PR. |
Explicitly open and close files instead of using FileType.
The The only reliable way to handle this is to get rid of FileType and open and close files explicitly. PR #113618 does this. |
…ythonGH-113618) Explicitly open and close files instead of using FileType. (cherry picked from commit bd754b9) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
…ythonGH-113618) Explicitly open and close files instead of using FileType. (cherry picked from commit bd754b9) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
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:
Linked PRs
The text was updated successfully, but these errors were encountered: