Commit 61bd4d2e authored by Anselm Kruis's avatar Anselm Kruis Committed by Gregory P. Smith

[2.7] bpo-30028: make test.support.temp_cwd() fork-safe (GH-1066) (GH-5825)

Make test.support.temp_cwd() fork-safe. The context manager test.support.temp_cwd() no longer removes the temporary directory when executing in a process other than the parent it entered from.
If a forked child exits the context manager it won't do the cleanup..
(cherry picked from commit 33dddac0)
Co-authored-by: default avatarAnselm Kruis <a.kruis@science-computing.de>
parent b852d8c1
...@@ -753,10 +753,14 @@ def temp_dir(path=None, quiet=False): ...@@ -753,10 +753,14 @@ def temp_dir(path=None, quiet=False):
raise raise
warnings.warn('tests may fail, unable to create temp dir: ' + path, warnings.warn('tests may fail, unable to create temp dir: ' + path,
RuntimeWarning, stacklevel=3) RuntimeWarning, stacklevel=3)
if dir_created:
pid = os.getpid()
try: try:
yield path yield path
finally: finally:
if dir_created: # In case the process forks, let only the parent remove the
# directory. The child has a diffent process id. (bpo-30028)
if dir_created and pid == os.getpid():
rmtree(path) rmtree(path)
@contextlib.contextmanager @contextlib.contextmanager
......
...@@ -6,8 +6,10 @@ import os ...@@ -6,8 +6,10 @@ import os
import unittest import unittest
import socket import socket
import tempfile import tempfile
import textwrap
import errno import errno
from test import support from test import support
from test.support import script_helper
TESTFN = support.TESTFN TESTFN = support.TESTFN
...@@ -176,6 +178,34 @@ class TestSupport(unittest.TestCase): ...@@ -176,6 +178,34 @@ class TestSupport(unittest.TestCase):
expected = ['tests may fail, unable to create temp dir: ' + path] expected = ['tests may fail, unable to create temp dir: ' + path]
self.assertEqual(warnings, expected) self.assertEqual(warnings, expected)
@unittest.skipUnless(hasattr(os, "fork"), "test requires os.fork")
def test_temp_dir__forked_child(self):
"""Test that a forked child process does not remove the directory."""
# See bpo-30028 for details.
# Run the test as an external script, because it uses fork.
script_helper.assert_python_ok("-c", textwrap.dedent("""
import os
from test import support
with support.temp_cwd() as temp_path:
pid = os.fork()
if pid != 0:
# parent process (child has pid == 0)
# wait for the child to terminate
(pid, status) = os.waitpid(pid, 0)
if status != 0:
raise AssertionError("Child process failed with exit "
"status indication "
"0x{:x}.".format(status))
# Make sure that temp_path is still present. When the child
# process leaves the 'temp_cwd'-context, the __exit__()-
# method of the context must not remove the temporary
# directory.
if not os.path.isdir(temp_path):
raise AssertionError("Child removed temp_path.")
"""))
# Tests for change_cwd() # Tests for change_cwd()
def test_change_cwd(self): def test_change_cwd(self):
......
...@@ -774,6 +774,7 @@ Pedro Kroger ...@@ -774,6 +774,7 @@ Pedro Kroger
Hannu Krosing Hannu Krosing
Andrej Krpic Andrej Krpic
Ivan Krstić Ivan Krstić
Anselm Kruis
Steven Kryskalla Steven Kryskalla
Andrew Kuchling Andrew Kuchling
Dave Kuhlman Dave Kuhlman
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment