Revert name watching race condition fix
authorAdrian Szyndela <adrian.s@samsung.com>
Fri, 17 Jan 2020 11:07:07 +0000 (12:07 +0100)
committerAdrian Szyndela <adrian.s@samsung.com>
Fri, 17 Jan 2020 11:14:48 +0000 (12:14 +0100)
This reverts three commits aimed to fix a race condition:
a0dc33df372eb7a54a89221b97db753d560fcfd7
dc8a71bcd7e904a6419f8e7cd1a772d8a066c95f
75ab298452793ea20ab1daa255602200bb309ac8

The race condition was fixed in upstream in a different way, so we're
importing it.

Change-Id: Icab842f507b033ef83c984cdc30004a681c69c43

gio/gdbusconnection.c
gio/gdbusconnection.h
gio/gdbusnamewatching.c [changed mode: 0755->0644]

index 9dd59a9..8510a3f 100755 (executable)
@@ -4072,8 +4072,6 @@ typedef struct
 {
   GDBusSignalCallback callback;
   gpointer user_data;
-  GFunc user_data_ref_func;
-  GFunc user_data_unref_func;
   GDestroyNotify user_data_free_func;
   guint id;
   GMainContext *context;
@@ -4278,34 +4276,6 @@ g_dbus_connection_signal_subscribe (GDBusConnection     *connection,
                                     gpointer             user_data,
                                     GDestroyNotify       user_data_free_func)
 {
-  return g_dbus_connection_signal_subscribe_with_ref (connection,
-                                                      sender,
-                                                      interface_name,
-                                                      member,
-                                                      object_path,
-                                                      arg0,
-                                                      flags,
-                                                      callback,
-                                                      user_data,
-                                                      NULL,
-                                                      NULL,
-                                                      user_data_free_func);
-}
-
-guint
-g_dbus_connection_signal_subscribe_with_ref (GDBusConnection             *connection,
-                                             const gchar                 *sender,
-                                             const gchar                 *interface_name,
-                                             const gchar                 *member,
-                                             const gchar                 *object_path,
-                                             const gchar                 *arg0,
-                                             GDBusSignalFlags            flags,
-                                             GDBusSignalCallback         callback,
-                                             gpointer                    user_data,
-                                             GFunc                       user_data_ref_func,
-                                             GFunc                       user_data_unref_func,
-                                             GDestroyNotify              user_data_free_func)
-{
   gchar *rule;
   SignalData *signal_data;
   SignalSubscriber subscriber;
@@ -4354,8 +4324,6 @@ g_dbus_connection_signal_subscribe_with_ref (GDBusConnection             *connec
   subscriber.user_data_free_func = user_data_free_func;
   subscriber.id = g_atomic_int_add (&_global_subscriber_id, 1); /* TODO: overflow etc. */
   subscriber.context = g_main_context_ref_thread_default ();
-  subscriber.user_data_ref_func = user_data_ref_func;
-  subscriber.user_data_unref_func = user_data_unref_func;
 
   /* see if we've already have this rule */
   signal_data = g_hash_table_lookup (connection->map_rule_to_signal_data, rule);
@@ -4603,8 +4571,6 @@ typedef struct
   guint                subscription_id;
   GDBusSignalCallback  callback;
   gpointer             user_data;
-  GFunc                user_data_ref_func;
-  GFunc                user_data_unref_func;
   GDBusMessage        *message;
   GDBusConnection     *connection;
   const gchar         *sender;
@@ -4649,25 +4615,17 @@ emit_signal_instance_in_idle_cb (gpointer data)
   has_subscription = FALSE;
   if (g_hash_table_lookup (signal_instance->connection->map_id_to_signal_data,
                            GUINT_TO_POINTER (signal_instance->subscription_id)) != NULL)
-    {
-      if (signal_instance->user_data_ref_func)
-        signal_instance->user_data_ref_func (signal_instance->user_data, 0);
-      has_subscription = TRUE;
-    }
+    has_subscription = TRUE;
   CONNECTION_UNLOCK (signal_instance->connection);
 
   if (has_subscription)
-    {
-      signal_instance->callback (signal_instance->connection,
-                                 signal_instance->sender,
-                                 signal_instance->path,
-                                 signal_instance->interface,
-                                 signal_instance->member,
-                                 parameters,
-                                 signal_instance->user_data);
-      if (signal_instance->user_data_unref_func)
-        signal_instance->user_data_unref_func (signal_instance->user_data, 0);
-    }
+    signal_instance->callback (signal_instance->connection,
+                               signal_instance->sender,
+                               signal_instance->path,
+                               signal_instance->interface,
+                               signal_instance->member,
+                               parameters,
+                               signal_instance->user_data);
 
   g_variant_unref (parameters);
 
@@ -4803,8 +4761,6 @@ schedule_callbacks (GDBusConnection *connection,
           signal_instance->subscription_id = subscriber->id;
           signal_instance->callback = subscriber->callback;
           signal_instance->user_data = subscriber->user_data;
-          signal_instance->user_data_ref_func = subscriber->user_data_ref_func;
-          signal_instance->user_data_unref_func = subscriber->user_data_unref_func;
           signal_instance->message = g_object_ref (message);
           signal_instance->connection = g_object_ref (connection);
           signal_instance->sender = sender;
index 4a80c17..9338fd7 100644 (file)
@@ -634,19 +634,6 @@ guint            g_dbus_connection_signal_subscribe           (GDBusConnection
                                                                gpointer             user_data,
                                                                GDestroyNotify       user_data_free_func) TIZEN_PUBLIC_DEPRECATED_API;
 GLIB_AVAILABLE_IN_ALL
-guint            g_dbus_connection_signal_subscribe_with_ref  (GDBusConnection     *connection,
-                                                               const gchar         *sender,
-                                                               const gchar         *interface_name,
-                                                               const gchar         *member,
-                                                               const gchar         *object_path,
-                                                               const gchar         *arg0,
-                                                               GDBusSignalFlags     flags,
-                                                               GDBusSignalCallback  callback,
-                                                               gpointer             user_data,
-                                                               GFunc                user_data_ref_func,
-                                                               GFunc                user_data_unref_func,
-                                                               GDestroyNotify       user_data_free_func) TIZEN_PUBLIC_DEPRECATED_API;
-GLIB_AVAILABLE_IN_ALL
 void             g_dbus_connection_signal_unsubscribe         (GDBusConnection     *connection,
                                                                guint                subscription_id) TIZEN_PUBLIC_DEPRECATED_API;
 
old mode 100755 (executable)
new mode 100644 (file)
index 6ac4aa8..14603c5
@@ -112,20 +112,6 @@ client_unref (Client *client)
     }
 }
 
-static void
-client_ref_func (gpointer user_data, gpointer unused)
-{
-  if (user_data)
-    client_ref ((Client*)user_data);
-}
-
-static void
-client_unref_func (gpointer user_data, gpointer unused)
-{
-  if (user_data)
-    client_unref ((Client*)user_data);
-}
-
 /* ---------------------------------------------------------------------------------------------------- */
 
 typedef enum
@@ -299,6 +285,9 @@ on_name_owner_changed (GDBusConnection *connection,
   const gchar *old_owner;
   const gchar *new_owner;
 
+  if (!client->initialized)
+    goto out;
+
   if (g_strcmp0 (object_path, "/org/freedesktop/DBus") != 0 ||
       g_strcmp0 (interface_name, "org.freedesktop.DBus") != 0 ||
       g_strcmp0 (sender_name, "org.freedesktop.DBus") != 0)
@@ -314,7 +303,7 @@ on_name_owner_changed (GDBusConnection *connection,
   if (g_strcmp0 (name, client->name) != 0)
     goto out;
 
-  if (old_owner != NULL && strlen (old_owner) > 0)
+  if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
     {
       g_free (client->name_owner);
       client->name_owner = NULL;
@@ -323,18 +312,12 @@ on_name_owner_changed (GDBusConnection *connection,
 
   if (new_owner != NULL && strlen (new_owner) > 0)
     {
+      g_warn_if_fail (client->name_owner == NULL);
       g_free (client->name_owner);
       client->name_owner = g_strdup (new_owner);
       call_appeared_handler (client);
     }
 
-  /* initialized set to TRUE means that signal was delivered and processed.
-   * Now, if we receive a reply to GetNameOwner call, we may just ignore it as it carries the same
-   * information as the current signal, or if something changed in the meantime we will
-   * get next signal very soon.
-   */
-  client->initialized = TRUE;
-
  out:
   ;
 }
@@ -356,13 +339,6 @@ get_name_owner_cb (GObject      *source_object,
   result = g_dbus_connection_call_finish (client->connection,
                                           res,
                                           NULL);
-  /* In case we already received NameOwnerChanged signal, we don't need to
-   * process GetNameOwner answer, because all the information we needed was already
-   * delivered with the signal and processed by the signal handler.
-   */
-  if (client->initialized)
-    goto out;
-
   if (result != NULL)
     {
       g_variant_get (result, "(&s)", &name_owner);
@@ -379,10 +355,10 @@ get_name_owner_cb (GObject      *source_object,
       call_vanished_handler (client, FALSE);
     }
 
- out:
+  client->initialized = TRUE;
+
   if (result != NULL)
     g_variant_unref (result);
-
   client_unref (client);
 }
 
@@ -437,6 +413,7 @@ start_service_by_name_cb (GObject      *source_object,
         {
           g_warning ("Unexpected reply %d from StartServiceByName() method", start_service_result);
           call_vanished_handler (client, FALSE);
+          client->initialized = TRUE;
         }
     }
   else
@@ -470,19 +447,16 @@ has_connection (Client *client)
                                                              client);
 
   /* start listening to NameOwnerChanged messages immediately */
-  client->name_owner_changed_subscription_id
-    = g_dbus_connection_signal_subscribe_with_ref (client->connection,
-                                                   "org.freedesktop.DBus",  /* name */
-                                                   "org.freedesktop.DBus",  /* if */
-                                                   "NameOwnerChanged",      /* signal */
-                                                   "/org/freedesktop/DBus", /* path */
-                                                   client->name,
-                                                   G_DBUS_SIGNAL_FLAGS_NONE,
-                                                   on_name_owner_changed,
-                                                   client,
-                                                   client_ref_func,
-                                                   client_unref_func,
-                                                   NULL);
+  client->name_owner_changed_subscription_id = g_dbus_connection_signal_subscribe (client->connection,
+                                                                                   "org.freedesktop.DBus",  /* name */
+                                                                                   "org.freedesktop.DBus",  /* if */
+                                                                                   "NameOwnerChanged",      /* signal */
+                                                                                   "/org/freedesktop/DBus", /* path */
+                                                                                   client->name,
+                                                                                   G_DBUS_SIGNAL_FLAGS_NONE,
+                                                                                   on_name_owner_changed,
+                                                                                   client,
+                                                                                   NULL);
 
   if (client->flags & G_BUS_NAME_WATCHER_FLAGS_AUTO_START)
     {