node-api: enter env context for async callbacks - #65406
Open
codebytere wants to merge 1 commit into
Open
Conversation
`uvimpl::Work::AfterThreadPoolWork()` and the thread-safe function's `DispatchOne()` and `Finalize()` opened an `AsyncResource::CallbackScope` with only a `HandleScope`. `InternalCallbackScope` expects the resource's environment context to be entered and otherwise asserts that `Environment::GetCurrent(isolate)` is that environment, so with two environments on one isolate an addon's async work completion or thread-safe function call aborted the process whenever the other environment's context was current when the loop ran the callback. Enter the node-api env's context first, as `CallFinalizer()` and the zlib and WebCrypto thread pool callbacks already do. In `Finalize()` the scope covers only the finalizer call, since `MaybeDelete()` can free the env whose persistent handle `context()` returns. Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
Collaborator
|
Review requested:
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65406 +/- ##
==========================================
+ Coverage 90.10% 90.12% +0.01%
==========================================
Files 752 752
Lines 252209 252212 +3
Branches 47454 47449 -5
==========================================
+ Hits 227260 227302 +42
+ Misses 16264 16209 -55
- Partials 8685 8701 +16
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AsyncResource::CallbackScopehas to be opened with the resource's environment context entered;InternalCallbackScopeotherwise asserts thatEnvironment::GetCurrent(isolate)is that environment.uvimpl::Work::AfterThreadPoolWork()and the thread-safe function'sDispatchOne()andFinalize()opened it with only aHandleScope, whileCallFinalizer()beside them enterscontext()first. With one Environment per isolate that never shows, since the loop runs with that context entered. With two Environments on one isolate sharing a loop, an addon'snapi_async_workcompletion or thread-safe function call aborts the process whenever the other Environment's context is the current one when the loop gets to it:This enters the node-api env's context at those three sites, the same way the zlib and WebCrypto thread pool callbacks do. In
Finalize()the scope covers only the finalizer call rather than the whole function:context()returns a handle onto the env's persistent, andMaybeDelete()at the end can drop the last reference and free the env, so a function-wide scope would exit through a dead handle (test_threadsafe_function_shutdowncovers that).The node-api env's own context is used rather than
node_env()->context(); they are the same today since addons only load in the principal realm.Tests: new cctest
NodeApiTest.AsyncCallbacksEnterOwnContextcreates two Environments on one isolate, queues async work and a thread-safe function call from an addon in the first with the second's context entered, runs the loop, and checks that the complete callback,call_jsand the thread-safe function's finalizer each ran in the addon's context; it hits the assertion above onmainand passes here. cctest, node-api, js-native-api and async-hooks suites pass.Disclosure: the code, test and this description were written by Claude Code, directed and reviewed by @codebytere.