[gnome-photos/sam/tracker3: 17/27] photos-tracker-queue: Keep object alive when init fails




commit 4411c6b2427b0bc5b232036b252e07a426aca4e8
Author: Sam Thursfield <sam afuera me uk>
Date:   Wed Jul 22 20:10:39 2020 +0200

    photos-tracker-queue: Keep object alive when init fails
    
    If init succeeds, we will have one singleton as before.
    If init fails, we will also have one singleton, which will
    return an error any time g_initable_init() is called
    (thanks to 51b2290601494d4aa900d838179ab63012ffd214).
    
    Previously, if init failed, the weak reference would cause the `static
    GObject *self` value inside the constructor function to return to NULL,
    meaning the next call to photos_tracker_queue_dup_singleton() would try
    to initialise Tracker again. In some cases this could result in a very
    slow startup.

 src/photos-search-context.c     |  2 +-
 src/photos-tracker-controller.c | 12 ++++++------
 src/photos-tracker-queue.c      | 13 ++++++++++++-
 3 files changed, 19 insertions(+), 8 deletions(-)
---
diff --git a/src/photos-search-context.c b/src/photos-search-context.c
index 4840caff..6f8694bc 100644
--- a/src/photos-search-context.c
+++ b/src/photos-search-context.c
@@ -70,7 +70,7 @@ photos_search_context_state_free (PhotosSearchContextState *state)
   g_object_unref (state->srch_mtch_mngr);
   g_object_unref (state->srch_typ_mngr);
   g_object_unref (state->srch_cntrlr);
-  g_object_unref (state->queue);
+  g_clear_object (&state->queue);
   g_slice_free (PhotosSearchContextState, state);
 }
 
diff --git a/src/photos-tracker-controller.c b/src/photos-tracker-controller.c
index a02db077..6d0053a1 100644
--- a/src/photos-tracker-controller.c
+++ b/src/photos-tracker-controller.c
@@ -292,6 +292,12 @@ photos_tracker_controller_perform_current_query (PhotosTrackerController *self)
 
   priv = photos_tracker_controller_get_instance_private (self);
 
+  if (G_UNLIKELY (priv->queue == NULL))
+    {
+      photos_tracker_controller_query_error (self, priv->queue_error);
+      goto out;
+    }
+
   g_clear_object (&priv->current_query);
   priv->current_query = PHOTOS_TRACKER_CONTROLLER_GET_CLASS (self)->get_query (self);
   g_return_if_fail (priv->current_query != NULL);
@@ -303,12 +309,6 @@ photos_tracker_controller_perform_current_query (PhotosTrackerController *self)
   g_object_unref (priv->cancellable);
   priv->cancellable = g_cancellable_new ();
 
-  if (G_UNLIKELY (priv->queue == NULL))
-    {
-      photos_tracker_controller_query_error (self, priv->queue_error);
-      goto out;
-    }
-
   photos_tracker_queue_select (priv->queue,
                                priv->current_query,
                                priv->cancellable,
diff --git a/src/photos-tracker-queue.c b/src/photos-tracker-queue.c
index cf1b8b96..32e3fb18 100644
--- a/src/photos-tracker-queue.c
+++ b/src/photos-tracker-queue.c
@@ -444,6 +444,7 @@ photos_tracker_queue_initable_init (GInitable *initable, GCancellable *cancellab
   else
     {
       photos_debug (PHOTOS_DEBUG_TRACKER, "Initialization failed due to %s", 
self->initialization_error->message);
+      g_clear_object (&self->connection);
       ret_val = FALSE;
     }
 
@@ -470,10 +471,20 @@ photos_tracker_queue_initable_iface_init (GInitableIface *iface)
 PhotosTrackerQueue *
 photos_tracker_queue_dup_singleton (GCancellable *cancellable, GError **error)
 {
+  GObject *singleton;
+
   g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  return g_initable_new (PHOTOS_TYPE_TRACKER_QUEUE, cancellable, error, NULL);
+  singleton = g_object_new (PHOTOS_TYPE_TRACKER_QUEUE, NULL);
+
+  if (g_initable_init (G_INITABLE (singleton), cancellable, error))
+      return PHOTOS_TRACKER_QUEUE (singleton);
+
+  /* On error we deliberately don't unref the object so that we won't try
+   * and re-initialize Tracker when called again.
+   */
+  return NULL;
 }
 
 const gchar *


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