Skip to contentSkip to conversation

tests: Fix possible deadlock in qtest initialization

PATCH Superseded Marcel Apfelbaum 2 participants 4 messages Mar 11, 2014

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover tests: Fix possible deadlock in qtest initialization

V2 -> V3:
Addressed Eric Blake's review:
- Corrected a typo.
- Replace the commented Makefile part

V1 -> V2:
Addressed Stefan Hajnoczi's review:
- Dropped qtest_state_valid API
- Moved socket options in socket_accept.

'socket_accept' waits for Qemu to initialize its unix socket. If Qemu encounters an error during command line parsing, it can exit before initializing the communication channel.

Using a timeout for sockets fixes the issue, but is not enough. We need to know which test from check-qtest-* fails.

We tweak the Makefile to produce per-test output. The effect is that the output will grow, but we will know which qtest failed. This helps when an assert is made on other code and not in the test itself.

Marcel Apfelbaum (1):
tests/libqtest: Fix possible deadlock in qtest initialization

Stefan Hajnoczi (1):
tests: Tweak the Makefile to produce per-test output.

 tests/Makefile   |  2 +-
 tests/libqtest.c | 11 +++++++++--
 2 files changed, 10 insertions(+), 3 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v3 1/2 tests/libqtest: Fix possible deadlock in qtest initialization

'socket_accept' waits for Qemu to initialize its unix socket. If Qemu encounters an error during command line parsing, it can exit before initializing the communication channel.

Using a timeout for sockets fixes the issue.

---
 tests/libqtest.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
tests/libqtest.c +9 -2
index f587d36..f1ba254 100644
@@ -34,6 +34,7 @@
 #include "qapi/qmp/json-parser.h"
 
 #define MAX_IRQ 256
+#define SOCKET_TIMEOUT 5
 
 QTestState *global_qtest;
 
@@ -78,12 +79,16 @@ static int socket_accept(int sock)
     struct sockaddr_un addr;
     socklen_t addrlen;
     int ret;
+    struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
+                               .tv_usec = 0 };
+
+    setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
+               sizeof(timeout));
 
     addrlen = sizeof(addr);
     do {
         ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
     } while (ret == -1 && errno == EINTR);
-    g_assert_no_errno(ret);
     close(sock);
 
     return ret;
@@ -91,7 +96,7 @@ static int socket_accept(int sock)
 
 static void kill_qemu(QTestState *s)
 {
-    if (s->qemu_pid != -1) {
+    if (s && s->qemu_pid != -1) {
         kill(s->qemu_pid, SIGTERM);
         waitpid(s->qemu_pid, NULL, 0);
     }
@@ -153,6 +158,8 @@ QTestState *qtest_init(const char *extra_args)
     g_free(socket_path);
     g_free(qmp_socket_path);
 
+    g_assert(s->fd >= 0 && s->qmp_fd >= 0);
+
     s->rx = g_string_new("");
     for (i = 0; i < MAX_IRQ; i++) {
         s->irq_level[i] = false;
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH v3 2/2 tests: Tweak the Makefile to produce per-test output.

In reply to Marcel Apfelbaum

From: Stefan Hajnoczi <[email protected]>

The effect is that the output will grow, but we will know which qtest failed. This helps when an assert is made on other code and not in the test itself.

---
 tests/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/Makefile +1 -1
index b17d41e..cbf90e1 100644
@@ -273,7 +273,7 @@ check-help:
 	@echo "changed with variable GTESTER_OPTIONS."
 
 SPEED = quick
-GTESTER_OPTIONS = -k $(if $(V),--verbose,-q)
+GTESTER_OPTIONS = -k $(if $(V),--verbose,)
 GCOV_OPTIONS = -n $(if $(V),-f,)
 
 # gtester tests, possibly with verbose output
Signature · 1 line
1.8.3.1
Reviewed-by on the v3 series
#3 lore raw
In reply to Marcel Apfelbaum
Quoted 14 lines · Marcel Apfelbaum

V2 -> V3:
Addressed Eric Blake's review:
- Corrected a typo.
- Replace the commented Makefile part

V1 -> V2:
Addressed Stefan Hajnoczi's review:
- Dropped qtest_state_valid API
- Moved socket options in socket_accept.

'socket_accept' waits for Qemu to initialize its unix socket. If Qemu encounters an error during command line parsing, it can exit before initializing the communication channel.

Quoted 9 lines

Using a timeout for sockets fixes the issue, but is not enough. We need to know which test from check-qtest-* fails.

We tweak the Makefile to produce per-test output. The effect is that the output will grow, but we will know which qtest failed. This helps when an assert is made on other code and not in the test itself.

Series:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org