From: Mikhail Galanin Date: Wed, 22 Aug 2018 15:00:30 +0000 (+0100) Subject: [Test] Improved process termination in tests X-Git-Tag: 1.8.0~216^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=170a4c4a04208aad1249494321be64e1f7b6c71b;p=thirdparty%2Frspamd.git [Test] Improved process termination in tests --- diff --git a/test/functional/lib/rspamd.py b/test/functional/lib/rspamd.py index 2010f41273..97ab516c15 100644 --- a/test/functional/lib/rspamd.py +++ b/test/functional/lib/rspamd.py @@ -8,11 +8,12 @@ import re import shutil import signal import socket -import string +import errno import sys import tempfile import time from robot.libraries.BuiltIn import BuiltIn +from robot.api import logger if sys.version_info > (3,): long = int @@ -167,32 +168,47 @@ def update_dictionary(a, b): a.update(b) return a -def shutdown_process(pid): +def shutdown_process(process): i = 0 while i < 100: try: - os.kill(pid, signal.SIGTERM) + os.kill(process.pid, signal.SIGTERM) except OSError as e: - assert e.errno == 3 + assert e.errno == errno.ESRCH return + + if process.status() == psutil.STATUS_ZOMBIE: + return + i += 1 time.sleep(0.1) + while i < 200: try: - os.kill(pid, signal.SIGKILL) + os.kill(process.pid, signal.SIGKILL) except OSError as e: - assert e.errno == 3 + assert e.errno == errno.ESRCH return + + if process.status() == psutil.STATUS_ZOMBIE: + return + i += 1 time.sleep(0.1) - assert False, "Failed to shutdown process %s" % pid + assert False, "Failed to shutdown process %d (%s)" % (process.pid, process.name()) + def shutdown_process_with_children(pid): pid = int(pid) - children = psutil.Process(pid=pid).children(recursive=False) - shutdown_process(pid) + try: + process = psutil.Process(pid=pid) + except psutil.NoSuchProcess: + return + children = process.children(recursive=False) + shutdown_process(process) for child in children: try: - shutdown_process(child.pid) + shutdown_process(child) except: pass +