Skip to content

Commit

Permalink
monitor: delay monitor iothread creation
Browse files Browse the repository at this point in the history
Commit d32749d 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 <[email protected]>
Fixes: d32749d ("monitor: move init global earlier")
Message-Id: <[email protected]>
Reviewed-by: Eric Blake <[email protected]>
Reviewed-by: Peter Xu <[email protected]>
Tested-by: Peter Xu <[email protected]>
[This fixes a crash on shutdown with --daemonize]
Signed-off-by: Markus Armbruster <[email protected]>
  • Loading branch information
Blub authored and Markus Armbruster committed Nov 6, 2018
1 parent 9a3e52e commit 8511770
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,14 @@ static void monitor_qapi_event_init(void)

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,
bool use_io_thread)
{
if (use_io_thread && !mon_iothread) {
monitor_iothread_init();
}
memset(mon, 0, sizeof(Monitor));
qemu_mutex_init(&mon->mon_lock);
qemu_mutex_init(&mon->qmp.qmp_queue_lock);
Expand Down Expand Up @@ -4461,15 +4466,6 @@ static AioContext *monitor_get_aio_context(void)
static void monitor_iothread_init(void)
{
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)
Expand All @@ -4479,7 +4475,15 @@ void monitor_init_globals(void)
sortcmdlist();
qemu_mutex_init(&monitor_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
Expand Down Expand Up @@ -4624,7 +4628,9 @@ void monitor_cleanup(void)
* we need to unregister from chardev below in
* 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 */
qemu_mutex_lock(&monitor_lock);
Expand All @@ -4639,9 +4645,10 @@ void monitor_cleanup(void)
/* QEMUBHs needs to be deleted before destroying the I/O thread */
qemu_bh_delete(qmp_dispatcher_bh);
qmp_dispatcher_bh = NULL;

iothread_destroy(mon_iothread);
mon_iothread = NULL;
if (mon_iothread) {
iothread_destroy(mon_iothread);
mon_iothread = NULL;
}
}

QemuOptsList qemu_mon_opts = {
Expand Down

0 comments on commit 8511770

Please sign in to comment.