]> granicus.if.org Git - python/commitdiff
bpo-30028: make test.support.temp_cwd() fork-safe (GH-1066)
authorAnselm Kruis <a.kruis@science-computing.de>
Fri, 23 Feb 2018 01:37:38 +0000 (02:37 +0100)
committerGregory P. Smith <greg@krypto.org>
Fri, 23 Feb 2018 01:37:38 +0000 (17:37 -0800)
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.

Lib/test/support/__init__.py
Lib/test/test_support.py
Misc/ACKS

index 6c9e31a22c10ac6274a3f523a8a636703b4b74f3..b4269f4a5b7043ce087e660afcc33f377735a1e9 100644 (file)
@@ -948,10 +948,14 @@ def temp_dir(path=None, quiet=False):
             warnings.warn(f'tests may fail, unable to create '
                           f'temporary directory {path!r}: {exc}',
                           RuntimeWarning, stacklevel=3)
+    if dir_created:
+        pid = os.getpid()
     try:
         yield path
     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)
 
 @contextlib.contextmanager
index e06f7b8e9952b8dba149af8b909500975010f310..36d5f849e1ad838f0ad4314ea76ef8fe57d2d586 100644 (file)
@@ -9,9 +9,11 @@ import stat
 import subprocess
 import sys
 import tempfile
+import textwrap
 import time
 import unittest
 from test import support
+from test.support import script_helper
 
 TESTFN = support.TESTFN
 
@@ -165,6 +167,33 @@ class TestSupport(unittest.TestCase):
                                         f'temporary directory {path!r}: '),
                         warn)
 
+    @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(f"Child process failed with exit "
+                                             f"status indication 0x{status:x}.")
+
+                    # 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()
 
     def test_change_cwd(self):
index 2eddc56d34d3d3e6be55308379813df334c54741..dee022f2ff0a64b9c7abf1dcd1d3ec5ad87a6423 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -857,6 +857,7 @@ Pedro Kroger
 Hannu Krosing
 Andrej Krpic
 Ivan Krstić
+Anselm Kruis
 Steven Kryskalla
 Andrew Kuchling
 Dave Kuhlman