Skip to contentSkip to conversation

Prevent vhost-user-blk-test hang

PATCH Superseded Raphael Norwitz 2 participants 2 messages Sep 6, 2021 – Sep 7, 2021

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Prevent vhost-user-blk-test hang

In the vhost-user-blk-test, as of now there is nothing stoping vhost-user-blk in QEMU writing to the socket right after forking off the storage daemon before it has a chance to come up properly, leaving the test hanging forever. This intermittently hanging test has caused QEMU automation failures reported multiple times on the mailing list [1].

This change makes the storage-daemon notify the vhost-user-blk-test that it is fully initialized and ready to handle client connections by creating a pidfile on initialiation. This ensures that the storage-daemon backend won't miss vhost-user messages and thereby resolves the hang.

[1] https://lore.kernel.org/qemu-devel/CAFEAcA8kYpz9LiPNxnWJAPSjc=nv532bEdyfynaBeMeohqBp3A@mail.gmail.com/

---
 tests/qtest/vhost-user-blk-test.c | 33 ++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
tests/qtest/vhost-user-blk-test.c +32 -1
index 6f108a1b62..78140e6f28 100644
@@ -24,6 +24,9 @@
 #define TEST_IMAGE_SIZE         (64 * 1024 * 1024)
 #define QVIRTIO_BLK_TIMEOUT_US  (30 * 1000 * 1000)
 #define PCI_SLOT_HP             0x06
+#define PIDFILE_RETRIES         5
+
+const char *pidfile_format = "/tmp/daemon-%d";
 
 typedef struct {
     pid_t pid;
@@ -885,7 +888,8 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
                                  int num_queues)
 {
     const char *vhost_user_blk_bin = qtest_qemu_storage_daemon_binary();
-    int i;
+    int i, err, retries;
+    char *daemon_pidfile_path;
     gchar *img_path;
     GString *storage_daemon_command = g_string_new(NULL);
     QemuStorageDaemonState *qsd;
@@ -898,6 +902,12 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
             " -object memory-backend-memfd,id=mem,size=256M,share=on "
             " -M memory-backend=mem -m 256M ");
 
+    err = asprintf(&daemon_pidfile_path, pidfile_format, getpid());
+    if (err == -1) {
+        fprintf(stderr, "Failed to format storage-daemon pidfile name %m");
+        abort();
+    }
+
     for (i = 0; i < vus_instances; i++) {
         int fd;
         char *sock_path = create_listen_socket(&fd);
@@ -914,6 +924,9 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
                                i + 1, sock_path);
     }
 
+    g_string_append_printf(storage_daemon_command, "--pidfile %s",
+                           daemon_pidfile_path);
+
     g_test_message("starting vhost-user backend: %s",
                    storage_daemon_command->str);
     pid_t pid = fork();
@@ -930,7 +943,25 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
         execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, NULL);
         exit(1);
     }
+
+    /*
+     * Ensure the storage-daemon has come up properly before allowing the
+     * test to proceed.
+     */
+    retries = 0;
+    while (access(daemon_pidfile_path, F_OK) != 0) {
+        if (retries > PIDFILE_RETRIES) {
+            fprintf(stderr, "The storage-daemon failed to come up after %d "
+                    "seconds - killing the test", PIDFILE_RETRIES);
+            abort();
+        }
+
+        retries++;
+        usleep(1000);
+    }
+
     g_string_free(storage_daemon_command, true);
+    free(daemon_pidfile_path);
 
     qsd = g_new(QemuStorageDaemonState, 1);
     qsd->pid = pid;
Signature · 1 line
2.20.1
#1 lore raw
Quoted 23 lines · Raphael Norwitz

In the vhost-user-blk-test, as of now there is nothing stoping vhost-user-blk in QEMU writing to the socket right after forking off the storage daemon before it has a chance to come up properly, leaving the test hanging forever. This intermittently hanging test has caused QEMU automation failures reported multiple times on the mailing list [1].

This change makes the storage-daemon notify the vhost-user-blk-test that it is fully initialized and ready to handle client connections by creating a pidfile on initialiation. This ensures that the storage-daemon backend won't miss vhost-user messages and thereby resolves the hang.

[1] https://lore.kernel.org/qemu-devel/CAFEAcA8kYpz9LiPNxnWJAPSjc=nv532bEdyfynaBeMeohqBp3A@mail.gmail.com/

---
 tests/qtest/vhost-user-blk-test.c | 33 ++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)
tests/qtest/vhost-user-blk-test.c +0 -0
index 6f108a1b62..78140e6f28 100644

tests/qtest/vhost-user-blk-test.c line 29

Quoted 6 lines
tests/qtest/vhost-user-blk-test.c +3 -0
@@ -24,6 +24,9 @@
 #define TEST_IMAGE_SIZE         (64 * 1024 * 1024)
 #define QVIRTIO_BLK_TIMEOUT_US  (30 * 1000 * 1000)
 #define PCI_SLOT_HP             0x06
+#define PIDFILE_RETRIES         5
+
+const char *pidfile_format = "/tmp/daemon-%d";

Why is this not static? In fact...

Quoted 14 lines
tests/qtest/vhost-user-blk-test.c +2 -1
 typedef struct {
     pid_t pid;
@@ -885,7 +888,8 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
                                  int num_queues)
 {
     const char *vhost_user_blk_bin = qtest_qemu_storage_daemon_binary();
-    int i;
+    int i, err, retries;
+    char *daemon_pidfile_path;
     gchar *img_path;
     GString *storage_daemon_command = g_string_new(NULL);
     QemuStorageDaemonState *qsd;

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 905

Quoted 4 lines
tests/qtest/vhost-user-blk-test.c +1 -0
@@ -898,6 +902,12 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
             " -object memory-backend-memfd,id=mem,size=256M,share=on "
             " -M memory-backend=mem -m 256M ");
 
+    err = asprintf(&daemon_pidfile_path, pidfile_format, getpid());

...action at a distance makes it harder for gcc to warn about bad formats. I'd just inline "/tmp/daemon-%d" here in the asprintf call, and drop pidfile_format altogether.

Why are we using bare asprintf instead of glib's g_strdup_printf?

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 907

Quoted 2 lines
tests/qtest/vhost-user-blk-test.c +2 -0
+    if (err == -1) {
+        fprintf(stderr, "Failed to format storage-daemon pidfile name %m");

%m in printf is a glibc-ism; not portable to non-Linux. Do we care?

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 908

Quoted 1 line
tests/qtest/vhost-user-blk-test.c +1 -0
+        abort();

Rather than directly abort, since this is a glib test runner, is there a glib function we should be using? For example, using g_assert_cmpint((err, !=, -1)?

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 927

Quoted 10 lines
tests/qtest/vhost-user-blk-test.c +3 -0
+    }
+
     for (i = 0; i < vus_instances; i++) {
         int fd;
         char *sock_path = create_listen_socket(&fd);
@@ -914,6 +924,9 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
                                i + 1, sock_path);
     }
 
+    g_string_append_printf(storage_daemon_command, "--pidfile %s",

Missing a space on the tail end if there are more arguments to append to the command line.

Quoted 10 lines
tests/qtest/vhost-user-blk-test.c +3 -0
+                           daemon_pidfile_path);
+
     g_test_message("starting vhost-user backend: %s",
                    storage_daemon_command->str);
     pid_t pid = fork();
@@ -930,7 +943,25 @@ static void start_vhost_user_blk(GString *cmd_line, int vus_instances,
         execlp("/bin/sh", "sh", "-c", storage_daemon_command->str, NULL);
         exit(1);
     }
+

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 956

Quoted 10 lines
tests/qtest/vhost-user-blk-test.c +10 -0
+    /*
+     * Ensure the storage-daemon has come up properly before allowing the
+     * test to proceed.
+     */
+    retries = 0;
+    while (access(daemon_pidfile_path, F_OK) != 0) {
+        if (retries > PIDFILE_RETRIES) {
+            fprintf(stderr, "The storage-daemon failed to come up after %d "
+                    "seconds - killing the test", PIDFILE_RETRIES);
+            abort();

Again, would some form of g_assert*() be better than bare abort?

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 960

Quoted 4 lines
tests/qtest/vhost-user-blk-test.c +4 -0
+        }
+
+        retries++;
+        usleep(1000);

We're inconsistent on whether qtest files use bare usleep or g_usleep.

tests/qtest/vhost-user-blk-test.c static void start_vhost_user_blk(GString *cmd_line, int vus_instances, · line 964

Quoted 4 lines
tests/qtest/vhost-user-blk-test.c +3 -0
+    }
+
     g_string_free(storage_daemon_command, true);
+    free(daemon_pidfile_path);

Do you want to unlink() the file (if it exists)?

Quoted 6 lines
tests/qtest/vhost-user-blk-test.c +0 -0
     qsd = g_new(QemuStorageDaemonState, 1);
     qsd->pid = pid;
Signature · 1 line
2.20.1
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org