From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41290) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4E7a-0002R0-Hw for qemu-devel@nongnu.org; Wed, 02 Dec 2015 15:35:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a4E7Z-0002UV-GA for qemu-devel@nongnu.org; Wed, 02 Dec 2015 15:35:26 -0500 Received: from mx1.redhat.com ([209.132.183.28]:42363) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a4E7Z-0002UR-8h for qemu-devel@nongnu.org; Wed, 02 Dec 2015 15:35:25 -0500 Date: Wed, 2 Dec 2015 22:35:21 +0200 From: "Michael S. Tsirkin" Message-ID: <1449088478-21099-2-git-send-email-mst@redhat.com> References: <1449088478-21099-1-git-send-email-mst@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline In-Reply-To: <1449088478-21099-1-git-send-email-mst@redhat.com> Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 1/9] vhost-user-test: fix chardriver race List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Marcel Apfelbaum , Peter Maydell , Yuanhan Liu , Paolo Bonzini , =?us-ascii?B?PT9VVEYtOD9xP01hcmMtQW5kcj1DMz1BOT0yMEx1cmVhdT89?= From: Marc-Andr=E9 Lureau 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 manipulated 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=E9 Lureau Reviewed-by: Michael S. Tsirkin Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- 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) =20 static void *thread_function(void *data) { - GMainLoop *loop; - loop =3D g_main_loop_new(NULL, FALSE); + GMainLoop *loop =3D 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__) =20 -static void test_server_free(TestServer *server) +static gboolean _test_server_free(TestServer *server) { int i; =20 @@ -406,9 +405,15 @@ static void test_server_free(TestServer *server) unlink(server->socket_path); g_free(server->socket_path); =20 - 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); } =20 static void wait_for_log_fd(TestServer *s) @@ -590,6 +595,8 @@ int main(int argc, char **argv) char *qemu_cmd =3D NULL; int ret; char template[] =3D "/tmp/vhost-test-XXXXXX"; + GMainLoop *loop; + GThread *thread; =20 g_test_init(&argc, &argv, NULL); =20 @@ -612,8 +619,9 @@ int main(int argc, char **argv) =20 server =3D test_server_new("test"); =20 + loop =3D g_main_loop_new(NULL, FALSE); /* run the main loop thread so the chardev may operate */ - g_thread_new(NULL, thread_function, NULL); + thread =3D g_thread_new(NULL, thread_function, loop); =20 qemu_cmd =3D GET_QEMU_CMD(server); =20 @@ -632,6 +640,14 @@ int main(int argc, char **argv) /* cleanup */ test_server_free(server); =20 + /* 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 =3D rmdir(tmpfs); if (ret !=3D 0) { g_test_message("unable to rmdir: path (%s): %s\n", --=20 MST