]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: give the container time to properly shut down on exception
authorFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 30 Nov 2022 15:13:19 +0000 (16:13 +0100)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Wed, 30 Nov 2022 15:13:19 +0000 (16:13 +0100)
Otherwise the `terminate()` method sends SIGKILL rather quickly (~0.3s),
which then leaves a dangling scope on the host system, breaking further
test executions.

test/test-shutdown.py

index e181f976bea580e8fb5ed0f84b568c8a1e6dd257..8cbcf1df42521823392e995c2ebc2e425b814350 100755 (executable)
@@ -5,6 +5,7 @@
 import argparse
 import logging
 import sys
+import time
 
 import pexpect
 
@@ -90,7 +91,23 @@ def run(args):
     except Exception as e:
         logger.error(e)
         logger.info("killing child pid %d", console.pid)
-        console.terminate(force=True)
+        # We can't use console.terminate(force=True) right away, since
+        # the internal delay between sending a signal and checking the process
+        # is just 0.1s [0], which means we'd get SIGKILLed pretty quickly.
+        # Let's send SIGHUP/SIGINT first, wait a bit, and then follow-up with
+        # SIGHUP/SIGINT/SIGKILL if the process is still alive.
+        # [0] https://github.com/pexpect/pexpect/blob/acb017a97332c19a9295660fe87316926a8adc55/pexpect/spawnbase.py#L71
+        console.terminate()
+        for _ in range(10):
+            if not console.isalive():
+                break
+
+            time.sleep(1)
+        else:
+            # We haven't exited the loop early, so check if the process is
+            # still alive - if so, force-kill it.
+            if console.isalive():
+                console.terminate(force=True)
 
     return ret