Skip to content

Commit 55fb81c

Browse files
DanielVenableaduh95
authored andcommitted
readline: fix unresolved promise on abortion
Fixes: #53497 PR-URL: #54030 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 31f98d7 commit 55fb81c

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

lib/internal/readline/interface.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ const {
3232
SymbolAsyncIterator,
3333
} = primordials;
3434

35-
const { codes: {
36-
ERR_INVALID_ARG_VALUE,
37-
ERR_USE_AFTER_CLOSE,
38-
} } = require('internal/errors');
35+
const {
36+
AbortError,
37+
codes: {
38+
ERR_INVALID_ARG_VALUE,
39+
ERR_USE_AFTER_CLOSE,
40+
},
41+
} = require('internal/errors');
3942

4043
const {
4144
validateAbortSignal,
@@ -110,6 +113,7 @@ const kPrompt = Symbol('_prompt');
110113
const kPushToKillRing = Symbol('_pushToKillRing');
111114
const kPushToUndoStack = Symbol('_pushToUndoStack');
112115
const kQuestionCallback = Symbol('_questionCallback');
116+
const kQuestionReject = Symbol('_questionReject');
113117
const kRedo = Symbol('_redo');
114118
const kRedoStack = Symbol('_redoStack');
115119
const kRefreshLine = Symbol('_refreshLine');
@@ -1125,6 +1129,7 @@ class Interface extends InterfaceConstructor {
11251129
} else {
11261130
// This readline instance is finished
11271131
this.close();
1132+
this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+C'));
11281133
}
11291134
break;
11301135

@@ -1136,6 +1141,7 @@ class Interface extends InterfaceConstructor {
11361141
if (this.cursor === 0 && this.line.length === 0) {
11371142
// This readline instance is finished
11381143
this.close();
1144+
this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+D'));
11391145
} else if (this.cursor < this.line.length) {
11401146
this[kDeleteRight]();
11411147
}
@@ -1391,6 +1397,7 @@ module.exports = {
13911397
kQuestion,
13921398
kQuestionCallback,
13931399
kQuestionCancel,
1400+
kQuestionReject,
13941401
kRefreshLine,
13951402
kSawKeyPress,
13961403
kSawReturnAt,

lib/readline/promises.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
Interface: _Interface,
1313
kQuestion,
1414
kQuestionCancel,
15+
kQuestionReject,
1516
} = require('internal/readline/interface');
1617

1718
const {
@@ -54,6 +55,8 @@ class Interface extends _Interface {
5455
};
5556
}
5657

58+
this[kQuestionReject] = reject;
59+
5760
this[kQuestion](query, cb);
5861
});
5962
}

test/parallel/test-readline-promises-interface.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,22 @@ for (let i = 0; i < 12; i++) {
951951
rli.close();
952952
}
953953

954+
// Aborting a question with ctrl+C
955+
{
956+
const [rli, fi] = getInterface({ terminal: true });
957+
assert.rejects(rli.question('hello?'), { name: 'AbortError' })
958+
.then(common.mustCall());
959+
fi.emit('keypress', '.', { ctrl: true, name: 'c' });
960+
}
961+
962+
// Aborting a question with ctrl+D
963+
{
964+
const [rli, fi] = getInterface({ terminal: true });
965+
assert.rejects(rli.question('hello?'), { name: 'AbortError' })
966+
.then(common.mustCall());
967+
fi.emit('keypress', '.', { ctrl: true, name: 'd' });
968+
}
969+
954970
(async () => {
955971
const [rli] = getInterface({ terminal });
956972
const signal = AbortSignal.abort('boom');

0 commit comments

Comments
 (0)