@@ -209,16 +209,16 @@ const kBufferedCommandSymbol = Symbol('bufferedCommand');
209209const kLoadingSymbol = Symbol ( 'loading' ) ;
210210
211211function processNewListener ( event , listener ) {
212- if ( event === 'uncaughtException' &&
213- process . domain &&
214- listener . name !== 'domainUncaughtExceptionClear' &&
215- domainSet . has ( process . domain ) ) {
216- // Throw an error so that the event will not be added and the current
217- // domain takes over. That way the user is notified about the error
218- // and the current code evaluation is stopped, just as any other code
219- // that contains an error.
220- throw new ERR_INVALID_REPL_INPUT (
221- 'Listeners for `uncaughtException` cannot be used in the REPL' ) ;
212+ if ( event === 'uncaughtException' ) {
213+ const store = replContext . getStore ( ) ;
214+ if ( store ?. replServer ) {
215+ // Throw an error so that the event will not be added and the
216+ // current REPL handles it. That way the user is notified about
217+ // the error and the current code evaluation is stopped, just as
218+ // any other code that contains an error.
219+ throw new ERR_INVALID_REPL_INPUT (
220+ 'Listeners for `uncaughtException` cannot be used in the REPL' ) ;
221+ }
222222 }
223223}
224224
@@ -396,24 +396,9 @@ class REPLServer extends Interface {
396396 // It is possible to introspect the running REPL accessing this variable
397397 // from inside the REPL. This is useful for anyone working on the REPL.
398398 module . exports . repl = this ;
399- } else if ( ! addedNewListener ) {
400- // Add this listener only once. Otherwise we'd have to add a single
401- // listener to each REPL instance and that could trigger the
402- // `MaxListenersExceededWarning`.
403- process . prependListener ( 'newListener' , ( event , listener ) => {
404- if ( event === 'uncaughtException' ) {
405- const store = replContext . getStore ( ) ;
406- if ( store ?. replServer ) {
407- // Throw an error so that the event will not be added and the
408- // current REPL handles it. That way the user is notified about
409- // the error and the current code evaluation is stopped, just as
410- // any other code that contains an error.
411- throw new ERR_INVALID_REPL_INPUT (
412- 'Listeners for `uncaughtException` cannot be used in the REPL' ) ;
413- }
414- }
415- } ) ;
416- addedNewListener = true ;
399+ } else {
400+ addProcessNewListener ( ) ;
401+ this . once ( 'exit' , removeProcessNewListener ) ;
417402 }
418403
419404 // Set up exception capture for async error handling
0 commit comments