From 8fb0646e27a8d1f5c5edbca2d0802d20f40ee44a Mon Sep 17 00:00:00 2001 From: milkpotatoes Date: Mon, 7 Apr 2025 17:12:34 +0800 Subject: [PATCH] Add tracker for mutil-queue async_work Signed-off-by: milkpotatoes --- native_engine/native_async_work.cpp | 15 ++++++++++++++- native_engine/native_async_work.h | 2 ++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/native_engine/native_async_work.cpp b/native_engine/native_async_work.cpp index 2b7f09d57..2026337b8 100644 --- a/native_engine/native_async_work.cpp +++ b/native_engine/native_async_work.cpp @@ -90,12 +90,19 @@ NativeAsyncWork::NativeAsyncWork(NativeEngine* engine, #endif } -NativeAsyncWork::~NativeAsyncWork() = default; +NativeAsyncWork::~NativeAsyncWork() { + if (queuedSize_ > 0) { + HILOG_FATAL("Do not delete napi_async_work until complete callback is executed."); + } +}; bool NativeAsyncWork::Queue(NativeEngine* engine) { VALID_ENGINE_CHECK(engine, engine_, engineId_); + if (queuedSize_ > 0) { + HILOG_FATAL("Do not queue a napi_async_work more than once."); + } uv_loop_t* loop = engine_->GetUVLoop(); if (loop == nullptr) { HILOG_ERROR("Get loop failed"); @@ -117,6 +124,7 @@ bool NativeAsyncWork::Queue(NativeEngine* engine) engine_->DecreaseWaitingRequestCounter(); return false; } + queuedSize_++; HILOG_DEBUG("uv_queue_work succeed"); return true; } @@ -125,6 +133,9 @@ bool NativeAsyncWork::QueueWithQos(NativeEngine* engine, napi_qos_t qos) { VALID_ENGINE_CHECK(engine, engine_, engineId_); + if (queuedSize_ > 0) { + HILOG_FATAL("Do not queue a napi_async_work more than once."); + } uv_loop_t* loop = engine_->GetUVLoop(); if (loop == nullptr) { HILOG_ERROR("Get loop failed"); @@ -146,6 +157,7 @@ bool NativeAsyncWork::QueueWithQos(NativeEngine* engine, napi_qos_t qos) engine_->DecreaseWaitingRequestCounter(); return false; } + queuedSize_++; HILOG_DEBUG("uv_queue_work_with_qos succeed"); return true; } @@ -198,6 +210,7 @@ void NativeAsyncWork::AsyncAfterWorkCallback(uv_work_t* req, int status) } auto that = reinterpret_cast(req->data); + that->queuedSize_--; auto engine = that->engine_; engine->DecreaseWaitingRequestCounter(); auto vm = engine->GetEcmaVm(); diff --git a/native_engine/native_async_work.h b/native_engine/native_async_work.h index 94eeb666a..d8c3aae1f 100644 --- a/native_engine/native_async_work.h +++ b/native_engine/native_async_work.h @@ -83,6 +83,8 @@ private: std::mutex workAsyncMutex_; std::queue asyncWorkRecvData_; std::string traceDescription_; + // only be modify in queue and complete, would in the same thread. + size_t queuedSize_ {0}; #ifdef ENABLE_CONTAINER_SCOPE int32_t containerScopeId_; #endif -- Gitee