From 0996eb71edbd47d9f9ec6153331255993fd6f0d1 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 12 May 2021 19:17:43 +0200 Subject: [PATCH] repl: fix Ctrl+C on top level await PR-URL: https://github.com/nodejs/node/pull/38656 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- lib/repl.js | 37 ++++++++++++---------- test/parallel/test-repl-top-level-await.js | 12 ++----- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index cf544fb224a446..2e80d652669ec5 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -74,8 +74,6 @@ const { ObjectKeys, ObjectSetPrototypeOf, Promise, - PromisePrototypeFinally, - PromisePrototypeThen, PromiseRace, ReflectApply, RegExp, @@ -566,21 +564,24 @@ function REPLServer(prompt, promise = PromiseRace([promise, interrupt]); } - PromisePrototypeFinally(PromisePrototypeThen(promise, (result) => { - finishExecution(null, result); - }, (err) => { - if (err && process.domain) { - debug('not recoverable, send to domain'); - process.domain.emit('error', err); - process.domain.exit(); - return; + (async () => { + try { + const result = await promise; + finishExecution(null, result); + } catch (err) { + if (err && process.domain) { + debug('not recoverable, send to domain'); + process.domain.emit('error', err); + process.domain.exit(); + return; + } + finishExecution(err); + } finally { + // Remove prioritized SIGINT listener if it was not called. + prioritizedSigintQueue.delete(sigintListener); + unpause(); } - finishExecution(err); - }), () => { - // Remove prioritized SIGINT listener if it was not called. - prioritizedSigintQueue.delete(sigintListener); - unpause(); - }); + })(); } } @@ -768,7 +769,9 @@ function REPLServer(prompt, const prioritizedSigintQueue = new SafeSet(); self.on('SIGINT', function onSigInt() { if (prioritizedSigintQueue.size > 0) { - ArrayPrototypeForEach(prioritizedSigintQueue, (task) => task()); + for (const task of prioritizedSigintQueue) { + task(); + } return; } diff --git a/test/parallel/test-repl-top-level-await.js b/test/parallel/test-repl-top-level-await.js index 464aae9b076d4b..39227d4f8bad29 100644 --- a/test/parallel/test-repl-top-level-await.js +++ b/test/parallel/test-repl-top-level-await.js @@ -164,18 +164,12 @@ async function ordinaryTests() { } async function ctrlCTest() { - putIn.run([ - `const timeout = (msecs) => new Promise((resolve) => { - setTimeout(resolve, msecs).unref(); - });`, - ]); - console.log('Testing Ctrl+C'); assert.deepStrictEqual(await runAndWait([ - 'await timeout(100000)', + 'await new Promise(() => {})', { ctrl: true, name: 'c' }, ]), [ - 'await timeout(100000)\r', + 'await new Promise(() => {})\r', 'Uncaught:', '[Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' + 'Script execution was interrupted by `SIGINT`] {', @@ -190,4 +184,4 @@ async function main() { await ctrlCTest(); } -main(); +main().then(common.mustCall());