Skip to content

Commit 16d5297

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 bdf75a6 commit 16d5297

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
@@ -2297,16 +2297,20 @@ Type: End-of-Life
22972297

22982298
<!-- YAML
22992299
changes:
2300+
- version: REPLACEME
2301+
pr-url: https://github.com/nodejs/node/pull/61095
2302+
description: End-of-Life.
23002303
- version: v10.0.0
23012304
pr-url: https://github.com/nodejs/node/pull/17417
23022305
description: Runtime deprecation.
23032306
-->
23042307

2305-
Type: Runtime
2308+
Type: End-of-Life
23062309

2307-
Users of `MakeCallback` that add the `domain` property to carry context,
2308-
should start using the `async_context` variant of `MakeCallback` or
2309-
`CallbackScope`, or the high-level `AsyncResource` class.
2310+
The `domain` property on async resources and `MakeCallback` has been removed.
2311+
The domain module now uses `AsyncLocalStorage` for context propagation instead
2312+
of `async_hooks`. Accessing the `domain` property on `AsyncResource` will throw
2313+
an error. Use `AsyncLocalStorage` instead for context propagation.
23102314

23112315
### DEP0098: AsyncHooks embedder `AsyncResource.emitBefore` and `AsyncResource.emitAfter` APIs
23122316

doc/api/errors.md

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

717+
<a id="ERR_ASYNC_RESOURCE_DOMAIN_REMOVED"></a>
718+
719+
### `ERR_ASYNC_RESOURCE_DOMAIN_REMOVED`
720+
721+
The `domain` property on `AsyncResource` has been removed. The domain module
722+
now uses `AsyncLocalStorage` for context propagation instead of `async_hooks`.
723+
Use `AsyncLocalStorage` instead for context propagation.
724+
717725
<a id="ERR_ASYNC_TYPE"></a>
718726

719727
### `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
ERR_INVALID_ARG_TYPE,
@@ -275,6 +276,10 @@ class AsyncResource {
275276
type ||= fn.name;
276277
return (new AsyncResource(type || 'bound-anonymous-fn')).bind(fn, thisArg);
277278
}
279+
280+
get domain() {
281+
throw new ERR_ASYNC_RESOURCE_DOMAIN_REMOVED();
282+
}
278283
}
279284

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

0 commit comments

Comments
 (0)