Skip to content

Commit

Permalink
async_hooks: remove experimental onPropagate option
Browse files Browse the repository at this point in the history
The `onPropagate` option for `AsyncLocalStorage` is problematic for a
couple of reasons:

1. It is not expected to be forwards compatible in any way with the
   upcoming TC-39 `AsyncContext` proposal.
2. It introduces a non-trivial O(n) cost invoking a JavaScript callback
   for *every* AsyncResource that is created, including every Promise.

While it is still experimental, I recommend removing it while we can
revisit the fundamental use cases in light of the coming `AsyncContext`
proposal.

Refs: #46374
PR-URL: #46386
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: Vladimir de Turckheim <[email protected]>
Reviewed-By: Geoffrey Booth <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Gerhard Stöbich <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
  • Loading branch information
jasnell authored and danielleadams committed Apr 3, 2023
1 parent f088ce2 commit b1bde69
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 79 deletions.
20 changes: 4 additions & 16 deletions doc/api/async_context.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,24 @@ Each instance of `AsyncLocalStorage` maintains an independent storage context.
Multiple instances can safely exist simultaneously without risk of interfering
with each other's data.

### `new AsyncLocalStorage([options])`
### `new AsyncLocalStorage()`

<!-- YAML
added:
- v13.10.0
- v12.17.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/46386
description: Removed experimental onPropagate option.
- version: v18.13.0
pr-url: https://github.com/nodejs/node/pull/45386
description: Add option onPropagate.
-->

> Stability: 1 - `options.onPropagate` is experimental.
* `options` {Object}
* `onPropagate` {Function} Optional callback invoked before a store is
propagated to a new async resource. Returning `true` allows propagation,
returning `false` avoids it. Default is to propagate always.

Creates a new instance of `AsyncLocalStorage`. Store is only provided within a
`run()` call or after an `enterWith()` call.

The `onPropagate` is called during creation of an async resource. Throwing at
this time will print the stack trace and exit. See
[`async_hooks` Error handling][] for details.

Creating an async resource within the `onPropagate` callback will result in
a recursive call to `onPropagate`.

### `asyncLocalStorage.disable()`

<!-- YAML
Expand Down Expand Up @@ -830,5 +819,4 @@ const server = createServer((req, res) => {
[`EventEmitter`]: events.md#class-eventemitter
[`Stream`]: stream.md#stream
[`Worker`]: worker_threads.md#class-worker
[`async_hooks` Error handling]: async_hooks.md#error-handling
[`util.promisify()`]: util.md#utilpromisifyoriginal
15 changes: 2 additions & 13 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const {
const { kEmptyObject } = require('internal/util');
const {
validateFunction,
validateObject,
validateString,
} = require('internal/validators');
const internal_async_hooks = require('internal/async_hooks');
Expand Down Expand Up @@ -275,17 +274,9 @@ const storageHook = createHook({
});

class AsyncLocalStorage {
constructor(options = kEmptyObject) {
validateObject(options, 'options');

const { onPropagate = null } = options;
if (onPropagate !== null) {
validateFunction(onPropagate, 'options.onPropagate');
}

constructor() {
this.kResourceStore = Symbol('kResourceStore');
this.enabled = false;
this._onPropagate = onPropagate;
}

disable() {
Expand All @@ -312,9 +303,7 @@ class AsyncLocalStorage {
_propagate(resource, triggerResource, type) {
const store = triggerResource[this.kResourceStore];
if (this.enabled) {
if (this._onPropagate === null || this._onPropagate(type, store)) {
resource[this.kResourceStore] = store;
}
resource[this.kResourceStore] = store;
}
}

Expand Down
50 changes: 0 additions & 50 deletions test/async-hooks/test-async-local-storage-stop-propagation.js

This file was deleted.

0 comments on commit b1bde69

Please sign in to comment.