From cac9b2ff8dcc3787350b48c3ad52494d636bde6c Mon Sep 17 00:00:00 2001 From: wangzhaoyong Date: Wed, 31 Aug 2022 10:06:09 +0800 Subject: [PATCH] fix for uv_close(uv_async_t) when ~Worker issue:https://gitee.com/openharmony/commonlibrary_ets_utils/issues/I5OXI2 Signed-off-by: wangzhaoyong --- worker/worker.cpp | 52 +++++++++++++++++++++++++++++++++++------------ worker/worker.h | 4 ++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/worker/worker.cpp b/worker/worker.cpp index 7265104..1446e72 100644 --- a/worker/worker.cpp +++ b/worker/worker.cpp @@ -34,8 +34,12 @@ void Worker::StartExecuteInThread(napi_env env, const char* script) Helper::CloseHelp::DeletePointer(script, true); return; } - uv_async_init(loop, &hostOnMessageSignal_, reinterpret_cast(Worker::HostOnMessage)); - uv_async_init(loop, &hostOnErrorSignal_, reinterpret_cast(Worker::HostOnError)); + hostOnMessageSignal_ = new uv_async_t; + uv_async_init(loop, hostOnMessageSignal_, reinterpret_cast(Worker::HostOnMessage)); + hostOnMessageSignal_->data = this; + hostOnErrorSignal_ = new uv_async_t; + uv_async_init(loop, hostOnErrorSignal_, reinterpret_cast(Worker::HostOnError)); + hostOnErrorSignal_->data = this; // 2. copy the script script_ = std::string(script); @@ -163,7 +167,7 @@ void Worker::PublishWorkerOverSignal() // post NULL tell host worker is not running if (!HostIsStop()) { hostMessageQueue_.EnQueue(NULL); - uv_async_send(&hostOnMessageSignal_); + uv_async_send(hostOnMessageSignal_); } } @@ -222,7 +226,7 @@ void Worker::ExecuteInThread(const void* data) void Worker::HostOnMessage(const uv_async_t* req) { - Worker* worker = Helper::DereferenceHelp::DereferenceOf(&Worker::hostOnMessageSignal_, req); + Worker* worker = static_cast(req->data); if (worker == nullptr) { HILOG_ERROR("worker::worker is null"); return; @@ -259,7 +263,7 @@ void Worker::HostOnErrorInner() void Worker::HostOnError(const uv_async_t* req) { - Worker* worker = Helper::DereferenceHelp::DereferenceOf(&Worker::hostOnErrorSignal_, req); + Worker* worker = static_cast(req->data); if (worker == nullptr) { HILOG_ERROR("worker::worker is null"); return; @@ -350,8 +354,18 @@ void Worker::HostOnMessageInner() // receive close signal. if (data == nullptr) { HILOG_INFO("worker:: worker received close signal"); - uv_close(reinterpret_cast(&hostOnMessageSignal_), nullptr); - uv_close(reinterpret_cast(&hostOnErrorSignal_), nullptr); + uv_close(reinterpret_cast(hostOnMessageSignal_), [](uv_handle_t* handle) { + if (handle != nullptr) { + delete reinterpret_cast(handle); + handle = nullptr; + } + }); + uv_close(reinterpret_cast(hostOnErrorSignal_), [](uv_handle_t* handle) { + if (handle != nullptr) { + delete reinterpret_cast(handle); + handle = nullptr; + } + }); CloseHostCallback(); return; } @@ -417,7 +431,7 @@ void Worker::HandleException() std::lock_guard lock(liveStatusLock_); if (!HostIsStop()) { errorQueue_.EnQueue(data); - uv_async_send(&hostOnErrorSignal_); + uv_async_send(hostOnErrorSignal_); } } } else { @@ -563,7 +577,7 @@ void Worker::PostMessageToHostInner(MessageDataType data) std::lock_guard lock(liveStatusLock_); if (hostEnv_ != nullptr && !HostIsStop()) { hostMessageQueue_.EnQueue(data); - uv_async_send(&hostOnMessageSignal_); + uv_async_send(hostOnMessageSignal_); } else { HILOG_ERROR("worker:: worker host engine is nullptr."); } @@ -741,11 +755,23 @@ napi_value Worker::WorkerConstructor(napi_env env, napi_callback_info cbinfo) { std::lock_guard lock(worker->liveStatusLock_); if (worker->UpdateHostState(INACTIVE)) { - if (!uv_is_closing(reinterpret_cast(&worker->hostOnMessageSignal_))) { - uv_close(reinterpret_cast(&worker->hostOnMessageSignal_), nullptr); + if (worker->hostOnMessageSignal_ != nullptr && + !uv_is_closing(reinterpret_cast(worker->hostOnMessageSignal_))) { + uv_close(reinterpret_cast(worker->hostOnMessageSignal_), [](uv_handle_t* handle) { + if (handle != nullptr) { + delete reinterpret_cast(handle); + handle = nullptr; + } + }); } - if (!uv_is_closing(reinterpret_cast(&worker->hostOnErrorSignal_))) { - uv_close(reinterpret_cast(&worker->hostOnErrorSignal_), nullptr); + if (worker->hostOnErrorSignal_ != nullptr && + !uv_is_closing(reinterpret_cast(worker->hostOnErrorSignal_))) { + uv_close(reinterpret_cast(worker->hostOnErrorSignal_), [](uv_handle_t* handle) { + if (handle != nullptr) { + delete reinterpret_cast(handle); + handle = nullptr; + } + }); } worker->ReleaseHostThreadContent(); } diff --git a/worker/worker.h b/worker/worker.h index df28846..10c47d0 100644 --- a/worker/worker.h +++ b/worker/worker.h @@ -431,8 +431,8 @@ private: MessageQueue errorQueue_ {}; uv_async_t workerOnMessageSignal_ {}; - uv_async_t hostOnMessageSignal_ {}; - uv_async_t hostOnErrorSignal_ {}; + uv_async_t* hostOnMessageSignal_ = nullptr; + uv_async_t* hostOnErrorSignal_ = nullptr; #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) uv_async_t debuggerOnPostTaskSignal_ {}; std::function debuggerTask_; -- Gitee