Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix unreliable assumption in js-native-api/test_cannot_run_js #51898

Merged
merged 3 commits into from
Mar 1, 2024

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Feb 27, 2024

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.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. test Issues and PRs related to the tests. labels Feb 27, 2024
Copy link
Member

@targos targos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as a hotfix, but the test will basically become useless once V8 12.2 lands, as it will always return napi_ok then.

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.
@joyeecheung
Copy link
Member Author

joyeecheung commented Feb 27, 2024

but the test will basically become useless once V8 12.2 lands, as it will always return napi_ok then.

That's not certain, it really depends on what the heap looks like when the addon is loaded. Any subtle change to the internals can delay the GC to happen after the check callbacks are called in the event loop (or not happen at all), and then it'll be napi_cannot_run_js again (and become a flaky nightmare). The fix just makes sure that it works for all cases (no matter GC kicks in before or after check callbacks, or if it doesn't happen at all)

@richardlau
Copy link
Member

If this fixes the test, perhaps remove the flaky designation in

js-native-api/test_cannot_run_js/test: PASS,FLAKY
?

@joyeecheung
Copy link
Member Author

If this fixes the test, perhaps remove the flaky designation in

Updated the status file - was the designation even working in the first place? It still got caught in the CI it seems, and locally the test runner still reports it. (I think it should've been marked as PASS,CRASH instead?)

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 27, 2024
@richardlau
Copy link
Member

If this fixes the test, perhaps remove the flaky designation in

Updated the status file - was the designation even working in the first place? It still got caught in the CI it seems, and locally the test runner still reports it. (I think it should've been marked as PASS,CRASH instead?)

Yes, it was working -- e.g. https://ci.nodejs.org/job/node-test-commit-ibmi/nodes=ibmi73-ppc64/1466/ (from #48180 (comment)) is yellow due to this test crashing and not red.

@richardlau richardlau linked an issue Feb 27, 2024 that may be closed by this pull request
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 27, 2024
@nodejs-github-bot
Copy link
Collaborator

@richardlau
Copy link
Member

It looks like the addon is failing to compile on Windows:
e.g. https://ci.nodejs.org/job/node-test-binary-windows-native-suites/21625/nodes=win10-vs2019-COMPILED_BY-vs2022/console

17:10:21 Failed to build addon in c:\workspace\node-test-binary-windows-native-suites\node\test\js-native-api\test_cannot_run_js:
17:10:21 Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.

17:10:21   test_cannot_run_js.c

17:10:21 c:\workspace\node-test-binary-windows-native-suites\node\test\js-native-api\test_cannot_run_js\test_cannot_run_js.c(28,1): error C2121: '#': invalid character: possibly the result of a macro expansion [c:\workspace\node-test-binary-windows-native-suites\node\test\js-native-api\test_cannot_run_js\build\test_cannot_run_js.vcxproj]

@StefanStojanovic
Copy link
Contributor

It looks like the addon is failing to compile on Windows

I tried this locally and can confirm the behavior. What fixed the compilation for me was this. @joyeecheung if you think it's good, feel free to apply it.

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 925a7117d06..19cc7ae9058 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,13 @@ 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");

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 29, 2024
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 29, 2024
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@richardlau richardlau added commit-queue Add this label to land a pull request using GitHub Actions. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Mar 1, 2024
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 1, 2024
@nodejs-github-bot nodejs-github-bot merged commit 30c9181 into nodejs:main Mar 1, 2024
57 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 30c9181

marco-ippolito pushed a commit that referenced this pull request Mar 1, 2024
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>
@marco-ippolito marco-ippolito mentioned this pull request Mar 1, 2024
richardlau pushed a commit that referenced this pull request Mar 25, 2024
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>
richardlau pushed a commit that referenced this pull request Mar 25, 2024
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>
@richardlau richardlau mentioned this pull request Mar 25, 2024
rdw-msft pushed a commit to rdw-msft/node that referenced this pull request Mar 26, 2024
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: nodejs#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>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. node-api Issues and PRs related to the Node-API. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Flaky js-native-api/test_cannot_run_js/test
7 participants