mirror of
https://github.com/python/cpython.git
synced 2025-08-18 07:41:05 +00:00
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 9d40554e0d
)
Co-authored-by: Victor Stinner <vstinner@redhat.com>
This commit is contained in:
parent
070d3d928d
commit
957656ee1d
2 changed files with 17 additions and 1 deletions
|
@ -106,6 +106,15 @@ def log_to_stderr(level=None):
|
||||||
# Function returning a temp directory which will be removed on exit
|
# 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():
|
def get_temp_dir():
|
||||||
# get name of a temp directory which will be automatically cleaned up
|
# get name of a temp directory which will be automatically cleaned up
|
||||||
tempdir = process.current_process()._config.get('tempdir')
|
tempdir = process.current_process()._config.get('tempdir')
|
||||||
|
@ -113,7 +122,10 @@ def get_temp_dir():
|
||||||
import shutil, tempfile
|
import shutil, tempfile
|
||||||
tempdir = tempfile.mkdtemp(prefix='pymp-')
|
tempdir = tempfile.mkdtemp(prefix='pymp-')
|
||||||
info('created temp directory %s', tempdir)
|
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
|
process.current_process()._config['tempdir'] = tempdir
|
||||||
return tempdir
|
return tempdir
|
||||||
|
|
||||||
|
|
|
@ -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.
|
Loading…
Add table
Add a link
Reference in a new issue