1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Wolfgang Bumiller <w.bumiller@proxmox.com>
3 Date: Tue, 25 Sep 2018 10:15:07 +0200
4 Subject: [PATCH] monitor: delay monitor iothread creation
6 Commit d32749deb615 moved the call to monitor_init_globals()
7 to before os_daemonize(), making it an unsuitable place to
8 spawn the monitor iothread as it won't be inherited over the
9 fork() in os_daemonize().
11 We now spawn the thread the first time we instantiate a
12 monitor which actually has use_io_thread == true.
13 Instantiation of monitors happens only after os_daemonize().
14 We still need to create the qmp_dispatcher_bh when not using
15 iothreads, so this now still happens in
16 monitor_init_globals().
18 Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
19 Fixes: d32749deb615 ("monitor: move init global earlier")
20 Message-Id: <20180925081507.11873-3-w.bumiller@proxmox.com>
21 Reviewed-by: Eric Blake <eblake@redhat.com>
22 Reviewed-by: Peter Xu <peterx@redhat.com>
23 Tested-by: Peter Xu <peterx@redhat.com>
24 [This fixes a crash on shutdown with --daemonize]
25 Signed-off-by: Markus Armbruster <armbru@redhat.com>
27 monitor.c | 36 ++++++++++++++++++++++--------------
28 1 file changed, 22 insertions(+), 14 deletions(-)
30 diff --git a/monitor.c b/monitor.c
31 index 836c0bbdaa..c7eae64fd9 100644
34 @@ -807,9 +807,14 @@ static void monitor_qapi_event_init(void)
36 static void handle_hmp_command(Monitor *mon, const char *cmdline);
38 +static void monitor_iothread_init(void);
40 static void monitor_data_init(Monitor *mon, bool skip_flush,
43 + if (use_io_thread && !mon_iothread) {
44 + monitor_iothread_init();
46 memset(mon, 0, sizeof(Monitor));
47 qemu_mutex_init(&mon->mon_lock);
48 qemu_mutex_init(&mon->qmp.qmp_queue_lock);
49 @@ -4544,6 +4549,15 @@ static AioContext *monitor_get_aio_context(void)
50 static void monitor_iothread_init(void)
52 mon_iothread = iothread_create("mon_iothread", &error_abort);
55 +void monitor_init_globals(void)
57 + monitor_init_qmp_commands();
58 + monitor_qapi_event_init();
60 + qemu_mutex_init(&monitor_lock);
61 + qemu_mutex_init(&mon_fdsets_lock);
64 * The dispatcher BH must run in the main loop thread, since we
65 @@ -4559,21 +4573,11 @@ static void monitor_iothread_init(void)
66 * monitors that are using the I/O thread have their output
67 * written by the I/O thread.
69 - qmp_respond_bh = aio_bh_new(monitor_get_aio_context(),
70 + qmp_respond_bh = aio_bh_new(iohandler_get_aio_context(),
71 monitor_qmp_bh_responder,
75 -void monitor_init_globals(void)
77 - monitor_init_qmp_commands();
78 - monitor_qapi_event_init();
80 - qemu_mutex_init(&monitor_lock);
81 - qemu_mutex_init(&mon_fdsets_lock);
82 - monitor_iothread_init();
85 /* These functions just adapt the readline interface in a typesafe way. We
86 * could cast function pointers but that discards compiler checks.
88 @@ -4711,7 +4715,9 @@ void monitor_cleanup(void)
89 * we need to unregister from chardev below in
90 * monitor_data_destroy(), and chardev is not thread-safe yet
92 - iothread_stop(mon_iothread);
94 + iothread_stop(mon_iothread);
98 * Flush all response queues. Note that even after this flush,
99 @@ -4735,8 +4741,10 @@ void monitor_cleanup(void)
100 qemu_bh_delete(qmp_respond_bh);
101 qmp_respond_bh = NULL;
103 - iothread_destroy(mon_iothread);
104 - mon_iothread = NULL;
105 + if (mon_iothread) {
106 + iothread_destroy(mon_iothread);
107 + mon_iothread = NULL;
111 QemuOptsList qemu_mon_opts = {