From 551e1043b4a5d1032a1599b7b24844c3cf2fd647 Mon Sep 17 00:00:00 2001 From: Teddy Reed Date: Sat, 6 Jan 2018 00:57:44 -0800 Subject: [PATCH] tests: Interrupt subprocesses of test_osqueryd (#4032) --- tools/tests/test_base.py | 14 ++++++++++++++ tools/tests/test_osqueryd.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/tests/test_base.py b/tools/tests/test_base.py index 152c5d17..136dd544 100644 --- a/tools/tests/test_base.py +++ b/tools/tests/test_base.py @@ -665,6 +665,20 @@ class QueryTester(ProcessGenerator, unittest.TestCase): print(" (%sms) rows: %d" % (duration_ms, len(result))) +class CleanChildProcesses: + # SO: 320232/ensuring-subprocesses-are-dead-on-exiting-python-program + def __enter__(self): + if os.name != "nt": + os.setpgrp() + def __exit__(self, type, value, traceback): + try: + if os.name != "nt": + os.killpg(0, signal.SIGINT) + except KeyboardInterrupt: + # SIGINT is delivered to this process and children. + pass + + def expectTrue(functional, interval=0.01, timeout=8): """Helper function to run a function with expected latency""" delay = 0 diff --git a/tools/tests/test_osqueryd.py b/tools/tests/test_osqueryd.py index b55685d1..f53117ab 100755 --- a/tools/tests/test_osqueryd.py +++ b/tools/tests/test_osqueryd.py @@ -249,4 +249,5 @@ class DaemonTests(test_base.ProcessGenerator, unittest.TestCase): daemon.kill() if __name__ == '__main__': - test_base.Tester().run() + with test_base.CleanChildProcesses(): + test_base.Tester().run()