fix for race condition in watching names 92/102992/1 accepted/tizen/common/20161212.185410 accepted/tizen/ivi/20161212.025243 accepted/tizen/mobile/20161212.025008 accepted/tizen/tv/20161212.025121 accepted/tizen/wearable/20161212.025155 submit/tizen/20161209.100157 submit/tizen/20161209.115942
authorAdrian Szyndela <adrian.s@samsung.com>
Mon, 5 Dec 2016 14:38:08 +0000 (15:38 +0100)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Wed, 7 Dec 2016 02:59:16 +0000 (18:59 -0800)
Change-Id: I11fa92a6dae62cb3bdaa4159db0da160752063d1
(cherry picked from commit f1f2e5e4df63f06cac53148e5c91a27a56eea9ca)

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

old mode 100644 (file)
new mode 100755 (executable)
index 07713db..c2ed2aa
@@ -282,9 +282,6 @@ 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)
@@ -300,7 +297,7 @@ on_name_owner_changed (GDBusConnection *connection,
   if (g_strcmp0 (name, client->name) != 0)
     goto out;
 
-  if ((old_owner != NULL && strlen (old_owner) > 0) && client->name_owner != NULL)
+  if (old_owner != NULL && strlen (old_owner) > 0)
     {
       g_free (client->name_owner);
       client->name_owner = NULL;
@@ -309,12 +306,18 @@ 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:
   ;
 }
@@ -336,6 +339,13 @@ 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);
@@ -352,10 +362,10 @@ get_name_owner_cb (GObject      *source_object,
       call_vanished_handler (client, FALSE);
     }
 
-  client->initialized = TRUE;
-
+ out:
   if (result != NULL)
     g_variant_unref (result);
+
   client_unref (client);
 }
 
@@ -410,7 +420,6 @@ 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