mtest: terminate a test via SIGTERM first then (if needed) via SIGKILL

pull/6517/head
Camilo Celis Guzman 5 years ago committed by Jussi Pakkanen
parent 4d6faf6a13
commit 18373cba74
  1. 6
      docs/markdown/snippets/termination_signal_for_tests.md
  2. 27
      mesonbuild/mtest.py

@ -0,0 +1,6 @@
## Changed the signal used to terminate a test process (group)
A test process (group) is now terminated via SIGTERM instead of SIGKILL
allowing the signal to be handled. However, it is now the responsibility of
the custom signal handler (if any) to ensure that any process spawned by the
top-level test processes is correctly killed.

@ -578,14 +578,27 @@ class SingleTestRunner:
if is_windows():
subprocess.run(['taskkill', '/F', '/T', '/PID', str(p.pid)])
else:
def _send_signal_to_process_group(pgid : int, signum : int):
""" sends a signal to a process group """
try:
os.killpg(pgid, signum) # type: ignore
except ProcessLookupError:
# Sometimes (e.g. with Wine) this happens.
# There's nothing we can do (maybe the process
# already died) so carry on.
pass
# Send a termination signal to the process group that setsid()
# created - giving it a chance to perform any cleanup.
_send_signal_to_process_group(p.pid, signal.SIGTERM)
# Make sure the termination signal actually kills the process
# group, otherwise retry with a SIGKILL.
try:
# Kill the process group that setsid() created.
os.killpg(p.pid, signal.SIGKILL) # type: ignore
except ProcessLookupError:
# Sometimes (e.g. with Wine) this happens.
# There's nothing we can do (maybe the process
# already died) so carry on.
pass
p.communicate(timeout=0.5)
except subprocess.TimeoutExpired:
_send_signal_to_process_group(p.pid, signal.SIGKILL)
try:
p.communicate(timeout=1)
except subprocess.TimeoutExpired:

Loading…
Cancel
Save