Index: tools/telemetry/telemetry/util/global_hooks.py |
diff --git a/tools/telemetry/telemetry/util/global_hooks.py b/tools/telemetry/telemetry/util/global_hooks.py |
index c7d559922cfdf5d4c9be15f97fc45c4937fa06b6..4d50417268507a3bd0043e7039ab9d38b4f5faa6 100644 |
--- a/tools/telemetry/telemetry/util/global_hooks.py |
+++ b/tools/telemetry/telemetry/util/global_hooks.py |
@@ -4,10 +4,12 @@ |
"""Hooks that apply globally to all scripts that import or use Telemetry.""" |
+import atexit |
import os |
import signal |
import sys |
+from telemetry.core import platform |
from telemetry.core import util |
from telemetry.util import exception_formatter |
@@ -18,6 +20,7 @@ def InstallHooks(): |
InstallUnhandledExceptionFormatter() |
InstallStackDumpOnSigusr1() |
InstallTerminationHook() |
+ InstallAtExitHook() |
def RemoveAllStalePycFiles(base_dir): |
@@ -71,3 +74,24 @@ def InstallTerminationHook(): |
exception_formatter.PrintFormattedFrame(stack_frame, exception_string) |
sys.exit(-1) |
signal.signal(signal.SIGTERM, PrintStackAndExit) |
+ |
+ |
+def InstallAtExitHook(): |
+ """Ensure all subprocesses are killed. |
+ |
+ Subprocesses should never outlive Telemetry. If they do, they can cause |
+ hangs on the builbots. |
+ """ |
+ # TODO(tonyg): Find a way to do something similar on Windows. |
+ if platform.GetHostPlatform().GetOSName() == 'win': |
+ return |
+ |
+ # Create new process group and become its leader. |
+ os.setpgrp() |
+ |
+ # At exit, terminate everything in the group. |
+ def KillGroup(): |
+ # Ignore the TERM we're about to get. |
+ signal.signal(signal.SIGTERM, signal.SIG_IGN) |
+ os.killpg(0, signal.SIGTERM) |
+ atexit.register(KillGroup) |