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 <admin@stephenbelanger.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
jasnell authored and MylesBorins committed Feb 18, 2023
1 parent 33a98c4 commit 05ad792
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 80 deletions.
24 changes: 7 additions & 17 deletions doc/api/async_context.md
Expand Up @@ -116,35 +116,26 @@ 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: v19.2.0
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/46386
description: Removed experimental onPropagate option.
- version:
- v19.2.0
- 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 @@ -834,5 +825,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
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 05ad792

Please sign in to comment.