Skip to content

Commit a39a0e7

Browse files
committed
readline: fix pre-aborted signal question handling
fix pre-aborted question handling
1 parent a9cdeed commit a39a0e7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/readline.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ Interface.prototype.question = function(query, options, cb) {
376376
options = typeof options === 'object' && options !== null ? options : {};
377377

378378
if (options.signal) {
379+
if (options.signal.aborted) {
380+
return;
381+
}
382+
379383
options.signal.addEventListener('abort', () => {
380384
this[kQuestionCancel]();
381385
}, { once: true });
@@ -397,8 +401,11 @@ Interface.prototype.question[promisify.custom] = function(query, options) {
397401
options = typeof options === 'object' && options !== null ? options : {};
398402

399403
return new Promise((resolve, reject) => {
400-
this.question(query, options, resolve);
404+
if (options.signal?.aborted) {
405+
throw new AbortError();
406+
}
401407

408+
this.question(query, options, resolve);
402409
if (options.signal) {
403410
options.signal.addEventListener('abort', () => {
404411
reject(new AbortError());

test/parallel/test-readline-interface.js

+25
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,31 @@ for (let i = 0; i < 12; i++) {
968968
rli.close();
969969
}
970970

971+
// pre-aborted signal
972+
{
973+
const signal = AbortSignal.abort();
974+
const [rli] = getInterface({ terminal });
975+
rli.pause();
976+
rli.on('resume', common.mustNotCall());
977+
rli.question('hello?', { signal }, common.mustNotCall());
978+
rli.close();
979+
}
980+
981+
// pre-aborted signal promisified question
982+
{
983+
const signal = AbortSignal.abort();
984+
const [rli] = getInterface({ terminal });
985+
const question = util.promisify(rli.question).bind(rli);
986+
rli.on('resume', common.mustNotCall());
987+
rli.pause();
988+
question('hello?', { signal })
989+
.then(common.mustNotCall())
990+
.catch(common.mustCall((error) => {
991+
assert.strictEqual(error.name, 'AbortError');
992+
}));
993+
rli.close();
994+
}
995+
971996
// Can create a new readline Interface with a null output argument
972997
{
973998
const [rli, fi] = getInterface({ output: null, terminal });

0 commit comments

Comments
 (0)