From 364467242855a2041cd6b19a80756b6ef45ff65e Mon Sep 17 00:00:00 2001 From: Nathan Yang Date: Fri, 15 Aug 2025 16:42:28 +0800 Subject: [PATCH] bugfix: interop mutex Signed-off-by: Nathan Yang --- .../context/application_context.cpp | 30 +++++++++++-------- .../context/application_context.h | 3 ++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index 6cf57e68463..f46585ea50c 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -71,6 +71,12 @@ void ApplicationContext::UnregisterAbilityLifecycleCallback( } } +std::vector> ApplicationContext::GetInteropCallbacks() +{ + std::lock_guard lock(interopCallbackLock_); + return interopCallbacks_; +} + void ApplicationContext::RegisterInteropAbilityLifecycleCallback( std::shared_ptr callback) { @@ -163,8 +169,8 @@ void ApplicationContext::DispatchOnAbilityCreate(std::shared_ptr TAG_LOGE(AAFwkTag::APPKIT, "null ability"); return; } - std::lock_guard lock(interopCallbackLock_); - for (auto callback : interopCallbacks_) { + auto callbacksCopy = GetInteropCallbacks(); + for (auto callback : callbacksCopy) { if (callback != nullptr) { TAG_LOGD(AAFwkTag::APPKIT, "call interop onAbilityCreate"); callback->OnAbilityCreate(ability); @@ -210,8 +216,8 @@ void ApplicationContext::DispatchOnWindowStageCreate(std::shared_ptr lock(interopCallbackLock_); - for (auto callback : interopCallbacks_) { + auto callbacksCopy = GetInteropCallbacks(); + for (auto callback : callbacksCopy) { if (callback != nullptr) { TAG_LOGD(AAFwkTag::APPKIT, "call interop OnWindowStageCreate"); callback->OnWindowStageCreate(ability, windowStage); @@ -257,8 +263,8 @@ void ApplicationContext::DispatchOnWindowStageDestroy(std::shared_ptr lock(interopCallbackLock_); - for (auto callback : interopCallbacks_) { + auto callbacksCopy = GetInteropCallbacks(); + for (auto callback : callbacksCopy) { if (callback != nullptr) { TAG_LOGD(AAFwkTag::APPKIT, "call interop OnWindowStageDestroy"); callback->OnWindowStageDestroy(ability, windowStage); @@ -333,8 +339,8 @@ void ApplicationContext::DispatchOnAbilityDestroy(std::shared_ptr TAG_LOGE(AAFwkTag::APPKIT, "null ability"); return; } - std::lock_guard lock(interopCallbackLock_); - for (auto callback : interopCallbacks_) { + auto callbacksCopy = GetInteropCallbacks(); + for (auto callback : callbacksCopy) { if (callback != nullptr) { TAG_LOGD(AAFwkTag::APPKIT, "call interop OnAbilityDestroy"); callback->OnAbilityDestroy(ability); @@ -377,8 +383,8 @@ void ApplicationContext::DispatchOnAbilityForeground(std::shared_ptr lock(interopCallbackLock_); - for (auto callback : interopCallbacks_) { + auto callbacksCopy = GetInteropCallbacks(); + for (auto callback : callbacksCopy) { if (callback != nullptr) { TAG_LOGD(AAFwkTag::APPKIT, "call interop OnAbilityForeground"); callback->OnAbilityForeground(ability); @@ -421,8 +427,8 @@ void ApplicationContext::DispatchOnAbilityBackground(std::shared_ptr lock(interopCallbackLock_); - for (auto callback : interopCallbacks_) { + auto callbacksCopy = GetInteropCallbacks(); + for (auto callback : callbacksCopy) { if (callback != nullptr) { TAG_LOGD(AAFwkTag::APPKIT, "call interop OnAbilityBackground"); callback->OnAbilityBackground(ability); diff --git a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h index cbf6c445f5a..4080b633e2d 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -189,6 +189,9 @@ protected: return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId); } +private: + std::vector> GetInteropCallbacks(); + private: std::shared_ptr contextImpl_; static std::vector> callbacks_; -- Gitee