From fb1d37a6463d88f801e7faaee96c2db5b04d77ae Mon Sep 17 00:00:00 2001 From: hanhaibin Date: Fri, 21 Jan 2022 00:42:36 +0800 Subject: [PATCH 1/4] Desktop abnormal process recovery Signed-off-by: hanhaibin --- .../include/ability_manager_service.h | 1 + services/abilitymgr/include/ability_record.h | 5 ++ .../include/ams_configuration_parameter.h | 6 ++ .../abilitymgr/include/mission_list_manager.h | 5 +- .../resource/ams_service_config.json | 3 +- .../src/ability_manager_service.cpp | 52 ++++++++++----- services/abilitymgr/src/ability_record.cpp | 26 ++++++++ .../src/ams_configuration_parameter.cpp | 6 ++ .../abilitymgr/src/mission_list_manager.cpp | 65 +++++++++++++++++-- 9 files changed, 146 insertions(+), 23 deletions(-) diff --git a/services/abilitymgr/include/ability_manager_service.h b/services/abilitymgr/include/ability_manager_service.h index def74abaddb..b5cc5184beb 100644 --- a/services/abilitymgr/include/ability_manager_service.h +++ b/services/abilitymgr/include/ability_manager_service.h @@ -655,6 +655,7 @@ public: std::list> GetConnectRecordListByCallback(sptr callback); void OnAbilityDied(std::shared_ptr abilityRecord); + void GetMaxRestartNum(int &max); /** * wait for starting system ui. diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index 087b7b1ce4d..ce6135da0bd 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -817,7 +817,12 @@ public: std::weak_ptr missionList_; std::weak_ptr mission_; int32_t missionId_ = -1; + + int32_t restartCount_ = -1; + int32_t restratMax_ = -1; + std::string specifiedFlag_; + }; class AbilityRecordNew : public AbilityRecord { diff --git a/services/abilitymgr/include/ams_configuration_parameter.h b/services/abilitymgr/include/ams_configuration_parameter.h index c04fee90ebb..ddb4c9fb37d 100644 --- a/services/abilitymgr/include/ams_configuration_parameter.h +++ b/services/abilitymgr/include/ams_configuration_parameter.h @@ -38,6 +38,7 @@ const std::string SYSTEM_ORIENTATION {"system_orientation"}; const std::string STARTUP_CONTACTS {"startup_contacts"}; const std::string STARTUP_MMS {"startup_mms"}; const std::string USE_NEW_MISSION {"use_new_mission"}; +const std::string ROOT_LAUNCHER_RESTART_MAX {"root_launcher_restart_max"}; } // namespace AmsConfig enum class SatrtUiMode { STATUSBAR = 1, NAVIGATIONBAR = 2, STARTUIBOTH = 3 }; @@ -108,6 +109,10 @@ public: * return true if use mission list, false if use mission stack. */ bool IsUseNewMission() const; + /** + * Get the max number of restart. + */ + int GetMaxRestartNum() const; enum { READ_OK = 0, READ_FAIL = 1, READ_JSON_FAIL = 2 }; @@ -131,6 +136,7 @@ private: bool canStartContacts {false}; bool canStartMms {false}; bool useNewMission_ {false}; + int maxRestartNum_ = 0; std::string orientation_ {""}; int missionSaveTime_ {12 * 60 * 60 * 1000}; std::map memThreshold_; diff --git a/services/abilitymgr/include/mission_list_manager.h b/services/abilitymgr/include/mission_list_manager.h index 7e4ab021029..05058c2a950 100755 --- a/services/abilitymgr/include/mission_list_manager.h +++ b/services/abilitymgr/include/mission_list_manager.h @@ -208,7 +208,7 @@ public: * * @param abilityRecord the died ability */ - void OnAbilityDied(std::shared_ptr abilityRecord); + void OnAbilityDied(std::shared_ptr abilityRecord, int32_t currentUserId); /** * Get mission id by target ability token. @@ -319,6 +319,9 @@ private: bool IsPC(); std::shared_ptr GetMissionBySpecifiedFlag(const std::string &flag) const; + void HandleLoadTimeout(const std::shared_ptr &ability); + void HandleForgroundNewTimeout(const std::shared_ptr &ability); + private: int userId_; std::recursive_mutex managerLock_; diff --git a/services/abilitymgr/resource/ams_service_config.json b/services/abilitymgr/resource/ams_service_config.json index 392255de15f..3eb6c45e883 100644 --- a/services/abilitymgr/resource/ams_service_config.json +++ b/services/abilitymgr/resource/ams_service_config.json @@ -7,7 +7,8 @@ "startup_phone_service" : true, "startup_contacts":true, "startup_mms":true, - "mission_save_time" : 43200000 + "mission_save_time" : 43200000, + "root_launcher_restart_max":15 }, "memorythreshold":{ "home_application": "20" diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 87b35c17059..2ff37d3c004 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -2113,7 +2113,8 @@ void AbilityManagerService::OnAbilityDied(std::shared_ptr ability } if (currentMissionListManager_) { - currentMissionListManager_->OnAbilityDied(abilityRecord); + int32_t currentUserId = userController_? userController_->GetCurrentUserId() : USER_ID_DEFAULT; + currentMissionListManager_->OnAbilityDied(abilityRecord, currentUserId); } } else { if (systemAppManager_ && abilityRecord->IsKernalSystemAbility()) { @@ -2135,6 +2136,13 @@ void AbilityManagerService::OnAbilityDied(std::shared_ptr ability } } +void AbilityManagerService::GetMaxRestartNum(int &max) +{ + if (amsConfigResolver_) { + max = amsConfigResolver_->GetMaxRestartNum(); + } +} + int AbilityManagerService::KillProcess(const std::string &bundleName) { HILOG_DEBUG("Kill process, bundleName: %{public}s", bundleName.c_str()); @@ -2275,14 +2283,17 @@ void AbilityManagerService::HandleLoadTimeOut(int64_t eventId) if (kernalAbilityManager_) { kernalAbilityManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); } + if (currentMissionListManager_) { + currentMissionListManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); + } } else { if (systemAppManager_) { systemAppManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); } + if (currentStackManager_) { + currentStackManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); + } } - if (currentStackManager_) { - currentStackManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); - } } void AbilityManagerService::HandleActiveTimeOut(int64_t eventId) @@ -2293,21 +2304,30 @@ void AbilityManagerService::HandleActiveTimeOut(int64_t eventId) if (kernalAbilityManager_) { kernalAbilityManager_->OnTimeOut(AbilityManagerService::ACTIVE_TIMEOUT_MSG, eventId); } + if (currentMissionListManager_) { + currentMissionListManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); + } } else { if (systemAppManager_) { systemAppManager_->OnTimeOut(AbilityManagerService::ACTIVE_TIMEOUT_MSG, eventId); } - } - if (currentStackManager_) { - currentStackManager_->OnTimeOut(AbilityManagerService::ACTIVE_TIMEOUT_MSG, eventId); + if (currentStackManager_) { + currentStackManager_->OnTimeOut(AbilityManagerService::ACTIVE_TIMEOUT_MSG, eventId); + } } } void AbilityManagerService::HandleInactiveTimeOut(int64_t eventId) { HILOG_DEBUG("Handle inactive timeout."); - if (currentStackManager_) { - currentStackManager_->OnTimeOut(AbilityManagerService::INACTIVE_TIMEOUT_MSG, eventId); + if (useNewMission_) { + if (currentMissionListManager_) { + currentMissionListManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); + } + }else { + if (currentStackManager_) { + currentStackManager_->OnTimeOut(AbilityManagerService::INACTIVE_TIMEOUT_MSG, eventId); + } } } @@ -2318,13 +2338,16 @@ void AbilityManagerService::HandleForegroundNewTimeOut(int64_t eventId) if (kernalAbilityManager_) { kernalAbilityManager_->OnTimeOut(AbilityManagerService::FOREGROUNDNEW_TIMEOUT_MSG, eventId); } + if (currentMissionListManager_) { + currentMissionListManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); + } } else { if (systemAppManager_) { systemAppManager_->OnTimeOut(AbilityManagerService::FOREGROUNDNEW_TIMEOUT_MSG, eventId); } - } - if (currentStackManager_) { - currentStackManager_->OnTimeOut(AbilityManagerService::FOREGROUNDNEW_TIMEOUT_MSG, eventId); + if (currentStackManager_) { + currentStackManager_->OnTimeOut(AbilityManagerService::FOREGROUNDNEW_TIMEOUT_MSG, eventId); + } } } @@ -2589,11 +2612,6 @@ void AbilityManagerService::StartSystemApplication() StartingSystemUiAbility(SatrtUiMode::NAVIGATIONBAR); } - if (amsConfigResolver_->GetPhoneServiceState()) { - HILOG_INFO("start phone service"); - StartingPhoneServiceAbility(); - } - if (amsConfigResolver_->GetStartContactsState()) { HILOG_INFO("start contacts"); StartingContactsAbility(); diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 01e8f54edf5..36318669fd8 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -159,6 +159,11 @@ int AbilityRecord::LoadAbility() return ERR_INVALID_VALUE; } + if (isLauncherRoot_ && isRestarting_ && IsLauncherAbility() && (restartCount_ < 0)) { + HILOG_ERROR("Root launcher restart is out of max count."); + return ERR_INVALID_VALUE;; + } + if (abilityInfo_.type != AppExecFwk::AbilityType::DATA) { if (isKernalSystemAbility_) { SendEvent(AbilityManagerService::LOAD_TIMEOUT_MSG, AbilityManagerService::SYSTEM_UI_TIMEOUT); @@ -304,6 +309,10 @@ void AbilityRecord::SetAbilityState(AbilityState state) mission->UpdateActiveTimestamp(); } } + + if (state == AbilityState::FOREGROUND_NEW) { + SetRestarting(false); + } } void AbilityRecord::SetScheduler(const sptr &scheduler) @@ -890,6 +899,11 @@ void AbilityRecord::Dump(std::vector &info) dumpInfo = " ready #" + std::to_string(isReady_) + " window attached #" + std::to_string(isWindowAttached_) + " launcher #" + std::to_string(isLauncherAbility_); info.push_back(dumpInfo); + + if (isLauncherRoot_) { + dumpInfo = " can restart num #" + std::to_string(restartCount_); + info.push_back(dumpInfo); + } } void AbilityRecord::SetStartTime() @@ -1117,6 +1131,18 @@ bool AbilityRecord::GetPowerState() const void AbilityRecord::SetRestarting(const bool isRestart) { isRestarting_ = isRestart; + + if (restratMax_ < 0) { + auto ams = DelayedSingleton::GetInstance(); + if (ams) { + ams->GetMaxRestartNum(restratMax_); + } + } + + if (isLauncherRoot_ && IsLauncherAbility()) { + restartCount_ = isRestart ? (--restartCount_) : restratMax_; + HILOG_INFO("root launcher restart count: %{public}d", restartCount_); + } } bool AbilityRecord::IsRestarting() const diff --git a/services/abilitymgr/src/ams_configuration_parameter.cpp b/services/abilitymgr/src/ams_configuration_parameter.cpp index 0c132a94d5e..19b314dffb1 100644 --- a/services/abilitymgr/src/ams_configuration_parameter.cpp +++ b/services/abilitymgr/src/ams_configuration_parameter.cpp @@ -82,6 +82,11 @@ bool AmsConfigurationParameter::IsUseNewMission() const return useNewMission_; } +int AmsConfigurationParameter::GetMaxRestartNum() const +{ + return maxRestartNum_; +} + int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath) { HILOG_DEBUG("%{public}s", __func__); @@ -145,6 +150,7 @@ int AmsConfigurationParameter::LoadAppConfigurationForStartUpService(nlohmann::j if (Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(AmsConfig::USE_NEW_MISSION)) { useNewMission_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::USE_NEW_MISSION).get(); } + maxRestartNum_ = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(AmsConfig::ROOT_LAUNCHER_RESTART_MAX).get(); HILOG_INFO("get ams service config succes!"); ret = 0; } diff --git a/services/abilitymgr/src/mission_list_manager.cpp b/services/abilitymgr/src/mission_list_manager.cpp index f5dac5dac55..12204a1361a 100644 --- a/services/abilitymgr/src/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission_list_manager.cpp @@ -1135,20 +1135,59 @@ void MissionListManager::OnTimeOut(uint32_t msgId, int64_t eventId) switch (msgId) { case AbilityManagerService::LOAD_TIMEOUT_MSG: + HandleLoadTimeout(abilityRecord); break; case AbilityManagerService::ACTIVE_TIMEOUT_MSG: break; case AbilityManagerService::INACTIVE_TIMEOUT_MSG: case AbilityManagerService::FOREGROUNDNEW_TIMEOUT_MSG: - if (abilityRecord->GetMission()) { - abilityRecord->GetMission()->SetMovingState(false); - } + HandleForgroundNewTimeout(abilityRecord); break; default: break; } } +void MissionListManager::HandleLoadTimeout(const std::shared_ptr &ability) +{ + if (ability == nullptr) { + HILOG_ERROR("MissionListManager on time out event: ability record is nullptr."); + return; + } + // root launcher load timeout, notify appMs force terminate the ability and restart immediately. + if (ability->IsLauncherAbility() && ability->IsLauncherRoot()) { + ability->SetRestarting(true); + DelayedSingleton::GetInstance()->AttachTimeOut(ability->GetToken()); + HILOG_INFO("Launcher root load timeout, restart."); + DelayedStartLauncher(); + return; + } + + // other +} + +void MissionListManager::HandleForgroundNewTimeout(const std::shared_ptr &ability) +{ + if (ability == nullptr) { + HILOG_ERROR("MissionListManager on time out event: ability record is nullptr."); + return; + } + + if (ability->GetMission()) { + ability->GetMission()->SetMovingState(false); + } + + // root launcher load timeout, notify appMs force terminate the ability and restart immediately. + if (ability->IsLauncherAbility() && ability->IsLauncherRoot()) { + DelayedSingleton::GetInstance()->AttachTimeOut(ability->GetToken()); + HILOG_INFO("Launcher root load timeout, restart."); + DelayedStartLauncher(); + return; + } + + // other +} + std::shared_ptr MissionListManager::GetAbilityRecordByCaller( const std::shared_ptr &caller, int requestCode) { @@ -1186,7 +1225,7 @@ std::shared_ptr MissionListManager::GetAbilityRecordByEventId(int return defaultStandardList_->GetAbilityRecordById(eventId); } -void MissionListManager::OnAbilityDied(std::shared_ptr abilityRecord) +void MissionListManager::OnAbilityDied(std::shared_ptr abilityRecord, int32_t currentUserId) { HILOG_INFO("On ability died."); if (!abilityRecord) { @@ -1199,7 +1238,17 @@ void MissionListManager::OnAbilityDied(std::shared_ptr abilityRec HILOG_ERROR("Ability type is not page."); return; } + std::lock_guard guard(managerLock_); + + if (abilityRecord->IsLauncherRoot() && currentUserId != userId_) { + HILOG_WARN("delay restart root launcher when start user."); + HILOG_INFO("launcher root Ability died, state: INITIAL, %{public}d", __LINE__); + abilityRecord->SetAbilityState(AbilityState::INITIAL); + abilityRecord->SetRestarting(true); + return; + } + HandleAbilityDied(abilityRecord); } @@ -1321,10 +1370,17 @@ void MissionListManager::HandleAbilityDied(std::shared_ptr abilit { HILOG_INFO("Handle Ability Died."); CHECK_POINTER(abilityRecord); + + if (abilityRecord->GetAbilityInfo().type != AbilityType::PAGE) { + HILOG_ERROR("Ability type is not page."); + return; + } + if (abilityRecord->IsLauncherAbility()) { HandleLauncherDied(abilityRecord); return; } + HandleAbilityDiedByDefault(abilityRecord); } @@ -1343,6 +1399,7 @@ void MissionListManager::HandleLauncherDied(std::shared_ptr abili if (ability->IsLauncherRoot()) { HILOG_INFO("launcher root Ability died, state: INITIAL, %{public}d", __LINE__); ability->SetAbilityState(AbilityState::INITIAL); + ability->SetRestarting(true); } else { HILOG_INFO("launcher Ability died, remove, %{public}d", __LINE__); missionList->RemoveMission(mission); -- Gitee From c79ccf2952a6cd56187c68ce8aee4cbe3b21ba2d Mon Sep 17 00:00:00 2001 From: hanhaibin Date: Fri, 21 Jan 2022 02:04:13 +0800 Subject: [PATCH 2/4] Fix codex Signed-off-by: hanhaibin --- services/abilitymgr/include/ability_record.h | 2 -- services/abilitymgr/src/ability_manager_service.cpp | 7 ++++++- services/abilitymgr/src/ability_record.cpp | 8 ++++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index ce6135da0bd..8b1b1370296 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -817,10 +817,8 @@ public: std::weak_ptr missionList_; std::weak_ptr mission_; int32_t missionId_ = -1; - int32_t restartCount_ = -1; int32_t restratMax_ = -1; - std::string specifiedFlag_; }; diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 2ff37d3c004..8bb229103b3 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -2324,7 +2324,7 @@ void AbilityManagerService::HandleInactiveTimeOut(int64_t eventId) if (currentMissionListManager_) { currentMissionListManager_->OnTimeOut(AbilityManagerService::LOAD_TIMEOUT_MSG, eventId); } - }else { + } else { if (currentStackManager_) { currentStackManager_->OnTimeOut(AbilityManagerService::INACTIVE_TIMEOUT_MSG, eventId); } @@ -2612,6 +2612,11 @@ void AbilityManagerService::StartSystemApplication() StartingSystemUiAbility(SatrtUiMode::NAVIGATIONBAR); } + if (amsConfigResolver_->GetPhoneServiceState()) { + HILOG_INFO("start phone service"); + StartingPhoneServiceAbility(); + } + if (amsConfigResolver_->GetStartContactsState()) { HILOG_INFO("start contacts"); StartingContactsAbility(); diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 36318669fd8..19dad167fde 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -161,7 +161,7 @@ int AbilityRecord::LoadAbility() if (isLauncherRoot_ && isRestarting_ && IsLauncherAbility() && (restartCount_ < 0)) { HILOG_ERROR("Root launcher restart is out of max count."); - return ERR_INVALID_VALUE;; + return ERR_INVALID_VALUE; } if (abilityInfo_.type != AppExecFwk::AbilityType::DATA) { @@ -1133,9 +1133,9 @@ void AbilityRecord::SetRestarting(const bool isRestart) isRestarting_ = isRestart; if (restratMax_ < 0) { - auto ams = DelayedSingleton::GetInstance(); - if (ams) { - ams->GetMaxRestartNum(restratMax_); + auto abilityMgr = DelayedSingleton::GetInstance(); + if (abilityMgr) { + abilityMgr->GetMaxRestartNum(restratMax_); } } -- Gitee From 2ae42373b0e06de22a8c8a3502e03d8c443019c4 Mon Sep 17 00:00:00 2001 From: hanhaibin Date: Fri, 21 Jan 2022 02:12:40 +0800 Subject: [PATCH 3/4] Fix codex Signed-off-by: hanhaibin --- services/abilitymgr/include/ability_record.h | 1 - 1 file changed, 1 deletion(-) diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index 8b1b1370296..8400c826cfb 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -820,7 +820,6 @@ public: int32_t restartCount_ = -1; int32_t restratMax_ = -1; std::string specifiedFlag_; - }; class AbilityRecordNew : public AbilityRecord { -- Gitee From 4fa70a55993ff74e01daef84c2fc7236cce75175 Mon Sep 17 00:00:00 2001 From: hanhaibin Date: Fri, 21 Jan 2022 04:54:07 +0800 Subject: [PATCH 4/4] fix bug Signed-off-by: hanhaibin --- services/abilitymgr/src/ability_record.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 19dad167fde..f8d4d9f72e9 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -88,6 +88,11 @@ AbilityRecord::AbilityRecord(const Want &want, const AppExecFwk::AbilityInfo &ab requestCode_(requestCode), compatibleVersion_(apiVersion) { recordId_ = abilityRecordId++; + auto abilityMgr = DelayedSingleton::GetInstance(); + if (abilityMgr) { + abilityMgr->GetMaxRestartNum(restratMax_); + } + restartCount_ = restratMax_; } AbilityRecord::~AbilityRecord() @@ -159,7 +164,7 @@ int AbilityRecord::LoadAbility() return ERR_INVALID_VALUE; } - if (isLauncherRoot_ && isRestarting_ && IsLauncherAbility() && (restartCount_ < 0)) { + if (isLauncherRoot_ && isRestarting_ && IsLauncherAbility() && (restartCount_ < 0) && IsNewVersion()) { HILOG_ERROR("Root launcher restart is out of max count."); return ERR_INVALID_VALUE; } @@ -900,7 +905,7 @@ void AbilityRecord::Dump(std::vector &info) std::to_string(isWindowAttached_) + " launcher #" + std::to_string(isLauncherAbility_); info.push_back(dumpInfo); - if (isLauncherRoot_) { + if (isLauncherRoot_ && IsNewVersion()) { dumpInfo = " can restart num #" + std::to_string(restartCount_); info.push_back(dumpInfo); } @@ -1132,13 +1137,6 @@ void AbilityRecord::SetRestarting(const bool isRestart) { isRestarting_ = isRestart; - if (restratMax_ < 0) { - auto abilityMgr = DelayedSingleton::GetInstance(); - if (abilityMgr) { - abilityMgr->GetMaxRestartNum(restratMax_); - } - } - if (isLauncherRoot_ && IsLauncherAbility()) { restartCount_ = isRestart ? (--restartCount_) : restratMax_; HILOG_INFO("root launcher restart count: %{public}d", restartCount_); -- Gitee