diff --git a/src/util/vireventglib.c b/src/util/vireventglib.c index 6e334b3398..7e61b096ed 100644 --- a/src/util/vireventglib.c +++ b/src/util/vireventglib.c @@ -185,6 +185,24 @@ virEventGLibHandleFind(int watch) return NULL; } +/* + * If the last refernce to a GSource is released in a non-main + * thread we're exposed to a race condition that causes a + * crash: + * https://gitlab.gnome.org/GNOME/glib/-/merge_requests/1358 + * Thus we're using an idle func to release our ref + */ +static gboolean +virEventGLibSourceUnrefIdle(gpointer data) +{ + GSource *src = data; + + g_source_unref(src); + + return FALSE; +} + + static void virEventGLibHandleUpdate(int watch, int events) @@ -213,7 +231,7 @@ virEventGLibHandleUpdate(int watch, if (data->source != NULL) { VIR_DEBUG("Removed old handle source=%p", data->source); g_source_destroy(data->source); - g_source_unref(data->source); + g_idle_add(virEventGLibSourceUnrefIdle, data->source); } data->source = virEventGLibAddSocketWatch( @@ -227,7 +245,7 @@ virEventGLibHandleUpdate(int watch, VIR_DEBUG("Removed old handle source=%p", data->source); g_source_destroy(data->source); - g_source_unref(data->source); + g_idle_add(virEventGLibSourceUnrefIdle, data->source); data->source = NULL; data->events = 0; } @@ -276,7 +294,7 @@ virEventGLibHandleRemove(int watch) if (data->source != NULL) { g_source_destroy(data->source); - g_source_unref(data->source); + g_idle_add(virEventGLibSourceUnrefIdle, data->source); data->source = NULL; data->events = 0; } @@ -409,7 +427,7 @@ virEventGLibTimeoutUpdate(int timer, if (interval >= 0) { if (data->source != NULL) { g_source_destroy(data->source); - g_source_unref(data->source); + g_idle_add(virEventGLibSourceUnrefIdle, data->source); } data->interval = interval; @@ -419,7 +437,7 @@ virEventGLibTimeoutUpdate(int timer, goto cleanup; g_source_destroy(data->source); - g_source_unref(data->source); + g_idle_add(virEventGLibSourceUnrefIdle, data->source); data->source = NULL; } @@ -468,7 +486,7 @@ virEventGLibTimeoutRemove(int timer) if (data->source != NULL) { g_source_destroy(data->source); - g_source_unref(data->source); + g_idle_add(virEventGLibSourceUnrefIdle, data->source); data->source = NULL; }