Skip to contentSkip to conversation

tests/libqtest: fix cleanup of QEMU processes and add robustness

PATCH In review Daniel P. Berrangé 2 participants 5 messages May 13, 2022

lore
Share & tools
lore
#0 lore raw

v2 cover tests/libqtest: fix cleanup of QEMU processes and add robustness

This fixes the existing logic that was supposed to be cleaning up running QEMU processes when g_assert fires. It then further adds use of a Linux specific prctl as a safety net if the ABRT handler couldn't convince QEMU to quit with SIGTERM, or if the test program crashes with SEGV.

Daniel P. Berrangé (2):
tests/qtest: fix registration of ABRT handler for QEMU cleanup tests/qtest: use prctl(PR_SET_PDEATHSIG) as fallback to kill QEMU

 tests/qtest/libqtest.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 1/2 tests/qtest: fix registration of ABRT handler for QEMU cleanup

qtest_init registers a hook to cleanup the running QEMU process should g_assert() fire before qtest_quit is called. When the first hook is registered, it is supposed to triggere registration of the SIGABRT handler. Unfortunately the logic in hook_list_is_empty is inverted, so the SIGABRT handler never gets registered, unless 2 or more QEMU processes are run concurrently. This caused qtest to leak QEMU processes anytime g_assert triggers.

---
 tests/qtest/libqtest.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tests/qtest/libqtest.c +2 -2
index 228357f1ea..4a4697c0d1 100644
@@ -197,11 +197,11 @@ static bool hook_list_is_empty(GHookList *hook_list)
     GHook *hook = g_hook_first_valid(hook_list, TRUE);
 
     if (!hook) {
-        return false;
+        return true;
     }
 
     g_hook_unref(hook_list, hook);
-    return true;
+    return false;
 }
 
 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
Signature · 1 line
2.36.1
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] tests/qtest: fix registration of ABRT handler for QEMU cleanup

Quoted 7 lines · Daniel P. Berrangé

qtest_init registers a hook to cleanup the running QEMU process should g_assert() fire before qtest_quit is called. When the first hook is registered, it is supposed to triggere registration of the SIGABRT handler. Unfortunately the logic in hook_list_is_empty is inverted, so the SIGABRT handler never gets registered, unless 2 or more QEMU processes are run concurrently. This caused qtest to leak QEMU processes anytime g_assert triggers.

Ouch, thanks for spotting it!

Quoted 11 lines
---
  tests/qtest/libqtest.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
tests/qtest/libqtest.c +0 -0
index 228357f1ea..4a4697c0d1 100644
@@ -197,11 +197,11 @@ static bool hook_list_is_empty(GHookList *hook_list)
      GHook *hook = g_hook_first_valid(hook_list, TRUE);

tests/qtest/libqtest.c static bool hook_list_is_empty(GHookList *hook_list) · line 205

Quoted 10 lines
tests/qtest/libqtest.c +2 -2
 
      if (!hook) {
-        return false;
+        return true;
      }
 
      g_hook_unref(hook_list, hook);
-    return true;
+    return false;
  }
#3 lore raw

PATCH v2 2/2 tests/qtest: use prctl(PR_SET_PDEATHSIG) as fallback to kill QEMU

In reply to Daniel P. Berrangé

Although we register a ABRT handler to kill off QEMU when g_assert() triggers, we want an extra safety net. The QEMU process might be non-functional and thus not have responded to SIGTERM. The test script might also have crashed with SEGV, in which case the cleanup handlers won't ever run.

Using the Linux specific prctl(PR_SET_PDEATHSIG) syscall, we can ensure that QEMU gets sent SIGKILL as soon as the controlling qtest exits, if nothing else has correctly told it to quit.

Note, technically the death signal is sent when the *thread* that called fork() exits. IOW, if you are calling qtest_init() in one thread, letting that thread exit, and then expecting to run qtest_quit() in a different thread, things are not going to work out. Fortunately that is not a scenario that exists in qtests, as pairs of qtest_init and qtest_quit are always called from the same thread.

---
 tests/qtest/libqtest.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tests/qtest/libqtest.c +17 -0
index 4a4697c0d1..2e49618454 100644
@@ -19,6 +19,9 @@
 #include <sys/socket.h>
 #include <sys/wait.h>
 #include <sys/un.h>
+#ifdef __linux__
+#include <sys/prctl.h>
+#endif /* __linux__ */
 
 #include "libqtest.h"
 #include "libqmp.h"
@@ -301,6 +304,20 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
     s->expected_status = 0;
     s->qemu_pid = fork();
     if (s->qemu_pid == 0) {
+#ifdef __linux__
+        /*
+         * Although we register a ABRT handler to kill off QEMU
+         * when g_assert() triggers, we want an extra safety
+         * net. The QEMU process might be non-functional and
+         * thus not have responded to SIGTERM. The test script
+         * might also have crashed with SEGV, in which case the
+         * cleanup handlers won't ever run.
+         *
+         * This PR_SET_PDEATHSIG setup will ensure any remaining
+         * QEMU will get terminated with SIGKILL in these cases.
+         */
+        prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
+#endif /* __linux__ */
         if (!g_setenv("QEMU_AUDIO_DRV", "none", true)) {
             exit(1);
         }
Signature · 1 line
2.36.1
Reviewed-by on v2 2/2
#4 lore raw

Re: [PATCH v2 2/2] tests/qtest: use prctl(PR_SET_PDEATHSIG) as fallback to kill QEMU

Quoted 48 lines · Daniel P. Berrangé

Although we register a ABRT handler to kill off QEMU when g_assert() triggers, we want an extra safety net. The QEMU process might be non-functional and thus not have responded to SIGTERM. The test script might also have crashed with SEGV, in which case the cleanup handlers won't ever run.

Using the Linux specific prctl(PR_SET_PDEATHSIG) syscall, we can ensure that QEMU gets sent SIGKILL as soon as the controlling qtest exits, if nothing else has correctly told it to quit.

Note, technically the death signal is sent when the *thread* that called fork() exits. IOW, if you are calling qtest_init() in one thread, letting that thread exit, and then expecting to run qtest_quit() in a different thread, things are not going to work out. Fortunately that is not a scenario that exists in qtests, as pairs of qtest_init and qtest_quit are always called from the same thread.

---
  tests/qtest/libqtest.c | 17 +++++++++++++++++
  1 file changed, 17 insertions(+)
tests/qtest/libqtest.c +10 -0
index 4a4697c0d1..2e49618454 100644
@@ -19,6 +19,9 @@
  #include <sys/socket.h>
  #include <sys/wait.h>
  #include <sys/un.h>
+#ifdef __linux__
+#include <sys/prctl.h>
+#endif /* __linux__ */
 
  #include "libqtest.h"
  #include "libqmp.h"
@@ -301,6 +304,20 @@ QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
      s->expected_status = 0;
      s->qemu_pid = fork();
      if (s->qemu_pid == 0) {
+#ifdef __linux__
+        /*
+         * Although we register a ABRT handler to kill off QEMU
+         * when g_assert() triggers, we want an extra safety
+         * net. The QEMU process might be non-functional and
+         * thus not have responded to SIGTERM. The test script
+         * might also have crashed with SEGV, in which case the

tests/qtest/libqtest.c QTestState *qtest_init_without_qmp_handshake(const char *extra_args) · line 323

Quoted 10 lines
tests/qtest/libqtest.c +7 -0
+         * cleanup handlers won't ever run.
+         *
+         * This PR_SET_PDEATHSIG setup will ensure any remaining
+         * QEMU will get terminated with SIGKILL in these cases.
+         */
+        prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
+#endif /* __linux__ */
          if (!g_setenv("QEMU_AUDIO_DRV", "none", true)) {
              exit(1);
          }