Skip to content

Commit 8b5d738

Browse files
addaleaxtargos
authored andcommitted
n-api: do not require JS Context for napi_async_destroy()
Allow the function to be called during GC, which is a common use case. Fixes: #27218 PR-URL: #27255 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
1 parent 809cf59 commit 8b5d738

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

src/node_api.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,11 @@ napi_status napi_async_destroy(napi_env env,
632632
CHECK_ENV(env);
633633
CHECK_ARG(env, async_context);
634634

635-
v8::Isolate* isolate = env->isolate;
636635
node::async_context* node_async_context =
637636
reinterpret_cast<node::async_context*>(async_context);
638-
node::EmitAsyncDestroy(isolate, *node_async_context);
637+
node::EmitAsyncDestroy(
638+
reinterpret_cast<node_napi_env>(env)->node_env(),
639+
*node_async_context);
639640

640641
delete node_async_context;
641642

test/node-api/test_make_callback/binding.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
#define NAPI_EXPERIMENTAL // napi_add_finalizer
12
#include <node_api.h>
3+
#include <assert.h>
24
#include "../../js-native-api/common.h"
35

46
#define MAX_ARGUMENTS 10
@@ -44,13 +46,42 @@ static napi_value MakeCallback(napi_env env, napi_callback_info info) {
4446
return result;
4547
}
4648

49+
static void AsyncDestroyCb(napi_env env, void* data, void* hint) {
50+
napi_status status = napi_async_destroy(env, (napi_async_context)data);
51+
// We cannot use NAPI_ASSERT_RETURN_VOID because we need to have a JS stack
52+
// below in order to use exceptions.
53+
assert(status == napi_ok);
54+
}
55+
56+
static napi_value CreateAsyncResource(napi_env env, napi_callback_info info) {
57+
napi_value object;
58+
NAPI_CALL(env, napi_create_object(env, &object));
59+
60+
napi_value resource_name;
61+
NAPI_CALL(env, napi_create_string_utf8(
62+
env, "test_gcable", NAPI_AUTO_LENGTH, &resource_name));
63+
64+
napi_async_context context;
65+
NAPI_CALL(env, napi_async_init(env, object, resource_name, &context));
66+
67+
NAPI_CALL(env, napi_add_finalizer(
68+
env, object, (void*)context, AsyncDestroyCb, NULL, NULL));
69+
70+
return object;
71+
}
72+
4773
static
4874
napi_value Init(napi_env env, napi_value exports) {
4975
napi_value fn;
5076
NAPI_CALL(env, napi_create_function(
5177
// NOLINTNEXTLINE (readability/null_usage)
5278
env, NULL, NAPI_AUTO_LENGTH, MakeCallback, NULL, &fn));
5379
NAPI_CALL(env, napi_set_named_property(env, exports, "makeCallback", fn));
80+
NAPI_CALL(env, napi_create_function(
81+
// NOLINTNEXTLINE (readability/null_usage)
82+
env, NULL, NAPI_AUTO_LENGTH, CreateAsyncResource, NULL, &fn));
83+
NAPI_CALL(env, napi_set_named_property(
84+
env, exports, "createAsyncResource", fn));
5485
return exports;
5586
}
5687

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
// Flags: --gc-interval=100 --gc-global
3+
4+
const common = require('../../common');
5+
const assert = require('assert');
6+
const async_hooks = require('async_hooks');
7+
const { createAsyncResource } = require(`./build/${common.buildType}/binding`);
8+
9+
// Test for https://github.com/nodejs/node/issues/27218:
10+
// napi_async_destroy() can be called during a regular garbage collection run.
11+
12+
const hook_result = {
13+
id: null,
14+
init_called: false,
15+
destroy_called: false,
16+
};
17+
18+
const test_hook = async_hooks.createHook({
19+
init: (id, type) => {
20+
if (type === 'test_gcable') {
21+
hook_result.id = id;
22+
hook_result.init_called = true;
23+
}
24+
},
25+
destroy: (id) => {
26+
if (id === hook_result.id) hook_result.destroy_called = true;
27+
},
28+
});
29+
30+
test_hook.enable();
31+
createAsyncResource();
32+
33+
// Trigger GC. This does *not* use global.gc(), because what we want to verify
34+
// is that `napi_async_destroy()` can be called when there is no JS context
35+
// on the stack at the time of GC.
36+
// Currently, using --gc-interval=100 + 1M elements seems to work fine for this.
37+
const arr = new Array(1024 * 1024);
38+
for (let i = 0; i < arr.length; i++)
39+
arr[i] = {};
40+
41+
assert.strictEqual(hook_result.destroy_called, false);
42+
setImmediate(() => {
43+
assert.strictEqual(hook_result.destroy_called, true);
44+
});

0 commit comments

Comments
 (0)