Skip to content

Commit 761dafa

Browse files
aduh95targos
authored andcommitted
repl: fix Ctrl+C on top level await
PR-URL: #38656 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent e7761b6 commit 761dafa

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

lib/repl.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ const {
7474
ObjectKeys,
7575
ObjectSetPrototypeOf,
7676
Promise,
77-
PromisePrototypeFinally,
78-
PromisePrototypeThen,
7977
PromiseRace,
8078
ReflectApply,
8179
RegExp,
@@ -571,21 +569,24 @@ function REPLServer(prompt,
571569
promise = PromiseRace([promise, interrupt]);
572570
}
573571

574-
PromisePrototypeFinally(PromisePrototypeThen(promise, (result) => {
575-
finishExecution(null, result);
576-
}, (err) => {
577-
if (err && process.domain) {
578-
debug('not recoverable, send to domain');
579-
process.domain.emit('error', err);
580-
process.domain.exit();
581-
return;
572+
(async () => {
573+
try {
574+
const result = await promise;
575+
finishExecution(null, result);
576+
} catch (err) {
577+
if (err && process.domain) {
578+
debug('not recoverable, send to domain');
579+
process.domain.emit('error', err);
580+
process.domain.exit();
581+
return;
582+
}
583+
finishExecution(err);
584+
} finally {
585+
// Remove prioritized SIGINT listener if it was not called.
586+
prioritizedSigintQueue.delete(sigintListener);
587+
unpause();
582588
}
583-
finishExecution(err);
584-
}), () => {
585-
// Remove prioritized SIGINT listener if it was not called.
586-
prioritizedSigintQueue.delete(sigintListener);
587-
unpause();
588-
});
589+
})();
589590
}
590591
}
591592

@@ -789,7 +790,9 @@ function REPLServer(prompt,
789790
const prioritizedSigintQueue = new SafeSet();
790791
self.on('SIGINT', function onSigInt() {
791792
if (prioritizedSigintQueue.size > 0) {
792-
ArrayPrototypeForEach(prioritizedSigintQueue, (task) => task());
793+
for (const task of prioritizedSigintQueue) {
794+
task();
795+
}
793796
return;
794797
}
795798

test/parallel/test-repl-top-level-await.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,18 +164,12 @@ async function ordinaryTests() {
164164
}
165165

166166
async function ctrlCTest() {
167-
putIn.run([
168-
`const timeout = (msecs) => new Promise((resolve) => {
169-
setTimeout(resolve, msecs).unref();
170-
});`,
171-
]);
172-
173167
console.log('Testing Ctrl+C');
174168
assert.deepStrictEqual(await runAndWait([
175-
'await timeout(100000)',
169+
'await new Promise(() => {})',
176170
{ ctrl: true, name: 'c' },
177171
]), [
178-
'await timeout(100000)\r',
172+
'await new Promise(() => {})\r',
179173
'Uncaught:',
180174
'[Error [ERR_SCRIPT_EXECUTION_INTERRUPTED]: ' +
181175
'Script execution was interrupted by `SIGINT`] {',
@@ -190,4 +184,4 @@ async function main() {
190184
await ctrlCTest();
191185
}
192186

193-
main();
187+
main().then(common.mustCall());

0 commit comments

Comments
 (0)