From bc06c6d022bfcef8b042610f4b6f206876f08e5c Mon Sep 17 00:00:00 2001 From: Olivier Martin Date: Wed, 10 Apr 2024 11:07:10 +0200 Subject: [PATCH] gattlib_adapter_close: Do not block mutex while waiting for scan_loop_thread to complete --- dbus/gattlib_adapter.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/dbus/gattlib_adapter.c b/dbus/gattlib_adapter.c index 50cf02e..3cea5cf 100644 --- a/dbus/gattlib_adapter.c +++ b/dbus/gattlib_adapter.c @@ -699,7 +699,16 @@ int gattlib_adapter_close(gattlib_adapter_t* adapter) { gattlib_adapter_scan_disable(adapter); _wait_scan_loop_stop_scanning(adapter); + + // We must release gattlib mutex to not block the library + // We must also increase reference counter to not wait for a thread that has been freed + GThread *scan_loop_thread = adapter->backend.ble_scan.scan_loop_thread; + g_thread_ref(scan_loop_thread); + g_rec_mutex_unlock(&m_gattlib_mutex); g_thread_join(adapter->backend.ble_scan.scan_loop_thread); + // At this stage scan_loop_thread should have completed + g_rec_mutex_lock(&m_gattlib_mutex); + g_thread_unref(scan_loop_thread); } // Unref/Free the adapter