Skip to content

Commit

Permalink
repl: fix Ctrl+C on top level await
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 committed May 12, 2021
1 parent fb1acd0 commit f2d1f4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
35 changes: 20 additions & 15 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,21 +566,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 +771,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 f2d1f4e

Please sign in to comment.