!10607 js scene post task may crash because of freed scheduler

Merge pull request !10607 from janjan/js_utils_crash
This commit is contained in:
openharmony_ci 2024-11-08 11:03:44 +00:00 committed by Gitee
commit c53305bc31
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 7 additions and 1 deletions

View File

@ -1518,6 +1518,7 @@ MainThreadScheduler::MainThreadScheduler(napi_env env)
: env_(env)
{
GetMainEventHandler();
envChecker_ = std::make_shared<int>(0);
}
inline void MainThreadScheduler::GetMainEventHandler()
@ -1535,8 +1536,12 @@ inline void MainThreadScheduler::GetMainEventHandler()
void MainThreadScheduler::PostMainThreadTask(Task&& localTask, std::string traceInfo, int64_t delayTime)
{
GetMainEventHandler();
auto task = [env = env_, localTask, traceInfo] () {
auto task = [env = env_, localTask, traceInfo, envChecker = std::weak_ptr<int>(envChecker_)]() {
HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "SCBCb:%s", traceInfo.c_str());
if (envChecker.expired()) {
TLOGNE(WmsLogTag::WMS_MAIN, "post task expired because of invalid scheduler");
return;
}
napi_handle_scope scope = nullptr;
napi_open_handle_scope(env, &scope);
localTask();

View File

@ -202,6 +202,7 @@ public:
private:
void GetMainEventHandler();
napi_env env_;
std::shared_ptr<int> envChecker_;
std::shared_ptr<OHOS::AppExecFwk::EventHandler> handler_;
};
} // namespace OHOS::Rosen