From 2134027be81bf057b474a8c9d0a64f1a885bbb89 Mon Sep 17 00:00:00 2001 From: Arkadiusz Bokowy Date: Wed, 9 Nov 2022 08:11:04 +0100 Subject: [PATCH] Fix BLE commissioning deadlock caused by 0e41b19 (#23545) --- src/platform/Linux/PlatformManagerImpl.cpp | 8 ++++++++ src/platform/Linux/PlatformManagerImpl.h | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index 9daa120976dfd4..e29f04bd1d0383 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -288,6 +288,14 @@ CHIP_ERROR PlatformManagerImpl::RunOnGLibMainLoopThread(GSourceFunc callback, vo VerifyOrReturnError(context != nullptr, (ChipLogDetail(DeviceLayer, "Failed to get GLib main loop context"), CHIP_ERROR_INTERNAL)); + // If we've been called from the GLib main loop thread itself, there is no reason to wait + // for the callback, as it will be executed immediately by the g_main_context_invoke() call + // below. Using a callback indirection in this case would cause a deadlock. + if (g_main_context_is_owner(context)) + { + wait = false; + } + if (wait) { std::unique_lock lock(mGLibMainLoopCallbackIndirectionMutex); diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index bc5bc7c10c4e4a..2b1bad0ea7fd2c 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -70,7 +70,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener * @brief Convenience method to require less casts to void pointers. */ template - CHIP_ERROR ScheduleOnGLibMainLoopThread(int (*callback)(T *), T * userData, bool wait = false) + CHIP_ERROR ScheduleOnGLibMainLoopThread(gboolean (*callback)(T *), T * userData, bool wait = false) { return RunOnGLibMainLoopThread(G_SOURCE_FUNC(callback), userData, wait); }