Skip to content

stream: preserve asynclocalstorage context in finished() #57865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

gurgunday
Copy link
Member

@gurgunday gurgunday commented Apr 13, 2025

Fixes #57844

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Apr 13, 2025

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Apr 13, 2025
@jasnell
Copy link
Member

jasnell commented Apr 13, 2025

Inside the finished(...) function definition, you ought to be able to simply use AsyncLocalStore.bind(...) to wrap the callback function such that the current context is preserved. Essentially equivalent to the following except the AsyncLocalStorage.bind(...) call would be inside the finished(...) function body.

const { finished, Readable } = require('stream');
const { AsyncLocalStorage } = require('async_hooks');

const als = new AsyncLocalStorage();
const readable = new Readable();

als.run(123, () => {
  finished(readable, AsyncLocalStorage.bind(() => console.log(als.getStore())));
});

readable.destroy();

@gurgunday gurgunday force-pushed the fix/async-storage-stream-finished branch from 783785d to a29d44d Compare April 13, 2025 18:16
@gurgunday
Copy link
Member Author

@jasnell oh you're absolutely right, I missed that, thanks!

Copy link

codecov bot commented Apr 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.25%. Comparing base (964e41c) to head (cd26628).
Report is 444 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #57865      +/-   ##
==========================================
+ Coverage   90.23%   90.25%   +0.02%     
==========================================
  Files         630      630              
  Lines      185518   185674     +156     
  Branches    36369    36401      +32     
==========================================
+ Hits       167401   167580     +179     
+ Misses      11005    10992      -13     
+ Partials     7112     7102      -10     
Files with missing lines Coverage Δ
lib/internal/streams/end-of-stream.js 97.28% <100.00%> (+0.03%) ⬆️

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

Copy link
Member

@bjohansebas bjohansebas left a comment

Choose a reason for hiding this comment

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

It looks good, thanks for working on this

Also, lgtm if the CI passes

@bjohansebas bjohansebas added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 13, 2025
@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 Apr 13, 2025
Copy link
Contributor

Failed to start CI
   ⚠  No approving reviews found
   ✘  Refusing to run CI on potentially unsafe PR
https://github.com/nodejs/node/actions/runs/14433881994

Copy link
Member

@rluvaton rluvaton left a comment

Choose a reason for hiding this comment

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

LGTM

@rluvaton rluvaton added request-ci Add this label to start a Jenkins CI on a PR. and removed request-ci-failed An error occurred while starting CI via request-ci label, and manual interventon is needed. labels Apr 13, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 13, 2025
@nodejs-github-bot

This comment was marked as outdated.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added semver-minor PRs that contain new features and should be released in the next minor version. request-ci Add this label to start a Jenkins CI on a PR. labels Apr 14, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 14, 2025
@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Apr 14, 2025

@mcollina mcollina added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 14, 2025
@legendecas legendecas added the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 16, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Apr 16, 2025
@nodejs-github-bot nodejs-github-bot merged commit 4b2b3c0 into nodejs:main Apr 16, 2025
83 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 4b2b3c0

RafaelGSS pushed a commit that referenced this pull request May 1, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 2, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 6, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
RafaelGSS pushed a commit that referenced this pull request May 14, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
@bjohansebas bjohansebas added lts-watch-v20.x PRs that may need to be released in v20.x lts-watch-v22.x PRs that may need to be released in v22.x labels May 15, 2025
aduh95 pushed a commit that referenced this pull request May 16, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 17, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 17, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 17, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 pushed a commit that referenced this pull request May 18, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
@aduh95 aduh95 removed the lts-watch-v22.x PRs that may need to be released in v22.x label May 19, 2025
nodejs-github-bot added a commit that referenced this pull request May 19, 2025
Notable changes:

deps:
  * update timezone to 2025b (Node.js GitHub Bot) #57857
doc:
  * add dario-piotrowicz to collaborators (Dario Piotrowicz) #58102
  * (SEMVER-MINOR) graduate multiple experimental apis (James M Snell) #57765
esm:
  * (SEMVER-MINOR) graduate import.meta properties (James M Snell) #58011
  * (SEMVER-MINOR) support top-level Wasm without package type (Guy Bedford) #57610
sqlite:
  * (SEMVER-MINOR) add StatementSync.prototype.columns() (Colin Ihrig) #57490
src:
  * (SEMVER-MINOR) set default config as `node.config.json` (Marco Ippolito) #57171
  * (SEMVER-MINOR) create `THROW_ERR_OPTIONS_BEFORE_BOOTSTRAPPING` (Marco Ippolito) #57016
  * (SEMVER-MINOR) add config file support (Marco Ippolito) #57016
  * (SEMVER-MINOR) add ExecutionAsyncId getter for any Context (Attila Szegedi) #57820
stream:
  * (SEMVER-MINOR) preserve AsyncLocalStorage context in finished() (Gürgün Dayıoğlu) #57865
util:
  * (SEMVER-MINOR) add `types.isFloat16Array()` (Livia Medeiros) #57879
worker:
  * (SEMVER-MINOR) add worker.getHeapStatistics() (Matteo Collina) #57888

PR-URL: #58388
aduh95 pushed a commit that referenced this pull request May 19, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
aduh95 added a commit that referenced this pull request May 19, 2025
Notable changes:

deps:
  * update timezone to 2025b (Node.js GitHub Bot) #57857
doc:
  * add dario-piotrowicz to collaborators (Dario Piotrowicz) #58102
  * (SEMVER-MINOR) graduate multiple experimental apis (James M Snell) #57765
esm:
  * (SEMVER-MINOR) graduate import.meta properties (James M Snell) #58011
  * (SEMVER-MINOR) support top-level Wasm without package type (Guy Bedford) #57610
sqlite:
  * (SEMVER-MINOR) add StatementSync.prototype.columns() (Colin Ihrig) #57490
src:
  * (SEMVER-MINOR) set default config as `node.config.json` (Marco Ippolito) #57171
  * (SEMVER-MINOR) create `THROW_ERR_OPTIONS_BEFORE_BOOTSTRAPPING` (Marco Ippolito) #57016
  * (SEMVER-MINOR) add config file support (Marco Ippolito) #57016
  * (SEMVER-MINOR) add ExecutionAsyncId getter for any Context (Attila Szegedi) #57820
stream:
  * (SEMVER-MINOR) preserve AsyncLocalStorage context in finished() (Gürgün Dayıoğlu) #57865
util:
  * (SEMVER-MINOR) add `types.isFloat16Array()` (Livia Medeiros) #57879
worker:
  * (SEMVER-MINOR) add worker.getHeapStatistics() (Matteo Collina) #57888

PR-URL: TODO
aduh95 added a commit that referenced this pull request May 19, 2025
Notable changes:

deps:
  * update timezone to 2025b (Node.js GitHub Bot) #57857
doc:
  * add dario-piotrowicz to collaborators (Dario Piotrowicz) #58102
  * (SEMVER-MINOR) graduate multiple experimental apis (James M Snell) #57765
esm:
  * (SEMVER-MINOR) graduate import.meta properties (James M Snell) #58011
  * (SEMVER-MINOR) support top-level Wasm without package type (Guy Bedford) #57610
sqlite:
  * (SEMVER-MINOR) add StatementSync.prototype.columns() (Colin Ihrig) #57490
src:
  * (SEMVER-MINOR) set default config as `node.config.json` (Marco Ippolito) #57171
  * (SEMVER-MINOR) create `THROW_ERR_OPTIONS_BEFORE_BOOTSTRAPPING` (Marco Ippolito) #57016
  * (SEMVER-MINOR) add config file support (Marco Ippolito) #57016
  * (SEMVER-MINOR) add ExecutionAsyncId getter for any Context (Attila Szegedi) #57820
stream:
  * (SEMVER-MINOR) preserve AsyncLocalStorage context in finished() (Gürgün Dayıoğlu) #57865
util:
  * (SEMVER-MINOR) add `types.isFloat16Array()` (Livia Medeiros) #57879
worker:
  * (SEMVER-MINOR) add worker.getHeapStatistics() (Matteo Collina) #57888

PR-URL: #58388
aduh95 added a commit that referenced this pull request May 19, 2025
Notable changes:

deps:
  * update timezone to 2025b (Node.js GitHub Bot) #57857
doc:
  * add dario-piotrowicz to collaborators (Dario Piotrowicz) #58102
  * (SEMVER-MINOR) graduate multiple experimental apis (James M Snell) #57765
esm:
  * (SEMVER-MINOR) graduate import.meta properties (James M Snell) #58011
  * (SEMVER-MINOR) support top-level Wasm without package type (Guy Bedford) #57610
sqlite:
  * (SEMVER-MINOR) add StatementSync.prototype.columns() (Colin Ihrig) #57490
src:
  * (SEMVER-MINOR) set default config as `node.config.json` (Marco Ippolito) #57171
  * (SEMVER-MINOR) create `THROW_ERR_OPTIONS_BEFORE_BOOTSTRAPPING` (Marco Ippolito) #57016
  * (SEMVER-MINOR) add config file support (Marco Ippolito) #57016
  * (SEMVER-MINOR) add ExecutionAsyncId getter for any Context (Attila Szegedi) #57820
stream:
  * (SEMVER-MINOR) preserve AsyncLocalStorage context in finished() (Gürgün Dayıoğlu) #57865
util:
  * (SEMVER-MINOR) add `types.isFloat16Array()` (Livia Medeiros) #57879
worker:
  * (SEMVER-MINOR) add worker.getHeapStatistics() (Matteo Collina) #57888

PR-URL: #58388
aduh95 added a commit to aduh95/node that referenced this pull request May 20, 2025
Notable changes:

deps:
  * update timezone to 2025b (Node.js GitHub Bot) nodejs#57857
doc:
  * add dario-piotrowicz to collaborators (Dario Piotrowicz) nodejs#58102
  * (SEMVER-MINOR) graduate multiple experimental apis (James M Snell) nodejs#57765
esm:
  * (SEMVER-MINOR) graduate import.meta properties (James M Snell) nodejs#58011
  * (SEMVER-MINOR) support top-level Wasm without package type (Guy Bedford) nodejs#57610
sqlite:
  * (SEMVER-MINOR) add StatementSync.prototype.columns() (Colin Ihrig) nodejs#57490
src:
  * (SEMVER-MINOR) set default config as `node.config.json` (Marco Ippolito) nodejs#57171
  * (SEMVER-MINOR) create `THROW_ERR_OPTIONS_BEFORE_BOOTSTRAPPING` (Marco Ippolito) nodejs#57016
  * (SEMVER-MINOR) add config file support (Marco Ippolito) nodejs#57016
  * (SEMVER-MINOR) add ExecutionAsyncId getter for any Context (Attila Szegedi) nodejs#57820
stream:
  * (SEMVER-MINOR) preserve AsyncLocalStorage context in finished() (Gürgün Dayıoğlu) nodejs#57865
util:
  * (SEMVER-MINOR) add `types.isFloat16Array()` (Livia Medeiros) nodejs#57879
worker:
  * (SEMVER-MINOR) add worker.getHeapStatistics() (Matteo Collina) nodejs#57888

PR-URL: nodejs#58388
aduh95 added a commit that referenced this pull request May 21, 2025
Notable changes:

deps:
  * update timezone to 2025b (Node.js GitHub Bot) #57857
doc:
  * add dario-piotrowicz to collaborators (Dario Piotrowicz) #58102
  * (SEMVER-MINOR) graduate multiple experimental apis (James M Snell) #57765
esm:
  * (SEMVER-MINOR) graduate import.meta properties (James M Snell) #58011
  * (SEMVER-MINOR) support top-level Wasm without package type (Guy Bedford) #57610
sqlite:
  * (SEMVER-MINOR) add StatementSync.prototype.columns() (Colin Ihrig) #57490
src:
  * (SEMVER-MINOR) set default config as `node.config.json` (Marco Ippolito) #57171
  * (SEMVER-MINOR) create `THROW_ERR_OPTIONS_BEFORE_BOOTSTRAPPING` (Marco Ippolito) #57016
  * (SEMVER-MINOR) add config file support (Marco Ippolito) #57016
  * (SEMVER-MINOR) add ExecutionAsyncId getter for any Context (Attila Szegedi) #57820
stream:
  * (SEMVER-MINOR) preserve AsyncLocalStorage context in finished() (Gürgün Dayıoğlu) #57865
util:
  * (SEMVER-MINOR) add `types.isFloat16Array()` (Livia Medeiros) #57879
worker:
  * (SEMVER-MINOR) add worker.getHeapStatistics() (Matteo Collina) #57888

PR-URL: #58388
@marco-ippolito marco-ippolito removed the lts-watch-v20.x PRs that may need to be released in v20.x label Jun 5, 2025
marco-ippolito pushed a commit that referenced this pull request Jun 5, 2025
PR-URL: #57865
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
@marco-ippolito marco-ippolito added the dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. label Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
async_local_storage AsyncLocalStorage author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cannot read stored value in asynclocalstorage when using finished
10 participants