]> source.dussan.org Git - rspamd.git/commitdiff
[functional test] Fix races in shutdown_process
authorAnton Yuzhaninov <citrin+git@citrin.ru>
Tue, 16 Oct 2018 15:34:24 +0000 (11:34 -0400)
committerAnton Yuzhaninov <citrin+git@citrin.ru>
Tue, 16 Oct 2018 15:37:59 +0000 (11:37 -0400)
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.

test/functional/lib/rspamd.py

index e0454e347924030b49ff23c6a9c7855662fcc302..5419a115e111d6cfbfbcfa88fd0449ef93996311 100644 (file)
@@ -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):