Skip to content

Commit

Permalink
readline: fix pre-aborted signal question handling
Browse files Browse the repository at this point in the history
fix pre-aborted question handling

PR-URL: #37929
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
Linkgoron authored and MylesBorins committed Apr 4, 2021
1 parent a1123f0 commit 6cc1e15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const {
StringPrototypeStartsWith,
StringPrototypeTrim,
Promise,
PromiseReject,
Symbol,
SymbolAsyncIterator,
SafeStringIterator,
Expand Down Expand Up @@ -385,6 +386,10 @@ Interface.prototype.question = function(query, options, cb) {
options = typeof options === 'object' && options !== null ? options : {};

if (options.signal) {
if (options.signal.aborted) {
return;
}

options.signal.addEventListener('abort', () => {
this[kQuestionCancel]();
}, { once: true });
Expand All @@ -405,6 +410,10 @@ Interface.prototype.question = function(query, options, cb) {
Interface.prototype.question[promisify.custom] = function(query, options) {
options = typeof options === 'object' && options !== null ? options : {};

if (options.signal && options.signal.aborted) {
return PromiseReject(new AbortError());
}

return new Promise((resolve, reject) => {
this.question(query, options, resolve);

Expand Down
25 changes: 25 additions & 0 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,31 @@ for (let i = 0; i < 12; i++) {
rli.close();
}

// pre-aborted signal
{
const signal = AbortSignal.abort();
const [rli] = getInterface({ terminal });
rli.pause();
rli.on('resume', common.mustNotCall());
rli.question('hello?', { signal }, common.mustNotCall());
rli.close();
}

// pre-aborted signal promisified question
{
const signal = AbortSignal.abort();
const [rli] = getInterface({ terminal });
const question = util.promisify(rli.question).bind(rli);
rli.on('resume', common.mustNotCall());
rli.pause();
question('hello?', { signal })
.then(common.mustNotCall())
.catch(common.mustCall((error) => {
assert.strictEqual(error.name, 'AbortError');
}));
rli.close();
}

// Can create a new readline Interface with a null output argument
{
const [rli, fi] = getInterface({ output: null, terminal });
Expand Down

0 comments on commit 6cc1e15

Please sign in to comment.