def test_kill_task_terminate_with_sigkill(self): task_id = tu.get_random_task_id() stdout_name = tu.ensure_directory('build/stdout.{}'.format(task_id)) stderr_name = tu.ensure_directory('build/stderr.{}'.format(task_id)) tu.redirect_stdout_to_file(stdout_name) tu.redirect_stderr_to_file(stderr_name) try: command = "trap '' TERM SIGTERM; sleep 200" process = cs.launch_process(command, {}) shutdown_grace_period_ms = 1000 group_id = cs.find_process_group(process.pid) self.assertGreater(len(find_process_ids_in_group(group_id)), 0) cs.kill_process(process, shutdown_grace_period_ms) # await process termination for i in range(1, 10 * shutdown_grace_period_ms): if process.poll() is None: time.sleep(0.01) if process.poll() is None: process.kill() self.assertTrue( ((-1 * signal.SIGKILL) == process.poll()) or ((128 + signal.SIGKILL) == process.poll()), 'Process exited with code {}'.format(process.poll())) self.assertEqual(len(find_process_ids_in_group(group_id)), 0) finally: tu.cleanup_output(stdout_name, stderr_name)
def test_kill_task_terminate_with_sigterm(self): task_id = tu.get_random_task_id() stdout_name = tu.ensure_directory('build/stdout.{}'.format(task_id)) stderr_name = tu.ensure_directory('build/stderr.{}'.format(task_id)) tu.redirect_stdout_to_file(stdout_name) tu.redirect_stderr_to_file(stderr_name) try: command = "bash -c 'function handle_term { echo GOT TERM; }; trap handle_term SIGTERM TERM; sleep 200'" process = cs.launch_process(command, {}) shutdown_grace_period_ms = 1000 group_id = cs.find_process_group(process.pid) self.assertGreater(len(find_process_ids_in_group(group_id)), 0) cs.kill_process(process, shutdown_grace_period_ms) # await process termination for i in range(1, 10 * shutdown_grace_period_ms): if process.poll() is None: time.sleep(0.01) if process.poll() is None: process.kill() self.assertTrue( ((-1 * signal.SIGTERM) == process.poll()) or ((128 + signal.SIGTERM) == process.poll()), 'Process exited with code {}'.format(process.poll())) self.assertEqual(0, len(find_process_ids_in_group(group_id))) with open(stdout_name) as f: file_contents = f.read() self.assertTrue('GOT TERM' in file_contents) finally: tu.cleanup_output(stdout_name, stderr_name)
def process_stop_signal(): stop_signal.wait() # wait indefinitely for the stop_signal to be set if cs.is_process_running(process): logging.info( 'Executor has been instructed to terminate running task') cs.kill_process(process, shutdown_grace_period_ms)