Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,6 @@ def fork_new_process_for_each_test(func: Callable[_P, None]) -> Callable[_P, Non

@functools.wraps(func)
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
# Make the process the leader of its own process group
# to avoid sending SIGTERM to the parent process
os.setpgrp()
from _pytest.outcomes import Skipped

# Create a unique temporary file to store exception info from child
Expand All @@ -890,6 +887,9 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
pid = os.fork()
print(f"Fork a new process to run a test {pid}")
if pid == 0:
# Make the child process the leader of its own process group
# to avoid sending SIGTERM to the parent process
os.setpgrp()
# Parent process responsible for deleting, don't delete
# in child.
delete_after.pop_all()
Expand Down Expand Up @@ -929,14 +929,12 @@ def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
else:
os._exit(0)
else:
pgid = os.getpgid(pid)
# After setpgrp(), the child's pgid equals its pid
pgid = pid
_pid, _exitcode = os.waitpid(pid, 0)
# ignore SIGTERM signal itself
old_signal_handler = signal.signal(signal.SIGTERM, signal.SIG_IGN)
# kill all child processes
os.killpg(pgid, signal.SIGTERM)
# restore the signal handler
signal.signal(signal.SIGTERM, old_signal_handler)
# kill all child processes - but they may already have exited cleanly
with contextlib.suppress(ProcessLookupError):
os.killpg(pgid, signal.SIGTERM)
if _exitcode != 0:
# Try to read the exception from the child process
exc_info = {}
Expand Down