Skip to content

async_hooks: enable AsyncLocalStorage once constructed #58029

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
merged 1 commit into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/internal/async_local_storage/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class AsyncLocalStorage {
if (options.name !== undefined) {
this.#name = `${options.name}`;
}

this._enable();
}

/** @type {string} */
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-async-local-storage-enter-with.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { AsyncLocalStorage } = require('async_hooks');

// Verify that `enterWith()` does not leak the store to the parent context in a promise.

const als = new AsyncLocalStorage();

async function asyncFunctionAfterAwait() {
await 0;
als.enterWith('after await');
}

function promiseThen() {
return Promise.resolve()
.then(() => {
als.enterWith('inside then');
});
}

async function asyncFunctionBeforeAwait() {
als.enterWith('before await');
await 0;
}

async function main() {
await asyncFunctionAfterAwait();
await promiseThen();
assert.strictEqual(als.getStore(), undefined);

// This is a known limitation of the `enterWith` API.
await asyncFunctionBeforeAwait();
assert.strictEqual(als.getStore(), 'before await');
}

main().then(common.mustCall());
Loading