gnetworkmonitornetlink: Fix potential GMainContext issue

Previously, the GSource would be attached to whatever GMainContext was
the thread default at the time; but that might no longer be the same as
the default at the time of constructing the GNetworkMonitor.

Save the default from construction time, so that source callbacks are
always invoked in the same GMainContext.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://bugzilla.gnome.org/show_bug.cgi?id=793880
This commit is contained in:
Philip Withnall 2018-02-27 12:48:05 +00:00
parent 11909c67d1
commit 85f3bc133f

View File

@ -46,6 +46,7 @@ struct _GNetworkMonitorNetlinkPrivate
{
GSocket *sock;
GSource *source, *dump_source;
GMainContext *context;
GPtrArray *dump_networks;
};
@ -75,7 +76,6 @@ g_network_monitor_netlink_init (GNetworkMonitorNetlink *nl)
nl->priv = g_network_monitor_netlink_get_instance_private (nl);
}
static gboolean
g_network_monitor_netlink_initable_init (GInitable *initable,
GCancellable *cancellable,
@ -143,11 +143,11 @@ g_network_monitor_netlink_initable_init (GInitable *initable,
}
g_socket_set_blocking (nl->priv->sock, FALSE);
nl->priv->context = g_main_context_ref_thread_default ();
nl->priv->source = g_socket_create_source (nl->priv->sock, G_IO_IN, NULL);
g_source_set_callback (nl->priv->source,
(GSourceFunc) read_netlink_messages, nl, NULL);
g_source_attach (nl->priv->source,
g_main_context_get_thread_default ());
g_source_attach (nl->priv->source, nl->priv->context);
return TRUE;
}
@ -209,8 +209,7 @@ queue_request_dump (GNetworkMonitorNetlink *nl)
nl->priv->dump_source = g_timeout_source_new (1000);
g_source_set_callback (nl->priv->dump_source,
(GSourceFunc) timeout_request_dump, nl, NULL);
g_source_attach (nl->priv->dump_source,
g_main_context_get_thread_default ());
g_source_attach (nl->priv->dump_source, nl->priv->context);
}
static void
@ -457,6 +456,8 @@ g_network_monitor_netlink_finalize (GObject *object)
g_source_unref (nl->priv->dump_source);
}
g_clear_pointer (&nl->priv->context, g_main_context_unref);
G_OBJECT_CLASS (g_network_monitor_netlink_parent_class)->finalize (object);
}
@ -465,7 +466,7 @@ g_network_monitor_netlink_class_init (GNetworkMonitorNetlinkClass *nl_class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);
gobject_class->finalize = g_network_monitor_netlink_finalize;
gobject_class->finalize = g_network_monitor_netlink_finalize;
}
static void