[issue10188] tempfile.TemporaryDirectory may throw errors at shutdown

Nick Coghlan report at bugs.python.org
Sun Dec 12 03:22:16 CET 2010


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

I have a slightly better fix for that coming soon. There's a subtle race condition in the proposed approach that can lead to the temporary dir not being deleted if an asynchronous exception, such as KeyboardInterrupt, arrives after mkdtemp completes successfully, but before _closed is set back to False.

Instead, the new init code will look like:

        self._closed = False
        self.name = None # Handle mkdtemp throwing an exception
        self.name = mkdtemp(suffix, prefix, dir)

And the cleanup condition will be gated on self.name being set as well as on _closed being False. I believe there is still a window where mkdtemp successfully creates the directory, but self.name never gets set, but we want to make that window as small as possible.

I also realised this should be emitting a ResourceWarning from __del__, as well catching the errors and shut down and turning them into something more meaningful on sys.stderr.

The attached patch won't quite apply cleanly, since it predates r87172

----------
keywords: +patch
Added file: http://bugs.python.org/file20022/issue10888_tempdir_cleanup.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10188>
_______________________________________


More information about the Python-bugs-list mailing list