From 4165dcacefc8f588756eea8b66f73ca7e401c36d Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 27 Feb 2024 15:56:00 +0100 Subject: [PATCH 1/3] 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. --- .../test_cannot_run_js/test_cannot_run_js.c | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c b/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c index c495f8780d0141..925a7117d060a1 100644 --- a/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c +++ b/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c @@ -6,17 +6,31 @@ static void Finalize(napi_env env, void* data, void* hint) { napi_value global, set_timeout; napi_ref* ref = data; + + NODE_API_NOGC_ASSERT_RETURN_VOID( + napi_delete_reference(env, *ref) == napi_ok, + "deleting reference in finalizer should succeed"); + NODE_API_NOGC_ASSERT_RETURN_VOID( + napi_get_global(env, &global) == napi_ok, + "getting global reference in finalizer should succeed"); + napi_status result = + napi_get_named_property(env, global, "setTimeout", &set_timeout); + + // The finalizer could be invoked either from check callbacks (as native + // immediates) if the event loop is still running (where napi_ok is returned) + // or during environment shutdown (where napi_cannot_run_js or + // napi_pending_exception is returned). This is not deterministic from + // the point of view of the addon. + NODE_API_NOGC_ASSERT_RETURN_VOID( #ifdef NAPI_EXPERIMENTAL - napi_status expected_status = napi_cannot_run_js; + result == napi_cannot_run_js || result == napi_ok, + "getting named property from global in finalizer should succeed " + "or return napi_cannot_run_js"); #else - napi_status expected_status = napi_pending_exception; + result == napi_pending_exception || result == napi_ok, + "getting named property from global in finalizer should succeed " + "or return napi_pending_exception"); #endif // NAPI_EXPERIMENTAL - - if (napi_delete_reference(env, *ref) != napi_ok) abort(); - if (napi_get_global(env, &global) != napi_ok) abort(); - if (napi_get_named_property(env, global, "setTimeout", &set_timeout) != - expected_status) - abort(); free(ref); } From e69bbcebcf385e75b42a4cdc078268c3b1583bac Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 27 Feb 2024 16:49:29 +0100 Subject: [PATCH 2/3] fixup! test: fix unreliable assumption in js-native-api/test_cannot_run_js --- test/root.status | 1 - 1 file changed, 1 deletion(-) diff --git a/test/root.status b/test/root.status index 12fef69a4b3c92..22f7260246f5e0 100644 --- a/test/root.status +++ b/test/root.status @@ -1,5 +1,4 @@ [true] -js-native-api/test_cannot_run_js/test: PASS,FLAKY [$mode==debug] async-hooks/test-callback-error: SLOW From d78f663515fa367f81198dde4625b7258afa52c8 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 29 Feb 2024 15:13:18 +0100 Subject: [PATCH 3/3] fixup! fixup! test: fix unreliable assumption in js-native-api/test_cannot_run_js --- test/js-native-api/test_cannot_run_js/test_cannot_run_js.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c b/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c index 925a7117d060a1..813e59918e7b3a 100644 --- a/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c +++ b/test/js-native-api/test_cannot_run_js/test_cannot_run_js.c @@ -21,12 +21,14 @@ static void Finalize(napi_env env, void* data, void* hint) { // or during environment shutdown (where napi_cannot_run_js or // napi_pending_exception is returned). This is not deterministic from // the point of view of the addon. - NODE_API_NOGC_ASSERT_RETURN_VOID( + #ifdef NAPI_EXPERIMENTAL + NODE_API_NOGC_ASSERT_RETURN_VOID( result == napi_cannot_run_js || result == napi_ok, "getting named property from global in finalizer should succeed " "or return napi_cannot_run_js"); #else + NODE_API_NOGC_ASSERT_RETURN_VOID( result == napi_pending_exception || result == napi_ok, "getting named property from global in finalizer should succeed " "or return napi_pending_exception");