From 577356b707e4d515c99213271bf562bbd60eb39e Mon Sep 17 00:00:00 2001 From: wangzhaoyong Date: Wed, 13 Jul 2022 17:10:53 +0800 Subject: [PATCH] Bugfix for worker XTS issue: https://gitee.com/openharmony/js_worker_module/issues/I5F4ER Signed-off-by: wangzhaoyong Change-Id: I6d1fa45ff943c47cf4335ecaf5918b74207928b9 --- helper/object_helper.h | 2 ++ plugin/timer.cpp | 31 +++++++++++++++++++------------ plugin/timer.h | 1 + worker/worker.h | 1 + 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/helper/object_helper.h b/helper/object_helper.h index f754863..35e3da1 100644 --- a/helper/object_helper.h +++ b/helper/object_helper.h @@ -43,6 +43,7 @@ public: delete[] value; } else { delete value; + value = nullptr; } } }; @@ -60,6 +61,7 @@ public: delete[] data_; } else { delete data_; + data_ = nullptr; } } diff --git a/plugin/timer.cpp b/plugin/timer.cpp index 4cd59ad..08e4136 100644 --- a/plugin/timer.cpp +++ b/plugin/timer.cpp @@ -52,11 +52,13 @@ bool Timer::RegisterTime(napi_env env) napi_value Timer::SetTimeout(napi_env env, napi_callback_info cbinfo) { + HILOG_INFO("worker:: timer SetTimeout"); return Timer::SetTimeoutInner(env, cbinfo, false); } napi_value Timer::SetInterval(napi_env env, napi_callback_info cbinfo) { + HILOG_INFO("worker:: timer SetInterval"); return Timer::SetTimeoutInner(env, cbinfo, true); } @@ -79,13 +81,16 @@ napi_value Timer::ClearTimer(napi_env env, napi_callback_info cbinfo) HILOG_WARN("handler should be number"); return nullptr; } - auto iter = timerTable.find(tId); - if (iter == timerTable.end()) { - HILOG_INFO("handler not in table"); - return nullptr; + { + std::lock_guard lock(timeLock); + auto iter = timerTable.find(tId); + if (iter == timerTable.end()) { + HILOG_INFO("handler not in table"); + return nullptr; + } + TimerCallbackInfo* callbackInfo = iter->second; + timerTable.erase(tId); } - TimerCallbackInfo* callbackInfo = iter->second; - timerTable.erase(tId); Helper::CloseHelp::DeletePointer(callbackInfo, false); return Helper::NapiHelper::GetUndefinedValue(env); } @@ -111,6 +116,7 @@ void Timer::TimerCallback(uv_timer_t* handle) return; } if (!callbackInfo->repeat_) { + std::lock_guard lock(timeLock); timerTable.erase(callbackInfo->tId_); Helper::CloseHelp::DeletePointer(callbackInfo, false); } else { @@ -158,13 +164,13 @@ napi_value Timer::SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool { std::lock_guard lock(timeLock); tId = timeCallbackId++; + // 4. generate time callback info + napi_ref callbackRef = Helper::NapiHelper::CreateReference(env, argv[0], 1); + TimerCallbackInfo* callbackInfo = + new TimerCallbackInfo(env, tId, timeout, callbackRef, repeat, callbackArgc, callbackArgv); + // 5. push callback info into timerTable + timerTable[tId] = callbackInfo; } - // 4. generate time callback info - napi_ref callbackRef = Helper::NapiHelper::CreateReference(env, argv[0], 1); - TimerCallbackInfo* callbackInfo = - new TimerCallbackInfo(env, tId, timeout, callbackRef, repeat, callbackArgc, callbackArgv); - // 5. push callback info into timerTable - timerTable[tId] = callbackInfo; // 6. start timer uv_timer_start(&callbackInfo->timeReq_, TimerCallback, timeout, timeout > 0 ? timeout : 1); @@ -173,6 +179,7 @@ napi_value Timer::SetTimeoutInner(napi_env env, napi_callback_info cbinfo, bool void Timer::ClearEnvironmentTimer(napi_env env) { + std::lock_guard lock(timeLock); auto iter = timerTable.begin(); while (iter != timerTable.end()) { TimerCallbackInfo* callbackInfo = iter->second; diff --git a/plugin/timer.h b/plugin/timer.h index 08a2cb5..31a6f6c 100644 --- a/plugin/timer.h +++ b/plugin/timer.h @@ -17,6 +17,7 @@ #define JS_WORKER_MODULE_PLUGIN_TIMER_H_ #include +#include #include #include "base/compileruntime/js_worker_module/helper/napi_helper.h" diff --git a/worker/worker.h b/worker/worker.h index df28846..41715ce 100644 --- a/worker/worker.h +++ b/worker/worker.h @@ -369,6 +369,7 @@ public: uv_run(loop, UV_RUN_DEFAULT); } else { HILOG_ERROR("worker:: Worker loop is nullptr when start worker loop"); + return; } } -- Gitee