Files
node/test/parallel/test-timers-reset-process-domain-on-throw.js
Matteo Collina 2c4356cedf 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.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
PR-URL: https://github.com/nodejs/node/pull/61095
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
2026-06-21 19:42:03 +00:00

47 lines
1.6 KiB
JavaScript

'use strict';
// This test makes sure that when throwing from within a timer's callback,
// its active domain at the time of the throw is not the process' active domain
// for the next timers that need to be processed on the same turn of the event
// loop.
const common = require('../common');
const assert = require('assert');
const domain = require('domain');
// Use the same timeout value so that both timers' callbacks are called during
// the same invocation of the underlying native timer's callback (listOnTimeout
// in lib/timers.js).
setTimeout(err, 50);
setTimeout(common.mustCall(secondTimer), 50);
function err() {
const d = domain.create();
d.on('error', handleDomainError);
d.run(err2);
function err2() {
// This function doesn't exist, and throws an error as a result.
err3(); // eslint-disable-line no-undef
}
function handleDomainError(e) {
assert.strictEqual(e.domain, d);
// Domains' error handlers are called outside of their domain's context, so
// we're not expecting any active domain here.
assert.strictEqual(process.domain, undefined);
}
}
function secondTimer() {
// secondTimer was scheduled before any domain had been created, so its
// callback should not have any active domain set when it runs.
if (process.domain !== undefined) {
console.log('process.domain should be undefined in this timer callback, but is:',
process.domain);
// Do not use assert here, as it throws errors and if a domain with an error
// handler is active, then asserting wouldn't make the test fail.
process.exit(1);
}
}