Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: fix Ctrl+C on top level await #38656

Merged
merged 1 commit into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());