Commit 18c0f8f1 authored by Mohammed Sadiq's avatar Mohammed Sadiq Committed by Mohammed Sadiq
Browse files

history: Fix possible deadlock

history is a singleton object.  It's created and finalized only once in
an application's lifetime.  But in tests, the same object may be reused
after closing database, which can result in races that lock the GTask to
never complete.

Free object data in dispose and explicitly run dispose when the database
is closed so that we don't have wait for GObject to free data which may
be delayed.
parent 35b3d1ae
......@@ -287,6 +287,18 @@ history_close_db (ChattyHistory *self,
status = sqlite3_close (self->db);
if (status == SQLITE_OK) {
/*
* We can’t know when will @self associated with the task will
* be unref. So chatty_history_get_default() called immediately
* after this may return the @self that is yet to be free. But
* as the worker_thread is exited after closing the database, any
* actions with the same @self will not execute, and so the tasks
* will take ∞ time to complete.
*
* So Instead of relying on GObject to free the object, Let’s
* explicitly run dispose
*/
g_object_run_dispose (G_OBJECT (self));
g_object_unref (self);
g_debug ("Database closed successfully");
g_task_return_boolean (task, TRUE);
......@@ -788,6 +800,16 @@ chatty_history_worker (gpointer user_data)
return NULL;
}
static void
chatty_history_dispose (GObject *object)
{
ChattyHistory *self = (ChattyHistory *)object;
g_clear_pointer (&self->worker_thread, g_thread_unref);
G_OBJECT_CLASS (chatty_history_parent_class)->dispose (object);
}
static void
chatty_history_finalize (GObject *object)
{
......@@ -797,7 +819,6 @@ chatty_history_finalize (GObject *object)
g_warning ("Database not closed");
g_clear_pointer (&self->queue, g_async_queue_unref);
g_clear_pointer (&self->worker_thread, g_thread_unref);
G_OBJECT_CLASS (chatty_history_parent_class)->finalize (object);
}
......@@ -807,6 +828,7 @@ chatty_history_class_init (ChattyHistoryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = chatty_history_dispose;
object_class->finalize = chatty_history_finalize;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment