From 5e1b7624928e76cd772c5db7b280ea85ba77889e Mon Sep 17 00:00:00 2001 From: Unsung Lee Date: Sun, 18 May 2025 20:52:21 +0900 Subject: [PATCH] libgdbus: Add a context to handle pending event sources Add a context in dbus_handle to handle pending an event source after calling g_dbus_connection_close_sync(). This is because, g_dbus_connection_close_sync() adds an idle source to the context. If this event source is not handled, then connection ref_cnt is not decremented. It causes that the connection structures and the corresponding threads are left after free. Change-Id: Ibc5597b26ef264e03fb86aef92d72627c7d2b8d1 Signed-off-by: Unsung Lee --- src/libgdbus/libgdbus.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/libgdbus/libgdbus.c b/src/libgdbus/libgdbus.c index 248bb67..f9aeabc 100644 --- a/src/libgdbus/libgdbus.c +++ b/src/libgdbus/libgdbus.c @@ -70,6 +70,7 @@ typedef struct { GList *list_names; /* dbus_name */ GList *list_object; /* dbus_object_handle_s */ pthread_mutex_t mutex; + GMainContext *main_context; } dbus_handle_s; /* path + interfaces */ @@ -247,7 +248,10 @@ static dbus_handle_s *_gdbus_get_connection_private(GBusType bus_type) gdbus_lock(dh); if (!dh->conn) { + dh->main_context = g_main_context_new(); + g_main_context_push_thread_default(dh->main_context); dh->conn = _get_bus_private(bus_type); + g_main_context_pop_thread_default(dh->main_context); dh->priv = TRUE; dh->bus_type = bus_type; if (!dh->conn) @@ -259,6 +263,7 @@ static dbus_handle_s *_gdbus_get_connection_private(GBusType bus_type) return dh; err: if (dh) { + g_main_context_unref(dh->main_context); gdbus_unlock(dh); free(dh); } @@ -652,6 +657,13 @@ int gdbus_free_connection(dbus_handle_h handle) g_error_free(err); err = NULL; } + + if (!g_main_context_iteration(pdh->main_context, FALSE)) { + _W("Cannot dispatch and handle an idle source from connection close"); + g_object_unref(pdh->conn); + } + + g_main_context_unref(pdh->main_context); } /* _free_func_object callback free the data */ -- 2.34.1