OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[OV JS] Fix uncaught exception in JS API (#37592)
Alicja Miloszewska597262ef181b
diff --git a/src/bindings/js/node/include/tensor_impl.hpp b/src/bindings/js/node/include/tensor_impl.hppindex 5aa2691a433..3844e645208 100644--- a/src/bindings/js/node/include/tensor_impl.hpp+++ b/src/bindings/js/node/include/tensor_impl.hpp@@ -29,10 +29,10 @@ public:struct CleanupContext {explicit CleanupContext(Napi::Reference<Napi::TypedArray>* typed_array_ref) : ref(typed_array_ref) {}- void release_owner();- void release_tsfn();- void remove_cleanup_hook();- static void cleanup_hook(napi_async_cleanup_hook_handle handle, void* data);+ void release_owner() noexcept;+ void release_tsfn() noexcept;+ void remove_cleanup_hook() noexcept;+ static void cleanup_hook(napi_async_cleanup_hook_handle handle, void* data) noexcept;std::atomic<uint32_t> owners{2};std::atomic<bool> tsfn_released{false};diff --git a/src/bindings/js/node/src/tensor_impl.cpp b/src/bindings/js/node/src/tensor_impl.cppindex df8ded01ee9..3866a35a6d5 100644--- a/src/bindings/js/node/src/tensor_impl.cpp+++ b/src/bindings/js/node/src/tensor_impl.cpp@@ -4,6 +4,8 @@#include "node/include/tensor_impl.hpp"+#include <cassert>+#include "openvino/core/except.hpp"#include "openvino/runtime/tensor.hpp"@@ -11,31 +13,29 @@ namespace ov {namespace js {// Drops one native owner of the shared cleanup state and deletes it on the last release.-void TensorImpl::CleanupContext::release_owner() {+void TensorImpl::CleanupContext::release_owner() noexcept {if (owners.fetch_sub(1) == 1) {delete this;}}// Releases the TSFN exactly once because both destructor and env cleanup may race here.-void TensorImpl::CleanupContext::release_tsfn() {+void TensorImpl::CleanupContext::release_tsfn() noexcept {if (!tsfn_released.exchange(true)) {- const auto status = tsfn.Release();- OPENVINO_ASSERT(status == napi_ok || status == napi_closing,- "TensorImpl: failed to release ThreadSafeFunction.");+ [[maybe_unused]] const auto status = tsfn.Release();+ assert((status == napi_ok) && "TensorImpl: TSFN release failed");}}// Unregisters the async cleanup hook exactly once to avoid double removal during shutdown.-void TensorImpl::CleanupContext::remove_cleanup_hook() {+void TensorImpl::CleanupContext::remove_cleanup_hook() noexcept {if (cleanup_handle != nullptr && !cleanup_handle_removed.exchange(true)) {- const auto status = napi_remove_async_cleanup_hook(cleanup_handle);- OPENVINO_ASSERT(status == napi_ok, "TensorImpl: failed to remove async cleanup hook.");+ napi_remove_async_cleanup_hook(cleanup_handle);}}// Handles Node environment teardown by forcing TSFN shutdown through the cleanup hook path.-void TensorImpl::CleanupContext::cleanup_hook(napi_async_cleanup_hook_handle handle, void* data) {+void TensorImpl::CleanupContext::cleanup_hook(napi_async_cleanup_hook_handle handle, void* data) noexcept {auto* cleanup_ctx = static_cast<TensorImpl::CleanupContext*>(data);cleanup_ctx->cleanup_handle = handle;cleanup_ctx->release_tsfn();