qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] tests: fix vhost-user-test race
@ 2015-11-26 18:30 marcandre.lureau
  2015-11-26 18:44 ` Michael S. Tsirkin
  0 siblings, 1 reply; 2+ messages in thread
From: marcandre.lureau @ 2015-11-26 18:30 UTC (permalink / raw)
  To: qemu-devel; +Cc: Marc-André Lureau, mst

From: Marc-André Lureau <marcandre.lureau@redhat.com>

vhost-user-tests uses a helper thread to dispatch the vhost-user servers
sources. However the CharDriverState is not thread-safe. Therefore, when
it's given to the thread, it shouldn't be maniuplated concurrently.

We dispatch cleaning the server in an idle source. By the end of the
test, we ensure not to leave anything behind by joining the thread and
finishing the sources dispatch.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 tests/vhost-user-test.c | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
index e4c36af..261f4b7 100644
--- a/tests/vhost-user-test.c
+++ b/tests/vhost-user-test.c
@@ -216,8 +216,7 @@ static void read_guest_mem(TestServer *s)
 
 static void *thread_function(void *data)
 {
-    GMainLoop *loop;
-    loop = g_main_loop_new(NULL, FALSE);
+    GMainLoop *loop = data;
     g_main_loop_run(loop);
     return NULL;
 }
@@ -389,7 +388,7 @@ static TestServer *test_server_new(const gchar *name)
     g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name,       \
                     (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
 
-static void test_server_free(TestServer *server)
+static gboolean _test_server_free(TestServer *server)
 {
     int i;
 
@@ -406,9 +405,15 @@ static void test_server_free(TestServer *server)
     unlink(server->socket_path);
     g_free(server->socket_path);
 
-
     g_free(server->chr_name);
     g_free(server);
+
+    return FALSE;
+}
+
+static void test_server_free(TestServer *server)
+{
+    g_idle_add((GSourceFunc)_test_server_free, server);
 }
 
 static void wait_for_log_fd(TestServer *s)
@@ -590,6 +595,8 @@ int main(int argc, char **argv)
     char *qemu_cmd = NULL;
     int ret;
     char template[] = "/tmp/vhost-test-XXXXXX";
+    GMainLoop *loop;
+    GThread *thread;
 
     g_test_init(&argc, &argv, NULL);
 
@@ -612,8 +619,9 @@ int main(int argc, char **argv)
 
     server = test_server_new("test");
 
+    loop = g_main_loop_new(NULL, FALSE);
     /* run the main loop thread so the chardev may operate */
-    g_thread_new(NULL, thread_function, NULL);
+    thread = g_thread_new(NULL, thread_function, loop);
 
     qemu_cmd = GET_QEMU_CMD(server);
 
@@ -632,6 +640,14 @@ int main(int argc, char **argv)
     /* cleanup */
     test_server_free(server);
 
+    /* finish the helper thread and dispatch pending sources */
+    g_main_loop_quit(loop);
+    g_thread_join(thread);
+    while (g_main_context_pending(NULL)) {
+        g_main_context_iteration (NULL, TRUE);
+    }
+    g_main_loop_unref(loop);
+
     ret = rmdir(tmpfs);
     if (ret != 0) {
         g_test_message("unable to rmdir: path (%s): %s\n",
-- 
2.5.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] tests: fix vhost-user-test race
  2015-11-26 18:30 [Qemu-devel] [PATCH] tests: fix vhost-user-test race marcandre.lureau
@ 2015-11-26 18:44 ` Michael S. Tsirkin
  0 siblings, 0 replies; 2+ messages in thread
From: Michael S. Tsirkin @ 2015-11-26 18:44 UTC (permalink / raw)
  To: marcandre.lureau; +Cc: qemu-devel

On Thu, Nov 26, 2015 at 07:30:55PM +0100, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> vhost-user-tests uses a helper thread to dispatch the vhost-user servers
> sources. However the CharDriverState is not thread-safe. Therefore, when
> it's given to the thread, it shouldn't be maniuplated concurrently.
> 
> We dispatch cleaning the server in an idle source. By the end of the
> test, we ensure not to leave anything behind by joining the thread and
> finishing the sources dispatch.
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Reviewed-by: Michael S. Tsirkin <mst@redhat.com>

Let's hope this fixes travis errors too.

> ---
>  tests/vhost-user-test.c | 26 +++++++++++++++++++++-----
>  1 file changed, 21 insertions(+), 5 deletions(-)
> 
> diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c
> index e4c36af..261f4b7 100644
> --- a/tests/vhost-user-test.c
> +++ b/tests/vhost-user-test.c
> @@ -216,8 +216,7 @@ static void read_guest_mem(TestServer *s)
>  
>  static void *thread_function(void *data)
>  {
> -    GMainLoop *loop;
> -    loop = g_main_loop_new(NULL, FALSE);
> +    GMainLoop *loop = data;
>      g_main_loop_run(loop);
>      return NULL;
>  }
> @@ -389,7 +388,7 @@ static TestServer *test_server_new(const gchar *name)
>      g_strdup_printf(QEMU_CMD extra, (mem), (mem), (root), (s)->chr_name,       \
>                      (s)->socket_path, (s)->chr_name, ##__VA_ARGS__)
>  
> -static void test_server_free(TestServer *server)
> +static gboolean _test_server_free(TestServer *server)
>  {
>      int i;
>  
> @@ -406,9 +405,15 @@ static void test_server_free(TestServer *server)
>      unlink(server->socket_path);
>      g_free(server->socket_path);
>  
> -
>      g_free(server->chr_name);
>      g_free(server);
> +
> +    return FALSE;
> +}
> +
> +static void test_server_free(TestServer *server)
> +{
> +    g_idle_add((GSourceFunc)_test_server_free, server);
>  }
>  
>  static void wait_for_log_fd(TestServer *s)
> @@ -590,6 +595,8 @@ int main(int argc, char **argv)
>      char *qemu_cmd = NULL;
>      int ret;
>      char template[] = "/tmp/vhost-test-XXXXXX";
> +    GMainLoop *loop;
> +    GThread *thread;
>  
>      g_test_init(&argc, &argv, NULL);
>  
> @@ -612,8 +619,9 @@ int main(int argc, char **argv)
>  
>      server = test_server_new("test");
>  
> +    loop = g_main_loop_new(NULL, FALSE);
>      /* run the main loop thread so the chardev may operate */
> -    g_thread_new(NULL, thread_function, NULL);
> +    thread = g_thread_new(NULL, thread_function, loop);
>  
>      qemu_cmd = GET_QEMU_CMD(server);
>  
> @@ -632,6 +640,14 @@ int main(int argc, char **argv)
>      /* cleanup */
>      test_server_free(server);
>  
> +    /* finish the helper thread and dispatch pending sources */
> +    g_main_loop_quit(loop);
> +    g_thread_join(thread);
> +    while (g_main_context_pending(NULL)) {
> +        g_main_context_iteration (NULL, TRUE);
> +    }
> +    g_main_loop_unref(loop);
> +
>      ret = rmdir(tmpfs);
>      if (ret != 0) {
>          g_test_message("unable to rmdir: path (%s): %s\n",
> -- 
> 2.5.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-11-26 18:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-26 18:30 [Qemu-devel] [PATCH] tests: fix vhost-user-test race marcandre.lureau
2015-11-26 18:44 ` Michael S. Tsirkin

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).