Skip to content

src: skip the async id stack when nobody sees it - #66511

Merged
nodejs-github-bot merged 2 commits into
nodejs:mainfrom
nigrosimone:lazy-async-ids
Nov 23, 2026
Merged

nodejs-github-bot merged 2 commits into
nodejs:mainfrom
nigrosimone:lazy-async-ids

Conversation

@nigrosimone

@nigrosimone nigrosimone commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

With no async hook and no executionAsyncResource() user, every InternalCallbackScope pushes and pops the async ids on the stack, only for executionAsyncId() and triggerAsyncId() to read the top. This swaps the two ids in place instead: they are all that those two and process.nextTick() read, so the readers pay nothing.

executionAsyncResource() is the only reader of the stack. The scopes that skip it form a chain in AsyncHooks, counted in a new field kLazyScopes, and JS asks C++ for the innermost one only while that count is not zero. The checks for a corrupted stack stay: test/async-hooks test-improper-order and test-emit-*-on-destroyed fail without them, because the main script runs in a scope that starts before any hook.

compare.js on napi/make_callback, 30 runs, Linux x64, one core, on main (3e5c8af, with #66395):

                                                     confidence improvement accuracy (*)   (**)  (***)
napi/make_callback n=1000000 type='AsyncResource'            **      6.57 %  ±4.56% ±6.07% ±7.91%
napi/make_callback n=1000000 type='Call'                            -2.91 %  ±5.61% ±7.51% ±9.86%
napi/make_callback n=1000000 type='MakeCallback'              *      7.47 %  ±5.64% ±7.50% ±9.77%
napi/make_callback n=10000000 type='AsyncResource'          ***      8.54 %  ±3.42% ±4.55% ±5.93%
napi/make_callback n=10000000 type='Call'                           -1.02 %  ±4.35% ±5.79% ±7.54%
napi/make_callback n=10000000 type='MakeCallback'           ***      7.30 %  ±2.85% ±3.80% ±4.96%

MakeCallback goes from 128.0 to 119.3 ns at n=1e7. test/async-hooks, the async hooks, AsyncLocalStorage, AsyncResource and nextTick tests in test/parallel, and the new addon test pass.

The new field and the chain are the part to look at first, the design was discussed in nodejs/performance#24.

Disclosure: I used Opus 5.5 (Max) as coding assistant

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Oct 4, 2026
With no async hook and no executionAsyncResource() user, every
InternalCallbackScope pushed and popped the async ids on the stack, only
for executionAsyncId() and triggerAsyncId() to read the top. Swap the
two ids in place instead: they are all that those two and
process.nextTick() read. executionAsyncResource() is the only reader of
the stack: the scopes that skip it form a chain in AsyncHooks, counted
in a new field, kLazyScopes, and JS asks C++ for the innermost one only
while that count is not zero. The checks for a corrupted stack stay.

Refs: nodejs/performance#24
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
@nigrosimone
nigrosimone marked this pull request as ready for review October 21, 2026 03:22
@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.15789% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.57%. Comparing base (3e5c8af) to head (21a6ea2).
⚠️ Report is 572 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/async_hooks.js 70.58% 5 Missing ⚠️
src/api/callback.cc 94.11% 1 Missing and 1 partial ⚠️
src/async_wrap.cc 92.30% 0 Missing and 1 partial ⚠️
src/env.cc 90.90% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66511      +/-   ##
==========================================
- Coverage   92.86%   90.57%   -2.29%     
==========================================
  Files         428      797     +369     
  Lines      195100   278432   +83332     
  Branches    30147    53510   +23363     
==========================================
+ Hits       181175   252200   +71025     
- Misses      13597    16582    +2985     
- Partials      328     9650    +9322     
Files with missing lines Coverage Δ
src/env.h 97.40% <ø> (ø)
src/node_internals.h 80.70% <100.00%> (ø)
src/async_wrap.cc 83.58% <92.30%> (ø)
src/env.cc 84.26% <90.90%> (ø)
src/api/callback.cc 81.78% <94.11%> (ø)
lib/internal/async_hooks.js 98.60% <70.58%> (+0.35%) ⬆️

... and 498 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.

executionAsyncResource() asked first inside a native callback, where the
scope skipped the id stack and holds its resource as a Global, and with
a context pushed on the stack above that scope.

Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
@nigrosimone

Copy link
Copy Markdown
Contributor Author

About the Codecov report: 21a6ea2 adds a test for the two gaps that can be covered, the lazy resource held as a Global (an fs callback) and a context pushed on the stack above a lazy scope.

The lines still reported, popAsyncContext() in lib/internal/async_hooks.js and the check in AsyncHooks::pop_async_context(), are the corrupted stack path. test-improper-order and test-emit-*-on-destroyed go through them, but the child process exits from C++ before it writes its coverage.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina 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 Nov 9, 2026
@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 Nov 9, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nigrosimone

Copy link
Copy Markdown
Contributor Author

@mcollina this one is approved since 9 November and has no commit-queue label. Could it go in the commit queue?

@mcollina mcollina added the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 23, 2026
@nodejs-github-bot nodejs-github-bot added commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. and removed commit-queue PRs queued for automated landing through the Commit Queue. labels Nov 23, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Commit Queue failed

This pull request has multiple commits, but no landing policy was selected.

Add commit-queue-squash PRs the Commit Queue should land as one squashed commit. to land it as one commit, or commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. to land the commits separately.

The pull request was removed from the Commit Queue and labeled commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. . After resolving the failure, remove that label and add commit-queue PRs queued for automated landing through the Commit Queue. to retry.

Full Commit Queue output
�[36m⠋�[39m Loading data for nodejs/node/pull/66511
�[36m⠋�[39m Loading data for nodejs/node/pull/66511
�[36m⠋�[39m Getting collaborator contacts from README of nodejs/node
�[36m⠋�[39m Getting PR from nodejs/node/pull/66511
�[36m⠋�[39m Getting reviews from nodejs/node/pull/66511
�[36m⠋�[39m Getting comments from nodejs/node/pull/66511
�[36m⠋�[39m Getting commits from nodejs/node/pull/66511
✔  Done loading data for nodejs/node/pull/66511
----------------------------------- PR info ------------------------------------
Title      src: skip the async id stack when nobody sees it (#66511)
Author     Nigro Simone <nigro.simone@gmail.com> (@nigrosimone)
Branch     nigrosimone:lazy-async-ids -> nodejs:main
Labels     c++, lib / src, needs-ci, commit-queue
Commits    2
 - src: skip the async id stack when nobody sees it
 - test: cover the resource lookup of a lazy async scope
Committers 1
 - Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66511
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/66511
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Sun, 04 Oct 2026 14:57:45 GMT
   ✔  Approvals: 1
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/66511#pullrequestreview-5828579602
   ✔  Last GitHub CI successful
   ℹ  Last Full PR CI on 2026-11-09T15:58:56Z: https://ci.nodejs.org/job/node-test-pull-request/78926/
�[36m⠙�[39m Querying data for job/node-test-pull-request/78926/
�[36m⠙�[39m Querying data for job/node-test-pull-request/78926/
�[36m⠙�[39m Querying API for job/node-test-pull-request/78926/
✔  Build data downloaded
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  No git cherry-pick in progress
   ✔  No git am in progress
   ✔  No git rebase in progress
--------------------------------------------------------------------------------
�[36m⠹�[39m Bringing origin/main up to date...
�[36m⠹�[39m Bringing origin/main up to date...
From https://github.com/nodejs/node
 * branch                  main       -> FETCH_HEAD
   660c104059..d32f1bd113  main       -> origin/main
✔  origin/main is now up-to-date
main is out of sync with origin/main. Mismatched commits:
 - 67e81a2c57 stream: run a replaced write() in the flowing pipe
 - d32f1bd113 stream: run a replaced write() in the flowing pipe
--------------------------------------------------------------------------------
HEAD is now at d32f1bd113 stream: run a replaced write() in the flowing pipe
   ✔  Reset to origin/main
�[36m⠸�[39m Downloading patch for 66511
�[36m⠸�[39m Downloading patch for 66511
From https://github.com/nodejs/node
 * branch                  refs/pull/66511/merge -> FETCH_HEAD
✔  Fetched commits as 660c1040592c..21a6ea2ff095
--------------------------------------------------------------------------------
Auto-merging src/env.cc
Auto-merging src/env.h
Auto-merging src/node_internals.h
[main c893b4db47] src: skip the async id stack when nobody sees it
 Author: Nigro Simone <nigro.simone@gmail.com>
 Date: Sun Oct 4 16:54:28 2026 +0200
 7 files changed, 144 insertions(+), 5 deletions(-)
 create mode 100755 test/addons/async-resource/test-execution-async-resource.js
[main ffe0b8d4c1] test: cover the resource lookup of a lazy async scope
 Author: Nigro Simone <nigro.simone@gmail.com>
 Date: Wed Oct 21 07:50:24 2026 +0200
 1 file changed, 27 insertions(+)
 create mode 100644 test/parallel/test-async-hooks-execution-async-resource-lazy.js
   ✔  Patches applied
There are 2 commits in the PR. Attempting autorebase.
(node:765) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated.
(Use `node --trace-deprecation ...` to show where the warning was created)
Rebasing (2/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
src: skip the async id stack when nobody sees it

With no async hook and no executionAsyncResource() user, every
InternalCallbackScope pushed and popped the async ids on the stack, only
for executionAsyncId() and triggerAsyncId() to read the top. Swap the
two ids in place instead: they are all that those two and
process.nextTick() read. executionAsyncResource() is the only reader of
the stack: the scopes that skip it form a chain in AsyncHooks, counted
in a new field, kLazyScopes, and JS asks C++ for the innermost one only
while that count is not zero. The checks for a corrupted stack stay.

Refs: https://github.com/nodejs/performance/issues/24
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66511
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
--------------------------------------------------------------------------------
[detached HEAD d9d554db84] src: skip the async id stack when nobody sees it
 Author: Nigro Simone <nigro.simone@gmail.com>
 Date: Sun Oct 4 16:54:28 2026 +0200
 7 files changed, 144 insertions(+), 5 deletions(-)
 create mode 100755 test/addons/async-resource/test-execution-async-resource.js
Rebasing (3/4)
Rebasing (4/4)
Executing: git node land --amend --yes
--------------------------------- New Message ----------------------------------
test: cover the resource lookup of a lazy async scope

executionAsyncResource() asked first inside a native callback, where the
scope skipped the id stack and holds its resource as a Global, and with
a context pushed on the stack above that scope.

Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66511
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
--------------------------------------------------------------------------------
[detached HEAD 7f439d546b] test: cover the resource lookup of a lazy async scope
 Author: Nigro Simone <nigro.simone@gmail.com>
 Date: Wed Oct 21 07:50:24 2026 +0200
 1 file changed, 27 insertions(+)
 create mode 100644 test/parallel/test-async-hooks-execution-async-resource-lazy.js
Successfully rebased and updated refs/heads/main.
--------------------------------------------------------------------------------
   ℹ  Add `commit-queue-squash` label to land the PR as one commit, or `commit-queue-rebase` to land as separate commits.

View workflow run

@mcollina mcollina added commit-queue PRs queued for automated landing through the Commit Queue. commit-queue-squash PRs the Commit Queue should land as one squashed commit. and removed commit-queue-failed PRs whose Commit Queue landing failed and need manual intervention before retrying. labels Nov 23, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 1ef4444 into nodejs:main Nov 23, 2026
78 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 1ef4444

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Nov 23, 2026
aduh95 pushed a commit that referenced this pull request Nov 23, 2026
With no async hook and no executionAsyncResource() user, every
InternalCallbackScope pushed and popped the async ids on the stack, only
for executionAsyncId() and triggerAsyncId() to read the top. Swap the
two ids in place instead: they are all that those two and
process.nextTick() read. executionAsyncResource() is the only reader of
the stack: the scopes that skip it form a chain in AsyncHooks, counted
in a new field, kLazyScopes, and JS asks C++ for the innermost one only
while that count is not zero. The checks for a corrupted stack stay.

Refs: nodejs/performance#24
Signed-off-by: Nigro Simone <nigro.simone@gmail.com>
PR-URL: #66511
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue-squash PRs the Commit Queue should land as one squashed commit. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants