diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-07-04 10:28:55 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-04 10:28:55 (GMT) |
commit | 9d40554e0da09a44a8547f3f3a2b9dedfeaf7928 (patch) | |
tree | 4ee94eec007f76e75675a50221e0aaf7c81b4cbf /Lib/multiprocessing | |
parent | b607d992e76e485f20be3bfd6b311525123f936b (diff) | |
download | cpython-9d40554e0da09a44a8547f3f3a2b9dedfeaf7928.zip cpython-9d40554e0da09a44a8547f3f3a2b9dedfeaf7928.tar.gz cpython-9d40554e0da09a44a8547f3f3a2b9dedfeaf7928.tar.bz2 |
bpo-37421: Fix multiprocessing get_temp_dir() finalizer (GH-14572)
Fix multiprocessing.util.get_temp_dir() finalizer: clear also the
'tempdir' configuration of the current process, so next call to
get_temp_dir() will create a new temporary directory, rather than
reusing the removed temporary directory.
Diffstat (limited to 'Lib/multiprocessing')
-rw-r--r-- | Lib/multiprocessing/util.py | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py index 5674ad7..1938091 100644 --- a/Lib/multiprocessing/util.py +++ b/Lib/multiprocessing/util.py @@ -106,6 +106,15 @@ def log_to_stderr(level=None): # Function returning a temp directory which will be removed on exit # +def _remove_temp_dir(rmtree, tempdir): + rmtree(tempdir) + + current_process = process.current_process() + # current_process() can be None if the finalizer is called + # late during Python finalization + if current_process is not None: + current_process._config['tempdir'] = None + def get_temp_dir(): # get name of a temp directory which will be automatically cleaned up tempdir = process.current_process()._config.get('tempdir') @@ -113,7 +122,10 @@ def get_temp_dir(): import shutil, tempfile tempdir = tempfile.mkdtemp(prefix='pymp-') info('created temp directory %s', tempdir) - Finalize(None, shutil.rmtree, args=[tempdir], exitpriority=-100) + # keep a strong reference to shutil.rmtree(), since the finalizer + # can be called late during Python shutdown + Finalize(None, _remove_temp_dir, args=(shutil.rmtree, tempdir), + exitpriority=-100) process.current_process()._config['tempdir'] = tempdir return tempdir |