Skip to content

domain: port to AsyncLocalStorage#61095

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:domain-als-port-v2
Open

domain: port to AsyncLocalStorage#61095
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:domain-als-port-v2

Conversation

@mcollina
Copy link
Copy Markdown
Member

Port the domain module from createHook (async_hooks) to AsyncLocalStorage using the AsyncContextFrame-based implementation.

Key changes:

  • Use AsyncLocalStorage for domain context propagation instead of async_hooks.createHook()
  • Lazy initialization that triggers AsyncContextFrame prototype swap on first domain use
  • Use enterWith instead of ALS.run() so domain context is NOT automatically restored on exception - this matches the original domain.run() behavior where exit() only runs on success
  • Add ERR_ASYNC_RESOURCE_DOMAIN_REMOVED error for AsyncResource.domain
  • Update DEP0097 to End-of-Life status
  • Remove tests that relied on the removed MakeCallback domain property

The domain module now uses the AsyncContextFrame version of AsyncLocalStorage directly for proper context propagation across async boundaries.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/gyp
  • @nodejs/userland-migrations

@mcollina mcollina added the semver-major PRs that contain breaking changes and should be released in the next major version. label Dec 17, 2025
@nodejs-github-bot nodejs-github-bot added async_hooks Issues and PRs related to the async hooks subsystem. domain Issues and PRs related to the domain subsystem. errors Issues and PRs related to JavaScript errors originated in Node.js core. needs-ci PRs that need a full CI run. labels Dec 17, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 17, 2025

Codecov Report

❌ Patch coverage is 92.88390% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.29%. Comparing base (8d0a3b8) to head (dfb45e5).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
lib/domain.js 92.63% 19 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61095      +/-   ##
==========================================
- Coverage   90.29%   90.29%   -0.01%     
==========================================
  Files         730      730              
  Lines      234782   234899     +117     
  Branches    43953    43962       +9     
==========================================
+ Hits       211993   212095     +102     
- Misses      14501    14533      +32     
+ Partials     8288     8271      -17     
Files with missing lines Coverage Δ
lib/async_hooks.js 100.00% <100.00%> (ø)
lib/internal/async_hooks.js 99.36% <100.00%> (-0.02%) ⬇️
lib/internal/errors.js 97.65% <100.00%> (+<0.01%) ⬆️
lib/domain.js 95.31% <92.63%> (-3.03%) ⬇️

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

Comment thread doc/api/deprecations.md Outdated
@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 18, 2025
@mcollina mcollina force-pushed the domain-als-port-v2 branch 2 times, most recently from b307f20 to 1d24947 Compare December 18, 2025 11:01
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 19, 2025
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Copy link
Copy Markdown
Member

@Flarna Flarna left a comment

Choose a reason for hiding this comment

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

I have to admit that I do not fully understand domains so take my review with a grain of salt.

Comment thread test/parallel/test-domain-async-id-map-leak.js
Comment thread lib/domain.js
Comment thread lib/domain.js Outdated
Comment thread lib/domain.js Outdated
Comment thread lib/domain.js
@Flarna Flarna added the async_local_storage AsyncLocalStorage label Jan 5, 2026
@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 13, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 13, 2026
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

@Flarna
Copy link
Copy Markdown
Member

Flarna commented Mar 29, 2026

@mcollina seems this requires a rebase

Copy link
Copy Markdown
Member

@AugustinMauroy AugustinMauroy left a comment

Choose a reason for hiding this comment

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

Seem good

Comment thread doc/api/deprecations.md
The `domain` property on async resources and `MakeCallback` has been removed.
The domain module now uses `AsyncLocalStorage` for context propagation instead
of `async_hooks`. Accessing the `domain` property on `AsyncResource` will throw
an error. Use `AsyncLocalStorage` instead for context propagation.
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.

Matteo is it possible to have a before/after example of this depreciation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm not sure how, the property is not there anymore. If you used it, your code would not work anymore.

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.

oh got it

@mcollina mcollina force-pushed the domain-als-port-v2 branch 2 times, most recently from 23abf1e to 6adae72 Compare April 6, 2026 21:52
@mcollina mcollina force-pushed the domain-als-port-v2 branch from 6adae72 to 5b03c94 Compare May 28, 2026 07:53
@mcollina mcollina force-pushed the domain-als-port-v2 branch from 5b03c94 to 0d3b40f Compare May 28, 2026 07:54
Port the domain module from createHook (async_hooks) to
AsyncLocalStorage using the AsyncContextFrame-based implementation.

Key changes:
- Use AsyncLocalStorage for domain context propagation instead of
  async_hooks.createHook()
- Lazy initialization that triggers AsyncContextFrame prototype swap
  on first domain use
- Use enterWith instead of ALS.run() so domain context is NOT
  automatically restored on exception - this matches the original
  domain.run() behavior where exit() only runs on success
- Add ERR_ASYNC_RESOURCE_DOMAIN_REMOVED error for AsyncResource.domain
- Update DEP0097 to End-of-Life status
- Remove tests that relied on the removed MakeCallback domain property

The domain module now uses the AsyncContextFrame version of
AsyncLocalStorage directly for proper context propagation across
async boundaries.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina force-pushed the domain-als-port-v2 branch from 0d3b40f to dfb45e5 Compare May 28, 2026 07:54
@mcollina
Copy link
Copy Markdown
Member Author

@nodejs/tsc PTAL

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label May 28, 2026
@github-actions github-actions Bot added request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. and removed request-ci Add this label to start a Jenkins CI on a PR. labels May 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Failed to start CI
- Validating Jenkins credentials
✔  Jenkins credentials valid
- Querying data for job/node-test-pull-request/70926/
[SyntaxError: Unexpected token '<', ..."    
  https://github.com/nodejs/node/actions/runs/26565145418

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label May 28, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Failed to start CI
- Validating Jenkins credentials
✔  Jenkins credentials valid
- Querying data for job/node-test-pull-request/70926/
[SyntaxError: Unexpected token '<', ..."    
  https://github.com/nodejs/node/actions/runs/26585062544

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

async_hooks Issues and PRs related to the async hooks subsystem. async_local_storage AsyncLocalStorage domain Issues and PRs related to the domain subsystem. errors Issues and PRs related to JavaScript errors originated in Node.js core. needs-ci PRs that need a full CI run. request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. semver-major PRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants