repl: remove dependency on domain module · nodejs/node@67b854d · GitHub
Skip to content

Commit 67b854d

Browse files
mcollinaaduh95
authored andcommitted
repl: remove dependency on domain module
Replace the domain-based error handling with AsyncLocalStorage and setUncaughtExceptionCaptureCallback. This removes the REPL's dependency on the deprecated domain module while preserving all existing behavior: - Synchronous errors during eval are caught and displayed - Async errors (setTimeout, promises, etc.) are caught via the uncaught exception capture callback - Top-level await errors are caught and displayed - The REPL continues operating after errors - Multiple REPL instances can coexist with errors routed correctly Changes: - Use AsyncLocalStorage to track which REPL instance owns an async context, replacing domain's automatic async tracking - Add setupExceptionCapture() to install setUncaughtExceptionCaptureCallback for catching async errors and routing them to the correct REPL - Extract error handling logic into REPLServer.prototype._handleError() - Wrap eval execution in replContext.run() for async context tracking - Update newListener protection to check AsyncLocalStorage context - Throw ERR_INVALID_ARG_VALUE if options.domain is passed PR-URL: #61227 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 62d2cd4 commit 67b854d

26 files changed

Lines changed: 417 additions & 289 deletions

doc/api/process.md

Lines changed: 46 additions & 2 deletions

lib/domain.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,11 @@ const {
4040
ReflectApply,
4141
SafeMap,
4242
SafeWeakMap,
43-
StringPrototypeRepeat,
4443
Symbol,
4544
} = primordials;
4645

4746
const EventEmitter = require('events');
4847
const {
49-
ERR_DOMAIN_CALLBACK_NOT_AVAILABLE,
50-
ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE,
5148
ERR_UNHANDLED_ERROR,
5249
} = require('internal/errors').codes;
5350
const { createHook } = require('async_hooks');
@@ -119,22 +116,9 @@ const asyncHook = createHook({
119116
},
120117
});
121118

122-
// When domains are in use, they claim full ownership of the
123-
// uncaught exception capture callback.
124-
if (process.hasUncaughtExceptionCaptureCallback()) {
125-
throw new ERR_DOMAIN_CALLBACK_NOT_AVAILABLE();
126-
}
127-
128-
// Get the stack trace at the point where `domain` was required.
129-
// eslint-disable-next-line no-restricted-syntax
130-
const domainRequireStack = new Error('require(`domain`) at this point').stack;
131-
119+
// Domain uses the stacking capability of setUncaughtExceptionCaptureCallback
120+
// to coexist with other callbacks (e.g., REPL).
132121
const { setUncaughtExceptionCaptureCallback } = process;
133-
process.setUncaughtExceptionCaptureCallback = function(fn) {
134-
const err = new ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE();
135-
err.stack += `\n${StringPrototypeRepeat('-', 40)}\n${domainRequireStack}`;
136-
throw err;
137-
};
138122

139123

140124
let sendMakeCallbackDeprecation = false;

lib/internal/bootstrap/node.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ ObjectDefineProperty(process, 'features', {
307307
const {
308308
onGlobalUncaughtException,
309309
setUncaughtExceptionCaptureCallback,
310+
addUncaughtExceptionCaptureCallback,
310311
hasUncaughtExceptionCaptureCallback,
311312
} = require('internal/process/execution');
312313

@@ -319,6 +320,8 @@ ObjectDefineProperty(process, 'features', {
319320
process._fatalException = onGlobalUncaughtException;
320321
process.setUncaughtExceptionCaptureCallback =
321322
setUncaughtExceptionCaptureCallback;
323+
process.addUncaughtExceptionCaptureCallback =
324+
addUncaughtExceptionCaptureCallback;
322325
process.hasUncaughtExceptionCaptureCallback =
323326
hasUncaughtExceptionCaptureCallback;
324327
}

lib/internal/process/execution.js

Lines changed: 47 additions & 14 deletions

0 commit comments

Comments
 (0)