[gnome-bluetooth/wip/hadess/upower-race] lib: Fix possible race between UPower and Bluez on init




commit 778d6d3d856fb8a42ee3bbe43eb76c6b142be084
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Feb 23 16:57:38 2022 +0100

    lib: Fix possible race between UPower and Bluez on init
    
    Fix possible race between UPower and Bluez when creating the
    BluetoothClient.
    
    Bluetooth-DEBUG: Successfully created UpClient
    Bluetooth-DEBUG: Got initial list of 2 UPower devices
    Bluetooth-DEBUG: Considering UPower device /org/freedesktop/UPower/devices/mouse_dev_11_22_33_44_55_66
    Bluetooth-DEBUG: Could not find bluez device for upower device /org/bluez/hci0/dev_11_22_33_44_55_66
    Bluetooth-DEBUG: Considering UPower device /org/freedesktop/UPower/devices/mouse_dev_11_22_33_44_55_67
    Bluetooth-DEBUG: Could not find bluez device for upower device /org/bluez/hci0/dev_11_22_33_44_55_67
    Bluetooth-DEBUG: Adding adapters from ObjectManager
    Bluetooth-DEBUG: Inserting adapter :1.2 /org/bluez/hci0 org.bluez.Adapter1
    Bluetooth-DEBUG: Setting '/org/bluez/hci0' as the new default adapter
    Bluetooth-DEBUG: Emptying list store as default adapter changed
    Bluetooth-DEBUG: Coldplugging devices for new default adapter
    Bluetooth-DEBUG: Adding device '11:22:33:44:55:66' on adapter '/org/bluez/hci0' to list store
    Bluetooth-DEBUG: Adding device '11:22:33:44:55:67' on adapter '/org/bluez/hci0' to list store
    Bluetooth-DEBUG: New default adapter so invalidating all the default-adapter* properties
    
    The UpDevices are enumerated before we've even enumerated the Bluetooth
    adapters, so we don't have any BluetoothDevices to attach the UpDevices
    to. Wait until we've at least run through setting a default adapter
    before enumerating the UPower devices.

 lib/bluetooth-client.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
---
diff --git a/lib/bluetooth-client.c b/lib/bluetooth-client.c
index 4705cdb5..bd138c02 100644
--- a/lib/bluetooth-client.c
+++ b/lib/bluetooth-client.c
@@ -64,6 +64,7 @@ struct _BluetoothClient {
        guint num_adapters;
        gboolean discovery_started;
        UpClient *up_client;
+       gboolean bluez_devices_coldplugged;
 };
 
 enum {
@@ -86,6 +87,8 @@ static guint signals[LAST_SIGNAL] = { 0 };
 
 G_DEFINE_TYPE(BluetoothClient, bluetooth_client, G_TYPE_OBJECT)
 
+static void up_client_coldplug (BluetoothClient *client);
+
 static BluetoothDevice *
 get_device_for_path (BluetoothClient *client,
                     const char      *path)
@@ -413,6 +416,9 @@ add_devices_to_list_store (BluetoothClient *client)
 {
        GList *object_list, *l;
        const char *default_adapter_path;
+       gboolean coldplug_upower;
+
+       coldplug_upower = !client->bluez_devices_coldplugged && client->up_client;
 
        g_debug ("Emptying list store as default adapter changed");
        g_list_store_remove_all (client->list_store);
@@ -420,6 +426,7 @@ add_devices_to_list_store (BluetoothClient *client)
        default_adapter_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (client->default_adapter));
 
        g_debug ("Coldplugging devices for new default adapter");
+       client->bluez_devices_coldplugged = TRUE;
        object_list = g_dbus_object_manager_get_objects (client->manager);
        for (l = object_list; l != NULL; l = l->next) {
                GDBusObject *object = l->data;
@@ -472,6 +479,9 @@ add_devices_to_list_store (BluetoothClient *client)
                g_signal_emit (G_OBJECT (client), signals[DEVICE_ADDED], 0, device_obj);
        }
        g_list_free_full (object_list, g_object_unref);
+
+       if (coldplug_upower)
+               up_client_coldplug (client);
 }
 
 static void
@@ -981,6 +991,14 @@ up_client_get_devices_cb (GObject      *source_object,
        g_ptr_array_unref (devices);
 }
 
+static void
+up_client_coldplug (BluetoothClient *client)
+{
+       g_return_if_fail (client->up_client != NULL);
+       up_client_get_devices_async (client->up_client, client->cancellable,
+                                    up_client_get_devices_cb, client);
+}
+
 static void
 up_client_new_cb (GObject      *source_object,
                  GAsyncResult *res,
@@ -1004,8 +1022,8 @@ up_client_new_cb (GObject      *source_object,
                                 G_CALLBACK (up_device_added_cb), client, 0);
        g_signal_connect_object (G_OBJECT (up_client), "device-removed",
                                 G_CALLBACK (up_device_removed_cb), client, 0);
-       up_client_get_devices_async (up_client, client->cancellable,
-                                    up_client_get_devices_cb, client);
+       if (client->bluez_devices_coldplugged)
+               up_client_coldplug (client);
 }
 
 static void bluetooth_client_init(BluetoothClient *client)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]