From aaab2dc74e8b4448f0ecc1bdd0c5310f0155f801 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 | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dbus/gattlib_adapter.c b/dbus/gattlib_adapter.c index 28ef9a2..d3d4b61 100644 --- a/dbus/gattlib_adapter.c +++ b/dbus/gattlib_adapter.c @@ -690,8 +690,18 @@ int gattlib_adapter_close(gattlib_adapter_t* adapter) { GATTLIB_LOG(GATTLIB_DEBUG, "Bluetooth adapter %s was scanning. We stop the scan", adapter->name); gattlib_adapter_scan_disable(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); + _wait_scan_loop_stop_scanning(adapter); + 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