From e1a6ea502b75171a26d667c2bee09fc304acffcb Mon Sep 17 00:00:00 2001 From: StreamBuns Date: Mon, 30 Mar 2026 15:32:52 +0800 Subject: [PATCH] fix saveState crash Signed-off-by: StreamBuns --- .../ability/native/recovery/app_recovery.cpp | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/frameworks/native/ability/native/recovery/app_recovery.cpp b/frameworks/native/ability/native/recovery/app_recovery.cpp index d1009cabc9..a77e76be5f 100644 --- a/frameworks/native/ability/native/recovery/app_recovery.cpp +++ b/frameworks/native/ability/native/recovery/app_recovery.cpp @@ -46,6 +46,28 @@ namespace OHOS { namespace AppExecFwk { +namespace{ + +static panda::ecmascript::EcmaVM* GetVMFromAbility(const std::shared_ptr& abilityPtr) +{ + if (!abilityPtr) { + return nullptr; + } + OHOS::AbilityRuntime::JsUIAbility& jsAbility = static_cast(*abilityPtr); + AbilityRuntime::JsRuntime& runtime = const_cast(jsAbility.GetJsRuntime()); + return runtime.GetEcmaVm(); +} + +void DisallowCrossThreadExecutionInRecovery() +{ + #ifdef SUPPORT_SCREEN + panda::ecmascript::EcmaVM* vm = GetVMFromAbility(abilityPtr); + if (vm != nullptr) { + panda::JSNApi::DisallowCrossThreadExecution(vm); + } + #endif +} +} std::mutex g_mutex; std::atomic g_blocked = false; const int DELAY_TIME = 1000; @@ -244,9 +266,7 @@ bool AppRecovery::ExecuteFreezeCallbackWithVMSafety(const std::shared_ptr(*abilityPtr); - AbilityRuntime::JsRuntime& runtime = const_cast(jsAbility.GetJsRuntime()); - panda::ecmascript::EcmaVM* vm = runtime.GetEcmaVm(); + panda::ecmascript::EcmaVM* vm = GetVMFromAbility(abilityPtr); if (!panda::JSNApi::CheckAndSetAllowCrossThreadExecution(vm)) { TAG_LOGE(AAFwkTag::RECOVERY, "VM is in gc process"); return false; @@ -256,9 +276,6 @@ bool AppRecovery::ExecuteFreezeCallbackWithVMSafety(const std::shared_ptrfreezeCallback(); TAG_LOGW(AAFwkTag::RECOVERY, "Freeze callback execution completed"); } -#ifdef SUPPORT_SCREEN - panda::JSNApi::DisallowCrossThreadExecution(vm); -#endif return true; }