From: Anton Yuzhaninov Date: Tue, 16 Oct 2018 15:34:24 +0000 (-0400) Subject: [functional test] Fix races in shutdown_process X-Git-Tag: 1.8.2~195^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=33016c62b77663561b02c6107285c3689f52208e;p=thirdparty%2Frspamd.git [functional test] Fix races in shutdown_process Currently functional test sometimes fails with and error: Teardown failed: NoSuchProcess: psutil.NoSuchProcess process no longer exists (pid=2259) Rewrite process termination - there is no need to send signals in a loop many times. --- diff --git a/test/functional/lib/rspamd.py b/test/functional/lib/rspamd.py index e0454e3479..5419a115e1 100644 --- a/test/functional/lib/rspamd.py +++ b/test/functional/lib/rspamd.py @@ -170,34 +170,31 @@ def update_dictionary(a, b): a.update(b) return a -def shutdown_process(process): - i = 0 - while i < 100: - try: - os.kill(process.pid, signal.SIGTERM) - except OSError as e: - assert e.errno == errno.ESRCH - return - if process.status() == psutil.STATUS_ZOMBIE: - return +TERM_TIMEOUT = 10 # wait after sending a SIGTERM signal +KILL_WAIT = 20 # additional wait after sending a SIGKILL signal - i += 1 - time.sleep(0.1) +def shutdown_process(process): + # send SIGTERM + process.terminate() - while i < 200: + try: + process.wait(TERM_TIMEOUT) + return + except psutil.TimeoutExpired: + logger.info( "PID {} is not termianated in {} seconds, sending SIGKILL..." % + (process.pid, TERM_TIMEOUT)) try: - os.kill(process.pid, signal.SIGKILL) - except OSError as e: - assert e.errno == errno.ESRCH + # send SIGKILL + process.kill() + except psutil.NoSuchProcess: + # process exited just befor we tried to kill return - if process.status() == psutil.STATUS_ZOMBIE: - return - - i += 1 - time.sleep(0.1) - assert False, "Failed to shutdown process %d (%s)" % (process.pid, process.name()) + try: + process.wait(KILL_WAIT) + except psutil.TimeoutExpired: + raise RuntimeError("Failed to shutdown process %d (%s)" % (process.pid, process.name())) def shutdown_process_with_children(pid):