Skip to content

Commit a34cb17

Browse files
gabrielschulhofjcbhmr
authored andcommitted
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: nodejs#51801 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent f80c9f4 commit a34cb17

File tree

3 files changed

+3
-17
lines changed

3 files changed

+3
-17
lines changed

src/node_api.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -1319,12 +1319,10 @@ napi_create_threadsafe_function(napi_env env,
13191319
size_t max_queue_size,
13201320
size_t initial_thread_count,
13211321
void* thread_finalize_data,
1322-
node_api_nogc_finalize nogc_thread_finalize_cb,
1322+
napi_finalize thread_finalize_cb,
13231323
void* context,
13241324
napi_threadsafe_function_call_js call_js_cb,
13251325
napi_threadsafe_function* result) {
1326-
napi_finalize thread_finalize_cb =
1327-
reinterpret_cast<napi_finalize>(nogc_thread_finalize_cb);
13281326
CHECK_ENV_NOT_IN_GC(env);
13291327
CHECK_ARG(env, async_resource_name);
13301328
RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg);

src/node_api.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ napi_create_threadsafe_function(napi_env env,
209209
size_t max_queue_size,
210210
size_t initial_thread_count,
211211
void* thread_finalize_data,
212-
node_api_nogc_finalize thread_finalize_cb,
212+
napi_finalize thread_finalize_cb,
213213
void* context,
214214
napi_threadsafe_function_call_js call_js_cb,
215215
napi_threadsafe_function* result);

test/node-api/test_threadsafe_function/test_uncaught_exception.c

+1-13
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,6 @@ static void ThreadSafeFunctionFinalize(napi_env env,
1616
NODE_API_CALL_RETURN_VOID(env, napi_delete_reference(env, js_func_ref));
1717
}
1818

19-
static void ThreadSafeFunctionNogcFinalize(node_api_nogc_env env,
20-
void* data,
21-
void* hint) {
22-
#ifdef NAPI_EXPERIMENTAL
23-
NODE_API_NOGC_CALL_RETURN_VOID(
24-
env,
25-
node_api_post_finalizer(env, ThreadSafeFunctionFinalize, data, hint));
26-
#else
27-
ThreadSafeFunctionFinalize(env, data, hint);
28-
#endif
29-
}
30-
3119
// Testing calling into JavaScript
3220
static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
3321
size_t argc = 4;
@@ -46,7 +34,7 @@ static napi_value CallIntoModule(napi_env env, napi_callback_info info) {
4634
0,
4735
1,
4836
finalize_func,
49-
ThreadSafeFunctionNogcFinalize,
37+
ThreadSafeFunctionFinalize,
5038
NULL,
5139
NULL,
5240
&tsfn));

0 commit comments

Comments
 (0)