[Python-checkins] bpo-37421: Fix multiprocessing get_temp_dir() finalizer (GH-14572)

Miss Islington (bot) webhook-mailer at python.org
Thu Jul 4 06:46:04 EDT 2019


https://github.com/python/cpython/commit/2d438fc0b748b64d518ea8876af3f6963c6d7d60
commit: 2d438fc0b748b64d518ea8876af3f6963c6d7d60
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-07-04T03:45:58-07:00
summary:

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.
(cherry picked from commit 9d40554e0da09a44a8547f3f3a2b9dedfeaf7928)

Co-authored-by: Victor Stinner <vstinner at redhat.com>

files:
A Misc/NEWS.d/next/Library/2019-07-03-12-47-52.bpo-37421.gR5hC8.rst
M Lib/multiprocessing/util.py

diff --git a/Lib/multiprocessing/util.py b/Lib/multiprocessing/util.py
index 0c4eb2473273..0f9eac7e9fb4 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
 
diff --git a/Misc/NEWS.d/next/Library/2019-07-03-12-47-52.bpo-37421.gR5hC8.rst b/Misc/NEWS.d/next/Library/2019-07-03-12-47-52.bpo-37421.gR5hC8.rst
new file mode 100644
index 000000000000..450d76f07289
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-07-03-12-47-52.bpo-37421.gR5hC8.rst
@@ -0,0 +1,4 @@
+Fix :func:`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.



More information about the Python-checkins mailing list