Skip to content

async_hooks: enable AsyncLocalStorage once constructed - #58029

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
legendecas:als/enable
Apr 27, 2025
Merged

async_hooks: enable AsyncLocalStorage once constructed#58029
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
legendecas:als/enable

Conversation

@legendecas

@legendecaslegendecas commented Apr 25, 2025

Copy link
Copy Markdown
Member

This fixes the leak behavior when using enterWith when no
AsyncLocalStorages were enabled inside a promise. With this
change, the following code snippets will behave the same:

import{AsyncLocalStorage}from'node:async_hooks';constals=newAsyncLocalStorage();asyncfunctionmain(){await1als.enterWith('internal');}awaitmain()console.log(als.getStore()??'default');
import{AsyncLocalStorage}from'node:async_hooks';constals=newAsyncLocalStorage();als.enterWith(undefined);// Forcefully enable the AsyncLocalStorage.asyncfunctionmain(){await1als.enterWith('internal');}awaitmain()console.log(als.getStore()??'default');

On the performance side, given that an AsyncLocalStorage is constructed, we should assume that it will be used and prefer correctness over lazy-initialization optimization.

Fixes: #53037
Refs: #58019

/cc @nodejs/diagnostics

This fixes the leak behavior when using `enterWith` when no
`AsyncLocalStorage`s were enabled inside a promise.
@nodejs-github-botnodejs-github-bot added async_local_storage AsyncLocalStorage needs-ci PRs that need a full CI run. labels Apr 25, 2025
@codecov

codecovBot commented Apr 25, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.27%. Comparing base (25fe802) to head (0ee37f3).
Report is 18 commits behind head on main.

Additional details and impacted files
@@ Coverage Diff @@## main #58029 +/- ##
==========================================
- Coverage 90.27% 90.27% -0.01% 
==========================================
Files 630 630 Lines 186159 186161 +2 Branches 36473 36476 +3 ==========================================
- Hits 168053 168048 -5 + Misses 10976 10974 -2 - Partials 7130 7139 +9 
Files with missing linesCoverage Δ
lib/internal/async_local_storage/async_hooks.js97.95% <100.00%> (+0.02%)⬆️

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

@FlarnaFlarna added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 26, 2025
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 26, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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

@mcollinamcollina 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

@Qard

Qard commented Apr 26, 2025

Copy link
Copy Markdown
Member

Does this only reproduce with the async_hooks version of AsyncLocalStorage?

@legendecas

Copy link
Copy Markdown
MemberAuthor

Does this only reproduce with the async_hooks version of AsyncLocalStorage?

Yes.

@legendecaslegendecas added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. labels Apr 27, 2025
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 27, 2025
@nodejs-github-bot
nodejs-github-bot merged commit 8e7ae60 into nodejs:mainApr 27, 2025
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 8e7ae60

@legendecas
legendecas deleted the als/enable branch April 27, 2025 23:48
@mcollina

Copy link
Copy Markdown
Member

I'm adding a backing-for-lts comment as we should evaluate if it has real-life breakage before backporting

@mcollinamcollina added the baking-for-lts PRs that need to wait before landing in a LTS release. label Apr 28, 2025
RafaelGSS pushed a commit that referenced this pull request May 1, 2025
This fixes the leak behavior when using `enterWith` when no
`AsyncLocalStorage`s were enabled inside a promise.
PR-URL: #58029Fixes: #53037
Refs: #58019
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 2, 2025
This fixes the leak behavior when using `enterWith` when no
`AsyncLocalStorage`s were enabled inside a promise.
PR-URL: #58029Fixes: #53037
Refs: #58019
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

async_local_storageAsyncLocalStorageauthor readyPRs that have at least one approval, no pending requests for changes, and a CI started.baking-for-ltsPRs that need to wait before landing in a LTS release.needs-ciPRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AsyncLocalStorage loses the store if used together with createHook if there is a async function in between

6 participants

@legendecas@nodejs-github-bot@Qard@mcollina@jasnell@Flarna