From: "Daniel P. Berrangé" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>,
"Thomas Huth" <thuth@redhat.com>,
"Daniel P. Berrangé" <berrange@redhat.com>
Subject: [PATCH] tests/qtest: kill off QEMU with SIGKILL when qtest exits abnormally
Date: Fri, 13 May 2022 15:37:43 +0100 [thread overview]
Message-ID: <20220513143743.198390-1-berrange@redhat.com> (raw)
If a qtest program exits without calling qtest_quit(), then the
QEMU emulator process will remain running in the background forever.
Unfortunately this scenario is exactly what will happen when a
g_assert() check triggers an abort().
Prior to switching to use of 'meson test', this problem would
cause tap-driver.pl to hang forever. It was waiting for its
STDIN to report EOF, but that would never happen due to the
ophaned QEMU emulator processes keeping the pipe open forever.
Fortunately this doesn't happen with meson, but it is still
desirable to not leak QEMU processes when asserts fire.
Using the Linux specific prctl(PR_SET_PDEATHSIG) syscall, we
can ensure that QEMU gets sent SIGKILL as soon as the controlling
qtest exits, despite being daemonized.
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.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
tests/qtest/libqtest.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 228357f1ea..553e82e492 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -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,21 @@ 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__
+ /*
+ * If the controlling qtest process exits without calling
+ * the qtest_quit() method, the QEMU processes will get
+ * orphaned and remain running forever in the background.
+ *
+ * Missing qtest_quit() calls are, unfortunately, exactly
+ * what happen when a g_assert() check triggers abort() in
+ * a failing test scenario.
+ *
+ * This PR_SET_PDEATHSIG setup will ensure QEMU will
+ * get terminated with SIGKILL.
+ */
+ prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
+#endif /* __linux__ */
if (!g_setenv("QEMU_AUDIO_DRV", "none", true)) {
exit(1);
}
--
2.36.1
next reply other threads:[~2022-05-13 14:44 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-13 14:37 Daniel P. Berrangé [this message]
2022-05-13 14:47 ` [PATCH] tests/qtest: kill off QEMU with SIGKILL when qtest exits abnormally Thomas Huth
2022-05-13 14:49 ` Thomas Huth
2022-05-13 15:40 ` Daniel P. Berrangé
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220513143743.198390-1-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=lvivier@redhat.com \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=thuth@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).