GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — node-api: enter env context for async callbacks by codebytere · Pull Request #65406 · nodejs/node

Conversation

@codebytere

@codebytere codebytere commented Aug 19, 2026

Copy link
Copy Markdown
Member

AsyncResource::CallbackScope has to be opened with the resource's environment context entered; InternalCallbackScope otherwise asserts that Environment::GetCurrent(isolate) is that environment. uvimpl::Work::AfterThreadPoolWork() and the thread-safe function's DispatchOne() and Finalize() opened it with only a HandleScope, while CallFinalizer() beside them enters context() 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's napi_async_work completion or thread-safe function call aborts the process whenever the other Environment's context is the current one when the loop gets to it:

#  Node.js[216]: node::InternalCallbackScope::InternalCallbackScope(...) at ../../src/api/callback.cc:108
#  Assertion failed: (Environment::GetCurrent(isolate)) == (env)

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, and MaybeDelete() 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_shutdown covers 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.AsyncCallbacksEnterOwnContext creates 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_js and the thread-safe function's finalizer each ran in the addon's context; it hits the assertion above on main and 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@codebytere
node-api: enter env context for async callbacks
bea5dfb
`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 <[email protected]>
@nodejs-github-bot

nodejs-github-bot commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/node-api
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. node-api Issues and PRs related to Node-API. labels Aug 19, 2026
@github-project-automation github-project-automation Bot added this to Node-API Team Project Aug 19, 2026
@github-project-automation github-project-automation Bot moved this to Need Triage in Node-API Team Project Aug 19, 2026
@codebytere
codebytere requested review from gengjiawen and legendecas August 19, 2026 21:19
@codebytere codebytere added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 19, 2026
@codecov

codecov Bot commented Aug 19, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.12%. Comparing base (03fb384) to head (bea5dfb).
⚠️ Report is 202 commits behind head on main.

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     
Files with missing lines Coverage Δ
src/node_api.cc 75.33% <100.00%> (+0.09%) ⬆️

... and 39 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@codebytere
codebytere requested a review from aduh95 August 22, 2026 14:38
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@sxa sxa mentioned this pull request Aug 28, 2026
Auto Start CI delayed running 2 - 3 hours instead of every 5 minutes nodejs/build#4443
Closed
legendecas
legendecas approved these changes Aug 28, 2026
View reviewed changes
@legendecas legendecas moved this from Need Triage to In Progress in Node-API Team Project Aug 28, 2026
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Aug 28, 2026
@nodejs-github-bot

This comment was marked as outdated.

Sign in to view
@nodejs-github-bot

nodejs-github-bot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/76692/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@codebytere codebytere added commit-queue PRs queued for automated landing through the Commit Queue. and removed needs-ci PRs that need a full CI run. labels Aug 30, 2026
@nodejs-github-bot
nodejs-github-bot merged commit df1aa93 into nodejs:main Aug 30, 2026
86 of 87 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Landed in df1aa93

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-project-automation github-project-automation Bot moved this from In Progress to Done in Node-API Team Project Aug 30, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Aug 30, 2026
aduh95 pushed a commit that referenced this pull request Sep 3, 2026
@codebytere @aduh95
node-api: enter env context for async callbacks
00add61
`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 <[email protected]>
PR-URL: #65406
Reviewed-By: Chengzhong Wu <[email protected]>
@github-actions github-actions Bot mentioned this pull request Sep 7, 2026
Version 26.9.0 (Current) #65881
Merged
aduh95 pushed a commit that referenced this pull request Sep 15, 2026
@codebytere @aduh95
node-api: enter env context for async callbacks
875c7ec
`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 <[email protected]>
PR-URL: #65406
Reviewed-By: Chengzhong Wu <[email protected]>
aduh95 pushed a commit that referenced this pull request Sep 19, 2026
@codebytere @aduh95
node-api: enter env context for async callbacks
2a6a63a
`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 <[email protected]>
PR-URL: #65406
Reviewed-By: Chengzhong Wu <[email protected]>
aduh95 pushed a commit that referenced this pull request Oct 23, 2026
@codebytere @aduh95
node-api: enter env context for async callbacks
b40723c
`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 <[email protected]>
PR-URL: #65406
Reviewed-By: Chengzhong Wu <[email protected]>
aduh95 pushed a commit that referenced this pull request Oct 28, 2026
@codebytere @aduh95
node-api: enter env context for async callbacks
f748520
`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 <[email protected]>
PR-URL: #65406
Reviewed-By: Chengzhong Wu <[email protected]>
aduh95 pushed a commit that referenced this pull request Nov 11, 2026
@codebytere @aduh95
node-api: enter env context for async callbacks
6d96c55
`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 <[email protected]>
PR-URL: #65406
Reviewed-By: Chengzhong Wu <[email protected]>
@github-actions github-actions Bot mentioned this pull request Nov 20, 2026
2026-11-30, Version 24.23.0 'Krypton' (LTS) #67663
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@legendecas legendecas legendecas approved these changes
@gengjiawen gengjiawen Awaiting requested review from gengjiawen
@aduh95 aduh95 Awaiting requested review from aduh95

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. node-api Issues and PRs related to Node-API.

Projects

Node-API Team Project
Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@codebytere @nodejs-github-bot @legendecas
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.