From f9d95674be94cc67611a65254f24c17125ca92b2 Mon Sep 17 00:00:00 2001 From: Gabriel Schulhof Date: Fri, 8 Mar 2024 21:58:42 -0800 Subject: [PATCH] node-api: make tsfn accept napi_finalize once more The thread-safe function's finalizer is not called in conjunction with the garbage collection of a JS value. In fact, it keeps a strong reference to the JS function it is expected to call. Thus, it is safe to make calls that affect GC state from its body. PR-URL: https://github.com/nodejs/node/pull/51801 Reviewed-By: Michael Dawson Reviewed-By: Chengzhong Wu --- src/node_api.cc | 4 +--- src/node_api.h | 2 +- .../test_uncaught_exception.c | 14 +------------- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/node_api.cc b/src/node_api.cc index 85621b3218b03a..b79b4dd1d01db3 100644 --- a/src/node_api.cc +++ b/src/node_api.cc @@ -1318,12 +1318,10 @@ napi_create_threadsafe_function(napi_env env, size_t max_queue_size, size_t initial_thread_count, void* thread_finalize_data, - node_api_nogc_finalize nogc_thread_finalize_cb, + napi_finalize thread_finalize_cb, void* context, napi_threadsafe_function_call_js call_js_cb, napi_threadsafe_function* result) { - napi_finalize thread_finalize_cb = - reinterpret_cast(nogc_thread_finalize_cb); CHECK_ENV_NOT_IN_GC(env); CHECK_ARG(env, async_resource_name); RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg); diff --git a/src/node_api.h b/src/node_api.h index 0074124dc6aa4f..e94ee486392840 100644 --- a/src/node_api.h +++ b/src/node_api.h @@ -209,7 +209,7 @@ napi_create_threadsafe_function(napi_env env, size_t max_queue_size, size_t initial_thread_count, void* thread_finalize_data, - node_api_nogc_finalize thread_finalize_cb, + napi_finalize thread_finalize_cb, void* context, napi_threadsafe_function_call_js call_js_cb, napi_threadsafe_function* result); diff --git a/test/node-api/test_threadsafe_function/test_uncaught_exception.c b/test/node-api/test_threadsafe_function/test_uncaught_exception.c index c5eb4271dd490e..f8499d4fe4d680 100644 --- a/test/node-api/test_threadsafe_function/test_uncaught_exception.c +++ b/test/node-api/test_threadsafe_function/test_uncaught_exception.c @@ -16,18 +16,6 @@ static void ThreadSafeFunctionFinalize(napi_env env, NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_func_ref)); } -static void ThreadSafeFunctionNogcFinalize(node_api_nogc_env env, - void* data, - void* hint) { -#ifdef NAPI_EXPERIMENTAL - NODE_API_NOGC_CALL_RETURN_VOID( - env, - node_api_post_finalizer(env, ThreadSafeFunctionFinalize, data, hint)); -#else - ThreadSafeFunctionFinalize(env, data, hint); -#endif -} - // Testing calling into JavaScript static napi_value CallIntoModule(napi_env env, napi_callback_info info) { size_t argc = 4; @@ -46,7 +34,7 @@ static napi_value CallIntoModule(napi_env env, napi_callback_info info) { 0, 1, finalize_func, - ThreadSafeFunctionNogcFinalize, + ThreadSafeFunctionFinalize, NULL, NULL, &tsfn));