[glib] Avoid a race in application registration



commit fdb15058a4625e0f2eb56c56d945e032d8a833ca
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jun 12 18:40:45 2010 -0400

    Avoid a race in application registration
    
    We need to register the object before taking the name.
    Remove the workarounds for this race from the test suite.

 gio/gdbusapplication.c |   45 ++++++++++++++++++++++++++-------------------
 gio/tests/testapps.c   |    7 +------
 2 files changed, 27 insertions(+), 25 deletions(-)
---
diff --git a/gio/gdbusapplication.c b/gio/gdbusapplication.c
index 00f3213..e687199 100644
--- a/gio/gdbusapplication.c
+++ b/gio/gdbusapplication.c
@@ -289,22 +289,6 @@ ensure_bus (GApplication *app)
 static void
 _g_application_platform_init (GApplication *app)
 {
-  GError *error = NULL;
-  guint registration_id;
-
-  ensure_bus (app);
-
-  registration_id = g_dbus_connection_register_object (app->priv->session_bus,
-                                                       app->priv->dbus_path,
-                                                       &application_dbus_interface_info,
-                                                       &application_dbus_vtable,
-                                                       app, NULL,
-                                                       &error);
-  if (registration_id == 0)
-    {
-      g_error ("%s", error->message);
-      g_error_free (error);
-    }
 }
 
 static gboolean
@@ -313,11 +297,24 @@ _g_application_platform_acquire_single_instance (GApplication  *app,
 {
   GVariant *request_result;
   guint32 request_status;
+  gboolean result;
+  guint registration_id;
 
   ensure_bus (app);
+
   if (app->priv->session_bus == NULL)
     return FALSE;
 
+  registration_id = g_dbus_connection_register_object (app->priv->session_bus,
+                                                       app->priv->dbus_path,
+                                                       &application_dbus_interface_info,
+                                                       &application_dbus_vtable,
+                                                       app,
+                                                       NULL,
+                                                       error);
+  if (registration_id == 0)
+    return FALSE;
+
   request_result = g_dbus_connection_call_sync (app->priv->session_bus,
                                                 "org.freedesktop.DBus",
                                                 "/org/freedesktop/DBus",
@@ -327,7 +324,10 @@ _g_application_platform_acquire_single_instance (GApplication  *app,
                                                 NULL, 0, -1, NULL, error);
 
   if (request_result == NULL)
-    return FALSE;
+    {
+      result = FALSE;
+      goto done;
+    }
 
   if (g_variant_is_of_type (request_result, G_VARIANT_TYPE ("(u)")))
     g_variant_get (request_result, "(u)", &request_status);
@@ -343,10 +343,17 @@ _g_application_platform_acquire_single_instance (GApplication  *app,
       else
         g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown error");
 
-      return FALSE;
+      result = FALSE;
+      goto done;
     }
 
-  return TRUE;
+  result = TRUE;
+
+done:
+  if (!result)
+    g_dbus_connection_unregister_object (app->priv->session_bus, registration_id);
+
+  return result;
 }
 
 static void
diff --git a/gio/tests/testapps.c b/gio/tests/testapps.c
index e48a869..00a5f9f 100644
--- a/gio/tests/testapps.c
+++ b/gio/tests/testapps.c
@@ -339,12 +339,7 @@ test_quit_on_app_appeared (void)
                             on_name_disappeared_quit,
                             loop,
                             NULL);
-
-  /* We need a timeout here, since we may otherwise end up calling
-   * Quit after the application took the name, but before it registered
-   * the object.
-   */
-  g_timeout_add (500, call_quit, NULL);
+  call_quit (NULL);
 
   g_main_loop_run (loop);
 



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