From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a3khk-0002h1-Fc for qemu-devel@nongnu.org; Tue, 01 Dec 2015 08:10:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a3khh-0006NH-8X for qemu-devel@nongnu.org; Tue, 01 Dec 2015 08:10:48 -0500 Received: from mail-wm0-x22e.google.com ([2a00:1450:400c:c09::22e]:35853) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a3khg-0006N5-Vr for qemu-devel@nongnu.org; Tue, 01 Dec 2015 08:10:45 -0500 Received: by wmww144 with SMTP id w144so172211226wmw.1 for ; Tue, 01 Dec 2015 05:10:44 -0800 (PST) References: <1448901890-17012-1-git-send-email-marcandre.lureau@redhat.com> <1448901890-17012-2-git-send-email-marcandre.lureau@redhat.com> From: Alex =?utf-8?Q?Benn=C3=A9e?= In-reply-to: <1448901890-17012-2-git-send-email-marcandre.lureau@redhat.com> Date: Tue, 01 Dec 2015 13:10:42 +0000 Message-ID: <87610ib9gt.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [PATCH for-2.5 v4 1/4] vhost-user-test: fix chardriver race List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: marcandre.lureau@redhat.com Cc: qemu-devel@nongnu.org, mst@redhat.com marcandre.lureau@redhat.com writes: > From: Marc-André 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é Lureau > Reviewed-by: Michael S. Tsirkin Reviewed-by: Alex Bennée > --- > 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", -- Alex Bennée