Skip to content

Commit 8511770

Browse files
BlubMarkus Armbruster
authored and
Markus Armbruster
committed
monitor: delay monitor iothread creation
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]>
1 parent 9a3e52e commit 8511770

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

Diff for: monitor.c

+21-14
Original file line numberDiff line numberDiff line change
@@ -708,9 +708,14 @@ static void monitor_qapi_event_init(void)
708708

709709
static void handle_hmp_command(Monitor *mon, const char *cmdline);
710710

711+
static void monitor_iothread_init(void);
712+
711713
static void monitor_data_init(Monitor *mon, bool skip_flush,
712714
bool use_io_thread)
713715
{
716+
if (use_io_thread && !mon_iothread) {
717+
monitor_iothread_init();
718+
}
714719
memset(mon, 0, sizeof(Monitor));
715720
qemu_mutex_init(&mon->mon_lock);
716721
qemu_mutex_init(&mon->qmp.qmp_queue_lock);
@@ -4461,15 +4466,6 @@ static AioContext *monitor_get_aio_context(void)
44614466
static void monitor_iothread_init(void)
44624467
{
44634468
mon_iothread = iothread_create("mon_iothread", &error_abort);
4464-
4465-
/*
4466-
* The dispatcher BH must run in the main loop thread, since we
4467-
* have commands assuming that context. It would be nice to get
4468-
* rid of those assumptions.
4469-
*/
4470-
qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
4471-
monitor_qmp_bh_dispatcher,
4472-
NULL);
44734469
}
44744470

44754471
void monitor_init_globals(void)
@@ -4479,7 +4475,15 @@ void monitor_init_globals(void)
44794475
sortcmdlist();
44804476
qemu_mutex_init(&monitor_lock);
44814477
qemu_mutex_init(&mon_fdsets_lock);
4482-
monitor_iothread_init();
4478+
4479+
/*
4480+
* The dispatcher BH must run in the main loop thread, since we
4481+
* have commands assuming that context. It would be nice to get
4482+
* rid of those assumptions.
4483+
*/
4484+
qmp_dispatcher_bh = aio_bh_new(iohandler_get_aio_context(),
4485+
monitor_qmp_bh_dispatcher,
4486+
NULL);
44834487
}
44844488

44854489
/* These functions just adapt the readline interface in a typesafe way. We
@@ -4624,7 +4628,9 @@ void monitor_cleanup(void)
46244628
* we need to unregister from chardev below in
46254629
* monitor_data_destroy(), and chardev is not thread-safe yet
46264630
*/
4627-
iothread_stop(mon_iothread);
4631+
if (mon_iothread) {
4632+
iothread_stop(mon_iothread);
4633+
}
46284634

46294635
/* Flush output buffers and destroy monitors */
46304636
qemu_mutex_lock(&monitor_lock);
@@ -4639,9 +4645,10 @@ void monitor_cleanup(void)
46394645
/* QEMUBHs needs to be deleted before destroying the I/O thread */
46404646
qemu_bh_delete(qmp_dispatcher_bh);
46414647
qmp_dispatcher_bh = NULL;
4642-
4643-
iothread_destroy(mon_iothread);
4644-
mon_iothread = NULL;
4648+
if (mon_iothread) {
4649+
iothread_destroy(mon_iothread);
4650+
mon_iothread = NULL;
4651+
}
46454652
}
46464653

46474654
QemuOptsList qemu_mon_opts = {

0 commit comments

Comments
 (0)