[issue34157] NamedTemporaryFile can leave temporary files behind

Nick Coghlan report at bugs.python.org
Fri Jul 20 11:17:51 EDT 2018


Nick Coghlan <ncoghlan at gmail.com> added the comment:

It's still the same problem - the underlying issue is that the with statement machinery doesn't currently mask signals in the main thread while __enter__ and __exit__ are running, so __enter__ and __exit__ methods written in Python are also at risk of being interrupted at an inopportune point.

This means that even "with closing(open('filename')) as f: ..." is more at risk of leaving the file open until __del__ cleans it up than the version that calls open() directly.

So if this a concern that an application needs to worry about, then it currently needs to adopt a more complicated execution structure where:

1. The main thread launches a subthread that actually does all the work.
2. The main thread immediately drops into "active_thread.join()" (which can be interrupted by Ctrl-C)

Unfortunately, this scheme *doesn't* work for applications where the application itself needs to detect and handling signals other than Ctrl-C.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34157>
_______________________________________


More information about the Python-bugs-list mailing list