Skip to content

Commit

Permalink
Disconnect monitors in dnf_repo_loader_finalize() (RhBug:2070153)
Browse files Browse the repository at this point in the history
This fixes a crash that could occur due to dangling monitors there we
executed even when the `DnfRepoLoader` was already freed.

https://bugzilla.redhat.com/show_bug.cgi?id=2070153
  • Loading branch information
kontura authored and m-blaha committed Jun 6, 2023
1 parent 2362930 commit 3fca06e
Showing 1 changed file with 40 additions and 33 deletions.
73 changes: 40 additions & 33 deletions libdnf/dnf-repo-loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,6 @@ static guint signals[SIGNAL_LAST] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE(DnfRepoLoader, dnf_repo_loader, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (static_cast<DnfRepoLoaderPrivate *>(dnf_repo_loader_get_instance_private (o)))

/**
* dnf_repo_loader_finalize:
**/
static void
dnf_repo_loader_finalize(GObject *object)
{
DnfRepoLoader *self = DNF_REPO_LOADER(object);
DnfRepoLoaderPrivate *priv = GET_PRIVATE(self);

if (priv->context != NULL)
g_object_remove_weak_pointer(G_OBJECT(priv->context),
(void **) &priv->context);
g_ptr_array_unref(priv->monitor_repos);
g_object_unref(priv->volume_monitor);
g_ptr_array_unref(priv->repos);

G_OBJECT_CLASS(dnf_repo_loader_parent_class)->finalize(object);
}

/**
* dnf_repo_loader_invalidate:
*/
Expand All @@ -104,6 +85,46 @@ dnf_repo_loader_mount_changed_cb(GVolumeMonitor *vm, GMount *mount, DnfRepoLoade
dnf_repo_loader_invalidate(self);
}

/**
* dnf_repo_loader_directory_changed_cb:
**/
static void
dnf_repo_loader_directory_changed_cb(GFileMonitor *monitor_,
GFile *file, GFile *other_file,
GFileMonitorEvent event_type,
DnfRepoLoader *self)
{
g_debug("emit changed(ReposDir changed)");
g_signal_emit(self, signals[SIGNAL_CHANGED], 0);
dnf_repo_loader_invalidate(self);
}

/**
* dnf_repo_loader_finalize:
**/
static void
dnf_repo_loader_finalize(GObject *object)
{
DnfRepoLoader *self = DNF_REPO_LOADER(object);
DnfRepoLoaderPrivate *priv = GET_PRIVATE(self);

if (priv->context != NULL)
g_object_remove_weak_pointer(G_OBJECT(priv->context),
(void **) &priv->context);
guint i;
for (i = 0; i < priv->monitor_repos->len; i++) {
auto repo_file_monitor = static_cast<GFileMonitor *>(g_ptr_array_index(priv->monitor_repos, i));
g_signal_handlers_disconnect_by_func(repo_file_monitor, (gpointer) dnf_repo_loader_directory_changed_cb, self);
}
g_ptr_array_unref(priv->monitor_repos);

g_signal_handlers_disconnect_by_func(priv->volume_monitor, (gpointer) dnf_repo_loader_mount_changed_cb, self);
g_object_unref(priv->volume_monitor);
g_ptr_array_unref(priv->repos);

G_OBJECT_CLASS(dnf_repo_loader_parent_class)->finalize(object);
}

/**
* dnf_repo_loader_init:
**/
Expand Down Expand Up @@ -525,20 +546,6 @@ dnf_repo_loader_get_repo_by_id(DnfRepoLoader *self, const gchar *id, GError **er
return NULL;
} CATCH_TO_GERROR(NULL)

/**
* dnf_repo_loader_directory_changed_cb:
**/
static void
dnf_repo_loader_directory_changed_cb(GFileMonitor *monitor_,
GFile *file, GFile *other_file,
GFileMonitorEvent event_type,
DnfRepoLoader *self)
{
g_debug("emit changed(ReposDir changed)");
g_signal_emit(self, signals[SIGNAL_CHANGED], 0);
dnf_repo_loader_invalidate(self);
}

/**
* dnf_repo_loader_setup_monitor:
*/
Expand Down

0 comments on commit 3fca06e

Please sign in to comment.