Skip to content

Commit

Permalink
repl: fix Ctrl+C on top level await
Browse files Browse the repository at this point in the history
PR-URL: #38656
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
aduh95 committed May 15, 2021
1 parent 4a22850 commit 0996eb7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
37 changes: 20 additions & 17 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ const {
ObjectKeys,
ObjectSetPrototypeOf,
Promise,
PromisePrototypeFinally,
PromisePrototypeThen,
PromiseRace,
ReflectApply,
RegExp,
Expand Down Expand Up @@ -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();
});
})();
}
}

Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 3 additions & 9 deletions test/parallel/test-repl-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`] {',
Expand All @@ -190,4 +184,4 @@ async function main() {
await ctrlCTest();
}

main();
main().then(common.mustCall());

0 comments on commit 0996eb7

Please sign in to comment.