diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py index fda4ca1ebca094..d716b83e235472 100644 --- a/Lib/test/libregrtest/runtest.py +++ b/Lib/test/libregrtest/runtest.py @@ -143,6 +143,10 @@ def runtest(ns, test): runtest.stringio = None +def post_test_cleanup(): + support.reap_children() + + def runtest_inner(ns, test, display_failure=True): support.unload(test) @@ -170,6 +174,7 @@ def test_runner(): if ns.huntrleaks: refleak = dash_R(the_module, test, test_runner, ns.huntrleaks) test_time = time.time() - start_time + post_test_cleanup() except support.ResourceDenied as msg: if not ns.quiet and not ns.pgo: print(test, "skipped --", msg, flush=True) diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 7a4b7eb9176ab9..49c4a53c052dc2 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -1560,6 +1560,10 @@ def run_child(self, child, terminal_input): self.fail("got %d lines in pipe but expected 2, child output was:\n%s" % (len(lines), child_output)) os.close(fd) + + # Wait until the child process completes + os.waitpid(pid, 0) + return lines def check_input_tty(self, prompt, terminal_input, stdio_encoding=None): diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index d0cf04b0cb67ca..710756bde64c04 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -78,7 +78,6 @@ def setUp(self): def tearDown(self): self._warnings_manager.__exit__(None, None, None) - def nameCheck(self, name, dir, pre, suf): (ndir, nbase) = os.path.split(name) npre = nbase[:len(pre)] @@ -184,12 +183,15 @@ def test_process_awareness(self): try: pid = os.fork() if not pid: + # child process os.close(read_fd) os.write(write_fd, next(self.r).encode("ascii")) os.close(write_fd) # bypass the normal exit handlers- leave those to # the parent. os._exit(0) + + # parent process parent_value = next(self.r) child_value = os.read(read_fd, len(parent_value)).decode("ascii") finally: @@ -200,6 +202,10 @@ def test_process_awareness(self): os.kill(pid, signal.SIGKILL) except OSError: pass + + # Read the process exit status to avoid zombie process + os.waitpid(pid, 0) + os.close(read_fd) os.close(write_fd) self.assertNotEqual(child_value, parent_value)