Skip to content

Commit 30c9181

Browse files
authored
test: fix unreliable assumption in js-native-api/test_cannot_run_js
Previously the test assumes that when the queued finalizer is run, it must be run at a point where env->can_call_into_js() is false (typically, during Environment shutdown), which is not certain. If GC kicks in early and the second pass finalizer is queued before the event loop runs the check callbacks, the finalizer would then be called in check callbacks (via native immediates), where the finalizer can still call into JS. Essentially, addons can't make assumptions about where the queued finalizer would be called. This patch updates the assertions in the test to account for that. PR-URL: #51898 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 40565e9 commit 30c9181

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

test/js-native-api/test_cannot_run_js/test_cannot_run_js.c

+24-8
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,33 @@
66
static void Finalize(napi_env env, void* data, void* hint) {
77
napi_value global, set_timeout;
88
napi_ref* ref = data;
9+
10+
NODE_API_NOGC_ASSERT_RETURN_VOID(
11+
napi_delete_reference(env, *ref) == napi_ok,
12+
"deleting reference in finalizer should succeed");
13+
NODE_API_NOGC_ASSERT_RETURN_VOID(
14+
napi_get_global(env, &global) == napi_ok,
15+
"getting global reference in finalizer should succeed");
16+
napi_status result =
17+
napi_get_named_property(env, global, "setTimeout", &set_timeout);
18+
19+
// The finalizer could be invoked either from check callbacks (as native
20+
// immediates) if the event loop is still running (where napi_ok is returned)
21+
// or during environment shutdown (where napi_cannot_run_js or
22+
// napi_pending_exception is returned). This is not deterministic from
23+
// the point of view of the addon.
24+
925
#ifdef NAPI_EXPERIMENTAL
10-
napi_status expected_status = napi_cannot_run_js;
26+
NODE_API_NOGC_ASSERT_RETURN_VOID(
27+
result == napi_cannot_run_js || result == napi_ok,
28+
"getting named property from global in finalizer should succeed "
29+
"or return napi_cannot_run_js");
1130
#else
12-
napi_status expected_status = napi_pending_exception;
31+
NODE_API_NOGC_ASSERT_RETURN_VOID(
32+
result == napi_pending_exception || result == napi_ok,
33+
"getting named property from global in finalizer should succeed "
34+
"or return napi_pending_exception");
1335
#endif // NAPI_EXPERIMENTAL
14-
15-
if (napi_delete_reference(env, *ref) != napi_ok) abort();
16-
if (napi_get_global(env, &global) != napi_ok) abort();
17-
if (napi_get_named_property(env, global, "setTimeout", &set_timeout) !=
18-
expected_status)
19-
abort();
2036
free(ref);
2137
}
2238

test/root.status

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[true]
2-
js-native-api/test_cannot_run_js/test: PASS,FLAKY
32

43
[$mode==debug]
54
async-hooks/test-callback-error: SLOW

0 commit comments

Comments
 (0)