Skip to content
Open
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
95 changes: 95 additions & 0 deletions doc/api/async_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,83 @@ try {
}
```

### `asyncLocalStorage.withScope(store)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

* `store` {any}
* Returns: {RunScope}

Creates a disposable scope that enters the given store and automatically
restores the previous store value when the scope is disposed. This method is
designed to work with JavaScript's explicit resource management (`using` syntax).

Example:

```mjs
import { AsyncLocalStorage } from 'node:async_hooks';

const asyncLocalStorage = new AsyncLocalStorage();

{
using scope = asyncLocalStorage.withScope('my-store');
console.log(asyncLocalStorage.getStore()); // Prints: my-store
}

console.log(asyncLocalStorage.getStore()); // Prints: undefined
```

```cjs
const { AsyncLocalStorage } = require('node:async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

{
using scope = asyncLocalStorage.withScope('my-store');
console.log(asyncLocalStorage.getStore()); // Prints: my-store
}

console.log(asyncLocalStorage.getStore()); // Prints: undefined
```

The `withScope()` method is particularly useful for managing context in
synchronous code where you want to ensure the previous store value is restored
when exiting a block, even if an error is thrown.

```mjs
import { AsyncLocalStorage } from 'node:async_hooks';

const asyncLocalStorage = new AsyncLocalStorage();

try {
using scope = asyncLocalStorage.withScope('my-store');
console.log(asyncLocalStorage.getStore()); // Prints: my-store
throw new Error('test');
} catch (e) {
// Store is automatically restored even after error
console.log(asyncLocalStorage.getStore()); // Prints: undefined
}
```

```cjs
const { AsyncLocalStorage } = require('node:async_hooks');

const asyncLocalStorage = new AsyncLocalStorage();

try {
using scope = asyncLocalStorage.withScope('my-store');
console.log(asyncLocalStorage.getStore()); // Prints: my-store
throw new Error('test');
} catch (e) {
// Store is automatically restored even after error
console.log(asyncLocalStorage.getStore()); // Prints: undefined
}
```

### Usage with `async/await`

If, within an async function, only one `await` call is to run within a context,
Expand Down Expand Up @@ -420,6 +497,22 @@ of `asyncLocalStorage.getStore()` after the calls you suspect are responsible
for the loss. When the code logs `undefined`, the last callback called is
probably responsible for the context loss.

## Class: `RunScope`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

A disposable scope returned by [`asyncLocalStorage.withScope()`][] that
automatically restores the previous store value when disposed. This class
implements the [Explicit Resource Management][] protocol and is designed to work
with JavaScript's `using` syntax.

The scope automatically restores the previous store value when the `using` block
exits, whether through normal completion or by throwing an error.

## Class: `AsyncResource`

<!-- YAML
Expand Down Expand Up @@ -905,8 +998,10 @@ const server = createServer((req, res) => {
}).listen(3000);
```

[Explicit Resource Management]: https://github.com/tc39/proposal-explicit-resource-management
[`AsyncResource`]: #class-asyncresource
[`EventEmitter`]: events.md#class-eventemitter
[`Stream`]: stream.md#stream
[`Worker`]: worker_threads.md#class-worker
[`asyncLocalStorage.withScope()`]: #asynclocalstoragewithscopestore
[`util.promisify()`]: util.md#utilpromisifyoriginal
6 changes: 6 additions & 0 deletions lib/internal/async_local_storage/async_context_frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const {
const AsyncContextFrame = require('internal/async_context_frame');
const { AsyncResource } = require('async_hooks');

const RunScope = require('internal/async_local_storage/run_scope');

class AsyncLocalStorage {
#defaultValue = undefined;
#name = undefined;
Expand Down Expand Up @@ -77,6 +79,10 @@ class AsyncLocalStorage {
}
return frame?.get(this);
}

withScope(store) {
return new RunScope(this, store);
}
}

module.exports = AsyncLocalStorage;
6 changes: 6 additions & 0 deletions lib/internal/async_local_storage/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const {
executionAsyncResource,
} = require('async_hooks');

const RunScope = require('internal/async_local_storage/run_scope');

const storageList = [];
const storageHook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
Expand Down Expand Up @@ -142,6 +144,10 @@ class AsyncLocalStorage {
}
return this.#defaultValue;
}

withScope(store) {
return new RunScope(this, store);
}
}

module.exports = AsyncLocalStorage;
27 changes: 27 additions & 0 deletions lib/internal/async_local_storage/run_scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const {
SymbolDispose,
} = primordials;

class RunScope {
#storage;
#previousStore;
#disposed = false;

constructor(storage, store) {
this.#storage = storage;
this.#previousStore = storage.getStore();
storage.enterWith(store);
}

[SymbolDispose]() {
if (this.#disposed) {
return;
}
this.#disposed = true;
this.#storage.enterWith(this.#previousStore);
}
}

module.exports = RunScope;
165 changes: 165 additions & 0 deletions test/parallel/test-async-local-storage-run-scope.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* eslint-disable no-unused-vars */
'use strict';
require('../common');
const assert = require('node:assert');
const { AsyncLocalStorage } = require('node:async_hooks');

// Test basic RunScope with using
{
const storage = new AsyncLocalStorage();

assert.strictEqual(storage.getStore(), undefined);

{
using scope = storage.withScope('test');
assert.strictEqual(storage.getStore(), 'test');
}

// Store should be restored to undefined
assert.strictEqual(storage.getStore(), undefined);
}

// Test RunScope restores previous value
{
const storage = new AsyncLocalStorage();

storage.enterWith('initial');
assert.strictEqual(storage.getStore(), 'initial');

{
using scope = storage.withScope('scoped');
assert.strictEqual(storage.getStore(), 'scoped');
}

// Should restore to previous value
assert.strictEqual(storage.getStore(), 'initial');
}

// Test nested RunScope
{
const storage = new AsyncLocalStorage();
const storeValues = [];

{
using outer = storage.withScope('outer');
storeValues.push(storage.getStore());

{
using inner = storage.withScope('inner');
storeValues.push(storage.getStore());
}

// Should restore to outer
storeValues.push(storage.getStore());
}

// Should restore to undefined
storeValues.push(storage.getStore());

assert.deepStrictEqual(storeValues, ['outer', 'inner', 'outer', undefined]);
}

// Test RunScope with error during usage
{
const storage = new AsyncLocalStorage();

storage.enterWith('before');

const testError = new Error('test');

assert.throws(() => {
using scope = storage.withScope('during');
assert.strictEqual(storage.getStore(), 'during');
throw testError;
}, testError);

// Store should be restored even after error
assert.strictEqual(storage.getStore(), 'before');
}

// Test idempotent disposal
{
const storage = new AsyncLocalStorage();

const scope = storage.withScope('test');
assert.strictEqual(storage.getStore(), 'test');

// Dispose via Symbol.dispose
scope[Symbol.dispose]();
assert.strictEqual(storage.getStore(), undefined);

// Double dispose should be idempotent
scope[Symbol.dispose]();
assert.strictEqual(storage.getStore(), undefined);
}

// Test RunScope with defaultValue
{
const storage = new AsyncLocalStorage({ defaultValue: 'default' });

assert.strictEqual(storage.getStore(), 'default');

{
using scope = storage.withScope('custom');
assert.strictEqual(storage.getStore(), 'custom');
}

// Should restore to default
assert.strictEqual(storage.getStore(), 'default');
}

// Test deeply nested RunScope
{
const storage = new AsyncLocalStorage();

{
using s1 = storage.withScope(1);
assert.strictEqual(storage.getStore(), 1);

{
using s2 = storage.withScope(2);
assert.strictEqual(storage.getStore(), 2);

{
using s3 = storage.withScope(3);
assert.strictEqual(storage.getStore(), 3);

{
using s4 = storage.withScope(4);
assert.strictEqual(storage.getStore(), 4);
}

assert.strictEqual(storage.getStore(), 3);
}

assert.strictEqual(storage.getStore(), 2);
}

assert.strictEqual(storage.getStore(), 1);
}

assert.strictEqual(storage.getStore(), undefined);
}

// Test RunScope with multiple storages
{
const storage1 = new AsyncLocalStorage();
const storage2 = new AsyncLocalStorage();

{
using scope1 = storage1.withScope('A');

{
using scope2 = storage2.withScope('B');

assert.strictEqual(storage1.getStore(), 'A');
assert.strictEqual(storage2.getStore(), 'B');
}

assert.strictEqual(storage1.getStore(), 'A');
assert.strictEqual(storage2.getStore(), undefined);
}

assert.strictEqual(storage1.getStore(), undefined);
assert.strictEqual(storage2.getStore(), undefined);
}
1 change: 1 addition & 0 deletions tools/doc/type-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const customTypesMap = {
'https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects',

'AsyncLocalStorage': 'async_context.html#class-asynclocalstorage',
'RunScope': 'async_context.html#class-runscope',

'AsyncHook': 'async_hooks.html#async_hookscreatehookoptions',
'AsyncResource': 'async_hooks.html#class-asyncresource',
Expand Down
Loading