Skip to content

Commit 525308f

Browse files
committed
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 first pass callback 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.
1 parent 4c46439 commit 525308f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

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

+24-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,30 @@ static void Finalize(napi_env env, void* data, void* hint) {
1212
napi_status expected_status = napi_pending_exception;
1313
#endif // NAPI_EXPERIMENTAL
1414

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();
15+
NODE_API_NOGC_ASSERT_RETURN_VOID(
16+
napi_delete_reference(env, *ref) == napi_ok,
17+
"deleting reference in finalizer should succeed");
18+
NODE_API_NOGC_ASSERT_RETURN_VOID(
19+
napi_get_global(env, &global) == napi_ok,
20+
"getting global reference in finalizer should succeed");
21+
napi_status result =
22+
napi_get_named_property(env, global, "setTimeout", &set_timeout);
23+
24+
// The finalizer could be invoked either from check callbacks (as native
25+
// immediates) if the event loop is still running (where napi_ok is returned)
26+
// or during environment shutdown (where napi_cannot_run_js or
27+
// napi_pending_exception is returned). This is not deterministic from
28+
// the point of view of the addon.
29+
NODE_API_NOGC_ASSERT_RETURN_VOID(
30+
#ifdef NAPI_EXPERIMENTAL
31+
result == napi_cannot_run_js || result == napi_ok,
32+
"getting named property from global in finalizer should succeed "
33+
"or return napi_cannot_run_js");
34+
#else
35+
result == napi_pending_exception || result == napi_ok,
36+
"getting named property from global in finalizer should succeed "
37+
"or return napi_pending_exception");
38+
#endif // NAPI_EXPERIMENTAL
2039
free(ref);
2140
}
2241

0 commit comments

Comments
 (0)