From 38482ead59ce9d4f0d65ddfb4031ff3397f1563f Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Mon, 10 Sep 2018 12:00:46 +0100 Subject: [PATCH] gnetworkmonitornetlink: Close the socket after disconnecting its GSources `read_netlink_messages()` is the callback attached to the netlink socket (G_IO_IN). It calls `g_socket_receive_message()`. There is a race condition that if the socket is closed while there is a pending call, we will try to receive on a closed socket, which fails. To avoid this, we switch the order of the operations around: first destroy the source and then close the socket. --- gio/gnetworkmonitornetlink.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gio/gnetworkmonitornetlink.c b/gio/gnetworkmonitornetlink.c index f889a3b64..3841e69e1 100644 --- a/gio/gnetworkmonitornetlink.c +++ b/gio/gnetworkmonitornetlink.c @@ -435,12 +435,6 @@ g_network_monitor_netlink_finalize (GObject *object) { GNetworkMonitorNetlink *nl = G_NETWORK_MONITOR_NETLINK (object); - if (nl->priv->sock) - { - g_socket_close (nl->priv->sock, NULL); - g_object_unref (nl->priv->sock); - } - if (nl->priv->source) { g_source_destroy (nl->priv->source); @@ -453,6 +447,12 @@ g_network_monitor_netlink_finalize (GObject *object) g_source_unref (nl->priv->dump_source); } + if (nl->priv->sock) + { + g_socket_close (nl->priv->sock, NULL); + g_object_unref (nl->priv->sock); + } + g_clear_pointer (&nl->priv->context, g_main_context_unref); g_clear_pointer (&nl->priv->dump_networks, g_ptr_array_unref);