Skip to content

Commit 8dac00b

Browse files
elmarcoMarkus Armbruster
authored and
Markus Armbruster
committed
monitor: prevent inserting new monitors after cleanup
monitor_cleanup() is one of the last things main() calls before it returns. In the following patch, monitor_cleanup() will release the monitor_lock during flushing. There may be pending commands to insert new monitors, which would modify the mon_list during iteration, and the clean-up could thus miss those new insertions. Add a monitor_destroyed global to check if monitor_cleanup() has been already called. In this case, don't insert the new monitor in the list, but free it instead. A cleaner solution would involve the main thread telling other threads to terminate, waiting for their termination. Signed-off-by: Marc-André Lureau <[email protected]> Reviewed-by: Markus Armbruster <[email protected]> Message-Id: <[email protected]> Signed-off-by: Markus Armbruster <[email protected]>
1 parent 269d25c commit 8dac00b

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

monitor.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,11 @@ typedef struct QMPRequest QMPRequest;
263263
/* QMP checker flags */
264264
#define QMP_ACCEPT_UNKNOWNS 1
265265

266-
/* Protects mon_list, monitor_qapi_event_state. */
266+
/* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
267267
static QemuMutex monitor_lock;
268268
static GHashTable *monitor_qapi_event_state;
269269
static QTAILQ_HEAD(mon_list, Monitor) mon_list;
270+
static bool monitor_destroyed;
270271

271272
/* Protects mon_fdsets */
272273
static QemuMutex mon_fdsets_lock;
@@ -4538,8 +4539,21 @@ void error_vprintf_unless_qmp(const char *fmt, va_list ap)
45384539
static void monitor_list_append(Monitor *mon)
45394540
{
45404541
qemu_mutex_lock(&monitor_lock);
4541-
QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4542+
/*
4543+
* This prevents inserting new monitors during monitor_cleanup().
4544+
* A cleaner solution would involve the main thread telling other
4545+
* threads to terminate, waiting for their termination.
4546+
*/
4547+
if (!monitor_destroyed) {
4548+
QTAILQ_INSERT_HEAD(&mon_list, mon, entry);
4549+
mon = NULL;
4550+
}
45424551
qemu_mutex_unlock(&monitor_lock);
4552+
4553+
if (mon) {
4554+
monitor_data_destroy(mon);
4555+
g_free(mon);
4556+
}
45434557
}
45444558

45454559
static void monitor_qmp_setup_handlers_bh(void *opaque)
@@ -4634,6 +4648,7 @@ void monitor_cleanup(void)
46344648

46354649
/* Flush output buffers and destroy monitors */
46364650
qemu_mutex_lock(&monitor_lock);
4651+
monitor_destroyed = true;
46374652
QTAILQ_FOREACH_SAFE(mon, &mon_list, entry, next) {
46384653
QTAILQ_REMOVE(&mon_list, mon, entry);
46394654
monitor_flush(mon);

0 commit comments

Comments
 (0)