Skip to content

Commit e2b8454

Browse files
aduh95targos
authored andcommitted
repl: fix Ctrl+C on top level await
PR-URL: #38656 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 608d0e1 commit e2b8454

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,
@@ -566,21 +564,24 @@ function REPLServer(prompt,
566564
promise = PromiseRace([promise, interrupt]);
567565
}
568566

569-
PromisePrototypeFinally(PromisePrototypeThen(promise, (result) => {
570-
finishExecution(null, result);
571-
}, (err) => {
572-
if (err && process.domain) {
573-
debug('not recoverable, send to domain');
574-
process.domain.emit('error', err);
575-
process.domain.exit();
576-
return;
567+
(async () => {
568+
try {
569+
const result = await promise;
570+
finishExecution(null, result);
571+
} catch (err) {
572+
if (err && process.domain) {
573+
debug('not recoverable, send to domain');
574+
process.domain.emit('error', err);
575+
process.domain.exit();
576+
return;
577+
}
578+
finishExecution(err);
579+
} finally {
580+
// Remove prioritized SIGINT listener if it was not called.
581+
prioritizedSigintQueue.delete(sigintListener);
582+
unpause();
577583
}
578-
finishExecution(err);
579-
}), () => {
580-
// Remove prioritized SIGINT listener if it was not called.
581-
prioritizedSigintQueue.delete(sigintListener);
582-
unpause();
583-
});
584+
})();
584585
}
585586
}
586587

@@ -768,7 +769,9 @@ function REPLServer(prompt,
768769
const prioritizedSigintQueue = new SafeSet();
769770
self.on('SIGINT', function onSigInt() {
770771
if (prioritizedSigintQueue.size > 0) {
771-
ArrayPrototypeForEach(prioritizedSigintQueue, (task) => task());
772+
for (const task of prioritizedSigintQueue) {
773+
task();
774+
}
772775
return;
773776
}
774777

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)