monitor: delay monitor iothread creation

Commit d32749deb6 moved the call to monitor_init_globals()
to before os_daemonize(), making it an unsuitable place to
spawn the monitor iothread as it won't be inherited over the
fork() in os_daemonize().

We now spawn the thread the first time we instantiate a
monitor which actually has use_io_thread == true.
Instantiation of monitors happens only after os_daemonize().
We still need to create the qmp_dispatcher_bh when not using
iothreads, so this now still happens in
monitor_init_globals().

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
Fixes: d32749deb6 ("monitor: move init global earlier")
Message-Id: <20180925081507.11873-3-w.bumiller@proxmox.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Peter Xu <peterx@redhat.com>
Tested-by: Peter Xu <peterx@redhat.com>
[This fixes a crash on shutdown with --daemonize]
Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
Wolfgang Bumiller 2018-09-25 10:15:07 +02:00 committed by Markus Armbruster
parent 9a3e52e346
commit 8511770185

View File

@ -708,9 +708,14 @@ static void monitor_qapi_event_init(void)
static void handle_hmp_command(Monitor *mon, const char *cmdline); static void handle_hmp_command(Monitor *mon, const char *cmdline);
static void monitor_iothread_init(void);
static void monitor_data_init(Monitor *mon, bool skip_flush, static void monitor_data_init(Monitor *mon, bool skip_flush,
bool use_io_thread) bool use_io_thread)
{ {
if (use_io_thread && !mon_iothread) {
monitor_iothread_init();
}
memset(mon, 0, sizeof(Monitor)); memset(mon, 0, sizeof(Monitor));
qemu_mutex_init(&mon->mon_lock); qemu_mutex_init(&mon->mon_lock);
qemu_mutex_init(&mon->qmp.qmp_queue_lock); qemu_mutex_init(&mon->qmp.qmp_queue_lock);
@ -4461,15 +4466,6 @@ static AioContext *monitor_get_aio_context(void)
static void monitor_iothread_init(void) static void monitor_iothread_init(void)
{ {
mon_iothread = iothread_create("mon_iothread", &error_abort); mon_iothread = iothread_create("mon_iothread", &error_abort);
/*
* The dispatcher BH must run in the main loop thread, since we
* have commands assuming that context. It would be nice to get
* rid of those assumptions.
*/
qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
monitor_qmp_bh_dispatcher,
NULL);
} }
void monitor_init_globals(void) void monitor_init_globals(void)
@ -4479,7 +4475,15 @@ void monitor_init_globals(void)
sortcmdlist(); sortcmdlist();
qemu_mutex_init(&monitor_lock); qemu_mutex_init(&monitor_lock);
qemu_mutex_init(&mon_fdsets_lock); qemu_mutex_init(&mon_fdsets_lock);
monitor_iothread_init();
/*
* The dispatcher BH must run in the main loop thread, since we
* have commands assuming that context. It would be nice to get
* rid of those assumptions.
*/
qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
monitor_qmp_bh_dispatcher,
NULL);
} }
/* These functions just adapt the readline interface in a typesafe way. We /* These functions just adapt the readline interface in a typesafe way. We
@ -4624,7 +4628,9 @@ void monitor_cleanup(void)
* we need to unregister from chardev below in * we need to unregister from chardev below in
* monitor_data_destroy(), and chardev is not thread-safe yet * monitor_data_destroy(), and chardev is not thread-safe yet
*/ */
iothread_stop(mon_iothread); if (mon_iothread) {
iothread_stop(mon_iothread);
}
/* Flush output buffers and destroy monitors */ /* Flush output buffers and destroy monitors */
qemu_mutex_lock(&monitor_lock); qemu_mutex_lock(&monitor_lock);
@ -4639,9 +4645,10 @@ void monitor_cleanup(void)
/* QEMUBHs needs to be deleted before destroying the I/O thread */ /* QEMUBHs needs to be deleted before destroying the I/O thread */
qemu_bh_delete(qmp_dispatcher_bh); qemu_bh_delete(qmp_dispatcher_bh);
qmp_dispatcher_bh = NULL; qmp_dispatcher_bh = NULL;
if (mon_iothread) {
iothread_destroy(mon_iothread); iothread_destroy(mon_iothread);
mon_iothread = NULL; mon_iothread = NULL;
}
} }
QemuOptsList qemu_mon_opts = { QemuOptsList qemu_mon_opts = {