Skip to content

Commit 1d24947

Browse files
committed
domain: port to AsyncLocalStorage
Port the domain module from createHook (async_hooks) to AsyncLocalStorage using the AsyncContextFrame-based implementation. Key changes: - Use AsyncLocalStorage for domain context propagation instead of async_hooks.createHook() - Lazy initialization that triggers AsyncContextFrame prototype swap on first domain use - Use enterWith instead of ALS.run() so domain context is NOT automatically restored on exception - this matches the original domain.run() behavior where exit() only runs on success - Add ERR_ASYNC_RESOURCE_DOMAIN_REMOVED error for AsyncResource.domain - Update DEP0097 to End-of-Life status - Remove tests that relied on the removed MakeCallback domain property The domain module now uses the AsyncContextFrame version of AsyncLocalStorage directly for proper context propagation across async boundaries.
1 parent 81e05e1 commit 1d24947

File tree

16 files changed

+349
-408
lines changed

16 files changed

+349
-408
lines changed

doc/api/deprecations.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,16 +2272,20 @@ Type: End-of-Life
22722272

22732273
<!-- YAML
22742274
changes:
2275+
- version: REPLACEME
2276+
pr-url: https://github.com/nodejs/node/pull/61095
2277+
description: End-of-Life.
22752278
- version: v10.0.0
22762279
pr-url: https://github.com/nodejs/node/pull/17417
22772280
description: Runtime deprecation.
22782281
-->
22792282

2280-
Type: Runtime
2283+
Type: End-of-Life
22812284

2282-
Users of `MakeCallback` that add the `domain` property to carry context,
2283-
should start using the `async_context` variant of `MakeCallback` or
2284-
`CallbackScope`, or the high-level `AsyncResource` class.
2285+
The `domain` property on async resources and `MakeCallback` has been removed.
2286+
The domain module now uses `AsyncLocalStorage` for context propagation instead
2287+
of `async_hooks`. Accessing the `domain` property on `AsyncResource` will throw
2288+
an error. Use `AsyncLocalStorage` instead for context propagation.
22852289

22862290
### DEP0098: AsyncHooks embedder `AsyncResource.emitBefore` and `AsyncResource.emitAfter` APIs
22872291

doc/api/errors.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,14 @@ An attempt was made to register something that is not a function as an
710710
An operation related to module loading is customized by an asynchronous loader
711711
hook that never settled the promise before the loader thread exits.
712712

713+
<a id="ERR_ASYNC_RESOURCE_DOMAIN_REMOVED"></a>
714+
715+
### `ERR_ASYNC_RESOURCE_DOMAIN_REMOVED`
716+
717+
The `domain` property on `AsyncResource` has been removed. The domain module
718+
now uses `AsyncLocalStorage` for context propagation instead of `async_hooks`.
719+
Use `AsyncLocalStorage` instead for context propagation.
720+
713721
<a id="ERR_ASYNC_TYPE"></a>
714722

715723
### `ERR_ASYNC_TYPE`

lib/async_hooks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616

1717
const {
1818
ERR_ASYNC_CALLBACK,
19+
ERR_ASYNC_RESOURCE_DOMAIN_REMOVED,
1920
ERR_ASYNC_TYPE,
2021
ERR_INVALID_ASYNC_ID,
2122
} = require('internal/errors').codes;
@@ -262,6 +263,10 @@ class AsyncResource {
262263
type ||= fn.name;
263264
return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn, thisArg);
264265
}
266+
267+
get domain() {
268+
throw new ERR_ASYNC_RESOURCE_DOMAIN_REMOVED();
269+
}
265270
}
266271

267272
// Placing all exports down here because the exported classes won't export

0 commit comments

Comments
 (0)