[gnome-software] packagekit: Fix newly added locking to not deadlock



commit 53efca36bbf4893a25d9708f8fa97bee343321ab
Author: Kalev Lember <klember redhat com>
Date:   Sun Feb 24 21:50:07 2019 +0100

    packagekit: Fix newly added locking to not deadlock
    
    Use fine grained locking around PkClient / PkTask methods so that we
    (a) only take the lock for as short period of time as needed and
    (b) avoid deadlocks when one locked function calls into another.
    
    This fixes the self tests to pass again (to not deadlock) after
    commit d427457.

 plugins/packagekit/gs-plugin-packagekit-local.c    | 14 ++--
 .../packagekit/gs-plugin-packagekit-refine-repos.c |  7 +-
 plugins/packagekit/gs-plugin-packagekit-refine.c   | 43 ++++--------
 plugins/packagekit/gs-plugin-packagekit-refresh.c  | 28 ++++----
 plugins/packagekit/gs-plugin-packagekit-upgrade.c  |  7 +-
 .../packagekit/gs-plugin-packagekit-url-to-app.c   |  8 +--
 plugins/packagekit/gs-plugin-packagekit.c          | 82 +++++++---------------
 7 files changed, 62 insertions(+), 127 deletions(-)
---
diff --git a/plugins/packagekit/gs-plugin-packagekit-local.c b/plugins/packagekit/gs-plugin-packagekit-local.c
index be230747..77ffe881 100644
--- a/plugins/packagekit/gs-plugin-packagekit-local.c
+++ b/plugins/packagekit/gs-plugin-packagekit-local.c
@@ -55,20 +55,17 @@ gs_plugin_packagekit_refresh_guess_app_id (GsPlugin *plugin,
        g_auto(GStrv) files = NULL;
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) array = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* get file list so we can work out ID */
        files = g_strsplit (filename, "\t", -1);
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_get_files_local (PK_CLIENT (priv->task),
                                             files,
                                             cancellable,
                                             gs_packagekit_helper_cb, helper,
                                             error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                gs_utils_error_add_origin_id (error, app);
                return FALSE;
@@ -137,7 +134,6 @@ gs_plugin_file_to_app (GsPlugin *plugin,
        g_auto(GStrv) split = NULL;
        g_autoptr(GPtrArray) array = NULL;
        g_autoptr(GsApp) app = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
        const gchar *mimetypes[] = {
                "application/x-app-package",
                "application/x-deb",
@@ -153,19 +149,17 @@ gs_plugin_file_to_app (GsPlugin *plugin,
        if (!g_strv_contains (mimetypes, content_type))
                return TRUE;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
-
        /* get details */
        filename = g_file_get_path (file);
        files = g_strsplit (filename, "\t", -1);
+       g_mutex_lock (&priv->task_mutex);
        pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT);
        results = pk_client_get_details_local (PK_CLIENT (priv->task),
                                               files,
                                               cancellable,
                                               gs_packagekit_helper_cb, helper,
                                               error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error))
                return FALSE;
 
diff --git a/plugins/packagekit/gs-plugin-packagekit-refine-repos.c 
b/plugins/packagekit/gs-plugin-packagekit-refine-repos.c
index 6ba8f2f6..1387b58a 100644
--- a/plugins/packagekit/gs-plugin-packagekit-refine-repos.c
+++ b/plugins/packagekit/gs-plugin-packagekit-refine-repos.c
@@ -61,20 +61,17 @@ gs_plugin_packagekit_refine_repo_from_filename (GsPlugin *plugin,
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) packages = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
 
        to_array[0] = filename;
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_search_files (priv->client,
                                          pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED, -1),
                                          (gchar **) to_array,
                                          cancellable,
                                          gs_packagekit_helper_cb, helper,
                                          error);
+       g_mutex_unlock (&priv->client_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to search file %s: ", filename);
                return FALSE;
diff --git a/plugins/packagekit/gs-plugin-packagekit-refine.c 
b/plugins/packagekit/gs-plugin-packagekit-refine.c
index 5230b81d..afc95abe 100644
--- a/plugins/packagekit/gs-plugin-packagekit-refine.c
+++ b/plugins/packagekit/gs-plugin-packagekit-refine.c
@@ -99,7 +99,6 @@ gs_plugin_packagekit_resolve_packages_with_filter (GsPlugin *plugin,
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) package_ids = NULL;
        g_autoptr(GPtrArray) packages = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        package_ids = g_ptr_array_new_with_free_func (g_free);
        for (i = 0; i < gs_app_list_length (list); i++) {
@@ -120,17 +119,15 @@ gs_plugin_packagekit_resolve_packages_with_filter (GsPlugin *plugin,
                return TRUE;
        g_ptr_array_add (package_ids, NULL);
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
-
        /* resolve them all at once */
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_resolve (priv->client,
                                     filter,
                                     (gchar **) package_ids->pdata,
                                     cancellable,
                                     gs_packagekit_helper_cb, helper,
                                     error);
+       g_mutex_unlock (&priv->client_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to resolve package_ids: ");
                return FALSE;
@@ -211,20 +208,17 @@ gs_plugin_packagekit_refine_from_desktop (GsPlugin *plugin,
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) packages = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
 
        to_array[0] = filename;
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_search_files (priv->client,
                                          pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED, -1),
                                          (gchar **) to_array,
                                          cancellable,
                                          gs_packagekit_helper_cb, helper,
                                          error);
+       g_mutex_unlock (&priv->client_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to search file %s: ", filename);
                return FALSE;
@@ -286,7 +280,6 @@ gs_plugin_packagekit_refine_updatedetails (GsPlugin *plugin,
        g_autofree const gchar **package_ids = NULL;
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) array = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        package_ids = g_new0 (const gchar *, gs_app_list_length (list) + 1);
        for (guint i = 0; i < gs_app_list_length (list); i++) {
@@ -300,16 +293,14 @@ gs_plugin_packagekit_refine_updatedetails (GsPlugin *plugin,
        if (cnt == 0)
                return TRUE;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
-
        /* get any update details */
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_get_update_detail (priv->client,
                                               (gchar **) package_ids,
                                               cancellable,
                                               gs_packagekit_helper_cb, helper,
                                               error);
+       g_mutex_unlock (&priv->client_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to get update details for %s: ",
                                package_ids[0]);
@@ -353,7 +344,6 @@ gs_plugin_packagekit_refine_details2 (GsPlugin *plugin,
        g_autoptr(GPtrArray) array = NULL;
        g_autoptr(GPtrArray) package_ids = NULL;
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        package_ids = g_ptr_array_new_with_free_func (g_free);
        for (i = 0; i < gs_app_list_length (list); i++) {
@@ -368,16 +358,14 @@ gs_plugin_packagekit_refine_details2 (GsPlugin *plugin,
                return TRUE;
        g_ptr_array_add (package_ids, NULL);
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
-
        /* get any details */
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_get_details (priv->client,
                                         (gchar **) package_ids->pdata,
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->client_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_autofree gchar *package_ids_str = g_strjoinv (",", (gchar **) package_ids->pdata);
                g_prefix_error (error, "failed to get details for %s: ",
@@ -409,23 +397,20 @@ gs_plugin_packagekit_refine_update_urgency (GsPlugin *plugin,
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkPackageSack) sack = NULL;
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        /* not required */
        if ((flags & GS_PLUGIN_REFINE_FLAGS_REQUIRE_UPDATE_SEVERITY) == 0)
                return TRUE;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
-
        /* get the list of updates */
        filter = pk_bitfield_value (PK_FILTER_ENUM_NONE);
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_get_updates (priv->client,
                                         filter,
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->client_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to get updates for urgency: ");
                return FALSE;
@@ -593,20 +578,17 @@ gs_plugin_packagekit_refine_distro_upgrade (GsPlugin *plugin,
        GsPluginData *priv = gs_plugin_get_data (plugin);
        guint i;
        GsApp *app2;
-       g_autoptr(GMutexLocker) locker = NULL;
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GsAppList) list = NULL;
        guint cache_age_save;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
+       gs_packagekit_helper_add_app (helper, app);
 
        /* ask PK to simulate upgrading the system */
+       g_mutex_lock (&priv->client_mutex);
        cache_age_save = pk_client_get_cache_age (priv->client);
        pk_client_set_cache_age (priv->client, 60 * 60 * 24 * 7); /* once per week */
-       gs_packagekit_helper_add_app (helper, app);
        results = pk_client_upgrade_system (priv->client,
                                            pk_bitfield_from_enums (PK_TRANSACTION_FLAG_ENUM_SIMULATE, -1),
                                            gs_app_get_version (app),
@@ -615,6 +597,7 @@ gs_plugin_packagekit_refine_distro_upgrade (GsPlugin *plugin,
                                            gs_packagekit_helper_cb, helper,
                                            error);
        pk_client_set_cache_age (priv->client, cache_age_save);
+       g_mutex_unlock (&priv->client_mutex);
 
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to refine distro upgrade: ");
diff --git a/plugins/packagekit/gs-plugin-packagekit-refresh.c 
b/plugins/packagekit/gs-plugin-packagekit-refresh.c
index 488f4b85..512d6779 100644
--- a/plugins/packagekit/gs-plugin-packagekit-refresh.c
+++ b/plugins/packagekit/gs-plugin-packagekit-refresh.c
@@ -57,24 +57,21 @@ _download_only (GsPlugin *plugin, GsAppList *list,
        g_autoptr(PkPackageSack) sack = NULL;
        g_autoptr(PkResults) results2 = NULL;
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
+       /* get the list of packages to update */
+       gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
 
+       g_mutex_lock (&priv->task_mutex);
        /* never refresh the metadata here as this can surprise the frontend if
         * we end up downloading a different set of packages than what was
         * shown to the user */
        pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT);
-
-       /* get the list of packages to update */
-       gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
        results = pk_client_get_updates (PK_CLIENT (priv->task),
                                         pk_bitfield_value (PK_FILTER_ENUM_NONE),
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                return FALSE;
        }
@@ -88,11 +85,17 @@ _download_only (GsPlugin *plugin, GsAppList *list,
                GsApp *app = gs_app_list_index (list, i);
                gs_packagekit_helper_add_app (helper, app);
        }
+       g_mutex_lock (&priv->task_mutex);
+       /* never refresh the metadata here as this can surprise the frontend if
+        * we end up downloading a different set of packages than what was
+        * shown to the user */
+       pk_client_set_cache_age (PK_CLIENT (priv->task), G_MAXUINT);
        results2 = pk_task_update_packages_sync (priv->task,
                                                 package_ids,
                                                 cancellable,
                                                 gs_packagekit_helper_cb, helper,
                                                 error);
+       g_mutex_unlock (&priv->task_mutex);
        if (results2 == NULL) {
                gs_plugin_packagekit_error_convert (error);
                return FALSE;
@@ -143,24 +146,21 @@ gs_plugin_refresh (GsPlugin *plugin,
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(GsApp) app_dl = gs_app_new (gs_plugin_get_name (plugin));
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
+       gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
+       gs_packagekit_helper_add_app (helper, app_dl);
 
+       g_mutex_lock (&priv->task_mutex);
        /* cache age of 1 is user-initiated */
        pk_client_set_background (PK_CLIENT (priv->task), cache_age > 1);
        pk_client_set_cache_age (PK_CLIENT (priv->task), cache_age);
-
        /* refresh the metadata */
-       gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
-       gs_packagekit_helper_add_app (helper, app_dl);
        results = pk_client_refresh_cache (PK_CLIENT (priv->task),
                                           FALSE /* force */,
                                           cancellable,
                                           gs_packagekit_helper_cb, helper,
                                           error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                return FALSE;
        }
diff --git a/plugins/packagekit/gs-plugin-packagekit-upgrade.c 
b/plugins/packagekit/gs-plugin-packagekit-upgrade.c
index 50a74298..3a04507f 100644
--- a/plugins/packagekit/gs-plugin-packagekit-upgrade.c
+++ b/plugins/packagekit/gs-plugin-packagekit-upgrade.c
@@ -56,7 +56,6 @@ gs_plugin_app_upgrade_download (GsPlugin *plugin,
        GsPluginData *priv = gs_plugin_get_data (plugin);
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        /* only process this app if was created by this plugin */
        if (g_strcmp0 (gs_app_get_management_plugin (app), "packagekit") != 0)
@@ -66,19 +65,17 @@ gs_plugin_app_upgrade_download (GsPlugin *plugin,
        if (gs_app_get_kind (app) != AS_APP_KIND_OS_UPGRADE)
                return TRUE;
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
-
        /* ask PK to download enough packages to upgrade the system */
        gs_app_set_state (app, AS_APP_STATE_INSTALLING);
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_task_upgrade_system_sync (priv->task,
                                               gs_app_get_version (app),
                                               PK_UPGRADE_KIND_ENUM_COMPLETE,
                                               cancellable,
                                               gs_packagekit_helper_cb, helper,
                                               error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                gs_app_set_state_recover (app);
                return FALSE;
diff --git a/plugins/packagekit/gs-plugin-packagekit-url-to-app.c 
b/plugins/packagekit/gs-plugin-packagekit-url-to-app.c
index e7acbc24..d9c2ed66 100644
--- a/plugins/packagekit/gs-plugin-packagekit-url-to-app.c
+++ b/plugins/packagekit/gs-plugin-packagekit-url-to-app.c
@@ -57,7 +57,6 @@ gs_plugin_url_to_app (GsPlugin *plugin,
        g_autoptr(GsOsRelease) os_release = NULL;
        g_autoptr(GPtrArray) packages = NULL;
        g_autoptr(GPtrArray) details = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
 
        path = gs_utils_get_url_path (url);
@@ -84,18 +83,17 @@ gs_plugin_url_to_app (GsPlugin *plugin,
        gs_app_set_kind (app, AS_APP_KIND_GENERIC);
        gs_app_set_bundle_kind (app, AS_BUNDLE_KIND_PACKAGE);
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->client_mutex);
-       g_assert (locker != NULL);
-
        package_ids = g_new0 (gchar *, 2);
        package_ids[0] = g_strdup (path);
+
+       g_mutex_lock (&priv->client_mutex);
        results = pk_client_resolve (priv->client,
                                     pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST, PK_FILTER_ENUM_ARCH, -1),
                                     package_ids,
                                     cancellable,
                                     gs_packagekit_helper_cb, helper,
                                     error);
+       g_mutex_unlock (&priv->client_mutex);
 
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to resolve package_ids: ");
diff --git a/plugins/packagekit/gs-plugin-packagekit.c b/plugins/packagekit/gs-plugin-packagekit.c
index 0dda7bbb..e51a2666 100644
--- a/plugins/packagekit/gs-plugin-packagekit.c
+++ b/plugins/packagekit/gs-plugin-packagekit.c
@@ -64,22 +64,19 @@ gs_plugin_add_sources_related (GsPlugin *plugin,
        gboolean ret = TRUE;
        g_autoptr(GsAppList) installed = gs_app_list_new ();
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        filter = pk_bitfield_from_enums (PK_FILTER_ENUM_INSTALLED,
                                         PK_FILTER_ENUM_NEWEST,
                                         PK_FILTER_ENUM_ARCH,
                                         PK_FILTER_ENUM_NOT_COLLECTIONS,
                                         -1);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_get_packages (PK_CLIENT(priv->task),
                                           filter,
                                           cancellable,
                                           gs_packagekit_helper_cb, helper,
                                           error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                g_prefix_error (error, "failed to get sources related: ");
                return FALSE;
@@ -130,22 +127,19 @@ gs_plugin_add_sources (GsPlugin *plugin,
        g_autoptr(GHashTable) hash = NULL;
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) array = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* ask PK for the repo details */
        filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NOT_SOURCE,
                                         PK_FILTER_ENUM_NOT_DEVELOPMENT,
                                         PK_FILTER_ENUM_NOT_SUPPORTED,
                                         -1);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_get_repo_list (PK_CLIENT(priv->task),
                                           filter,
                                           cancellable,
                                           gs_packagekit_helper_cb, helper,
                                           error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error))
                return FALSE;
        hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
@@ -187,20 +181,17 @@ gs_plugin_app_origin_repo_enable (GsPlugin *plugin,
        GsPluginData *priv = gs_plugin_get_data (plugin);
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* do sync call */
        gs_plugin_status_update (plugin, app, GS_PLUGIN_STATUS_WAITING);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_repo_enable (PK_CLIENT (priv->task),
                                         gs_app_get_origin (app),
                                         TRUE,
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                gs_utils_error_add_origin_id (error, app);
                return FALSE;
@@ -222,22 +213,19 @@ gs_plugin_repo_enable (GsPlugin *plugin,
        GsPluginData *priv = gs_plugin_get_data (plugin);
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* do sync call */
        gs_plugin_status_update (plugin, app, GS_PLUGIN_STATUS_WAITING);
        gs_app_set_state (app, AS_APP_STATE_INSTALLING);
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_repo_enable (PK_CLIENT (priv->task),
                                         gs_app_get_id (app),
                                         TRUE,
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                gs_app_set_state_recover (app);
                gs_utils_error_add_origin_id (error, app);
@@ -266,7 +254,6 @@ gs_plugin_app_install (GsPlugin *plugin,
        g_auto(GStrv) package_ids = NULL;
        g_autoptr(GPtrArray) array_package_ids = NULL;
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        /* only process this app if was created by this plugin */
        if (g_strcmp0 (gs_app_get_management_plugin (app),
@@ -307,17 +294,15 @@ gs_plugin_app_install (GsPlugin *plugin,
                 * is probably some kind of hard-to-debug race in the daemon. */
                g_usleep (G_USEC_PER_SEC * 3);
 
-               /* packagekit-glib is not threadsafe */
-               locker = g_mutex_locker_new (&priv->task_mutex);
-               g_assert (locker != NULL);
-
                /* actually install the package */
                gs_packagekit_helper_add_app (helper, app);
+               g_mutex_lock (&priv->task_mutex);
                results = pk_task_install_packages_sync (priv->task,
                                                         package_ids,
                                                         cancellable,
                                                         gs_packagekit_helper_cb, helper,
                                                         error);
+               g_mutex_unlock (&priv->task_mutex);
                if (!gs_plugin_packagekit_results_valid (results, error)) {
                        gs_app_set_state_recover (app);
                        return FALSE;
@@ -380,10 +365,6 @@ gs_plugin_app_install (GsPlugin *plugin,
                        return FALSE;
                }
 
-               /* packagekit-glib is not threadsafe */
-               locker = g_mutex_locker_new (&priv->task_mutex);
-               g_assert (locker != NULL);
-
                gs_app_set_state (app, AS_APP_STATE_INSTALLING);
                addons = gs_app_get_addons (app);
                for (i = 0; i < gs_app_list_length (addons); i++) {
@@ -392,11 +373,13 @@ gs_plugin_app_install (GsPlugin *plugin,
                                gs_app_set_state (addon, AS_APP_STATE_INSTALLING);
                }
                gs_packagekit_helper_add_app (helper, app);
+               g_mutex_lock (&priv->task_mutex);
                results = pk_task_install_packages_sync (priv->task,
                                                         (gchar **) array_package_ids->pdata,
                                                         cancellable,
                                                         gs_packagekit_helper_cb, helper,
                                                         error);
+               g_mutex_unlock (&priv->task_mutex);
                if (!gs_plugin_packagekit_results_valid (results, error)) {
                        gs_app_set_state_recover (app);
                        return FALSE;
@@ -417,17 +400,15 @@ gs_plugin_app_install (GsPlugin *plugin,
                local_filename = g_file_get_path (gs_app_get_local_file (app));
                package_ids = g_strsplit (local_filename, "\t", -1);
 
-               /* packagekit-glib is not threadsafe */
-               locker = g_mutex_locker_new (&priv->task_mutex);
-               g_assert (locker != NULL);
-
                gs_app_set_state (app, AS_APP_STATE_INSTALLING);
                gs_packagekit_helper_add_app (helper, app);
+               g_mutex_lock (&priv->task_mutex);
                results = pk_task_install_files_sync (priv->task,
                                                      package_ids,
                                                      cancellable,
                                                      gs_packagekit_helper_cb, helper,
                                                      error);
+               g_mutex_unlock (&priv->task_mutex);
                if (!gs_plugin_packagekit_results_valid (results, error)) {
                        gs_app_set_state_recover (app);
                        return FALSE;
@@ -465,22 +446,19 @@ gs_plugin_repo_disable (GsPlugin *plugin,
        GsPluginData *priv = gs_plugin_get_data (plugin);
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* do sync call */
        gs_plugin_status_update (plugin, app, GS_PLUGIN_STATUS_WAITING);
        gs_app_set_state (app, AS_APP_STATE_REMOVING);
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_repo_enable (PK_CLIENT (priv->task),
                                         gs_app_get_id (app),
                                         FALSE,
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                gs_app_set_state_recover (app);
                gs_utils_error_add_origin_id (error, app);
@@ -507,7 +485,6 @@ gs_plugin_app_remove (GsPlugin *plugin,
        guint cnt = 0;
        g_autoptr(PkResults) results = NULL;
        g_auto(GStrv) package_ids = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
 
        /* only process this app if was created by this plugin */
        if (g_strcmp0 (gs_app_get_management_plugin (app),
@@ -542,19 +519,17 @@ gs_plugin_app_remove (GsPlugin *plugin,
                return FALSE;
        }
 
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
-
        /* do the action */
        gs_app_set_state (app, AS_APP_STATE_REMOVING);
        gs_packagekit_helper_add_app (helper, app);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_task_remove_packages_sync (priv->task,
                                                package_ids,
                                                TRUE, GS_PACKAGEKIT_AUTOREMOVE,
                                                cancellable,
                                                gs_packagekit_helper_cb, helper,
                                                error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error)) {
                gs_app_set_state_recover (app);
                return FALSE;
@@ -605,19 +580,16 @@ gs_plugin_add_updates (GsPlugin *plugin,
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
        g_autoptr(GPtrArray) array = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* do sync call */
        gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_get_updates (PK_CLIENT (priv->task),
                                         pk_bitfield_value (PK_FILTER_ENUM_NONE),
                                         cancellable,
                                         gs_packagekit_helper_cb, helper,
                                         error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error))
                return FALSE;
 
@@ -643,23 +615,20 @@ gs_plugin_add_search_files (GsPlugin *plugin,
        PkBitfield filter;
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* do sync call */
        gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
        filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST,
                                         PK_FILTER_ENUM_ARCH,
                                         -1);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_search_files (PK_CLIENT (priv->task),
                                          filter,
                                          search,
                                          cancellable,
                                          gs_packagekit_helper_cb, helper,
                                          error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error))
                return FALSE;
 
@@ -678,23 +647,20 @@ gs_plugin_add_search_what_provides (GsPlugin *plugin,
        PkBitfield filter;
        g_autoptr(GsPackagekitHelper) helper = gs_packagekit_helper_new (plugin);
        g_autoptr(PkResults) results = NULL;
-       g_autoptr(GMutexLocker) locker = NULL;
-
-       /* packagekit-glib is not threadsafe */
-       locker = g_mutex_locker_new (&priv->task_mutex);
-       g_assert (locker != NULL);
 
        /* do sync call */
        gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);
        filter = pk_bitfield_from_enums (PK_FILTER_ENUM_NEWEST,
                                         PK_FILTER_ENUM_ARCH,
                                         -1);
+       g_mutex_lock (&priv->task_mutex);
        results = pk_client_what_provides (PK_CLIENT (priv->task),
                                           filter,
                                           search,
                                           cancellable,
                                           gs_packagekit_helper_cb, helper,
                                           error);
+       g_mutex_unlock (&priv->task_mutex);
        if (!gs_plugin_packagekit_results_valid (results, error))
                return FALSE;
 


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