Skip to content

Commit

Permalink
readline: fix unresolved promise on abortion
Browse files Browse the repository at this point in the history
Fixes: #53497
PR-URL: #54030
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
  • Loading branch information
DanielVenable authored and jasnell committed Jan 30, 2025
1 parent 82ac335 commit 5557ce4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
15 changes: 11 additions & 4 deletions lib/internal/readline/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ const {
SymbolDispose,
} = primordials;

const { codes: {
ERR_INVALID_ARG_VALUE,
ERR_USE_AFTER_CLOSE,
} } = require('internal/errors');
const {
AbortError,
codes: {
ERR_INVALID_ARG_VALUE,
ERR_USE_AFTER_CLOSE,
},
} = require('internal/errors');

const {
validateAbortSignal,
Expand Down Expand Up @@ -111,6 +114,7 @@ const kPrompt = Symbol('_prompt');
const kPushToKillRing = Symbol('_pushToKillRing');
const kPushToUndoStack = Symbol('_pushToUndoStack');
const kQuestionCallback = Symbol('_questionCallback');
const kQuestionReject = Symbol('_questionReject');
const kRedo = Symbol('_redo');
const kRedoStack = Symbol('_redoStack');
const kRefreshLine = Symbol('_refreshLine');
Expand Down Expand Up @@ -1126,6 +1130,7 @@ class Interface extends InterfaceConstructor {
} else {
// This readline instance is finished
this.close();
this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+C'));
}
break;

Expand All @@ -1137,6 +1142,7 @@ class Interface extends InterfaceConstructor {
if (this.cursor === 0 && this.line.length === 0) {
// This readline instance is finished
this.close();
this[kQuestionReject]?.(new AbortError('Aborted with Ctrl+D'));
} else if (this.cursor < this.line.length) {
this[kDeleteRight]();
}
Expand Down Expand Up @@ -1392,6 +1398,7 @@ module.exports = {
kQuestion,
kQuestionCallback,
kQuestionCancel,
kQuestionReject,
kRefreshLine,
kSawKeyPress,
kSawReturnAt,
Expand Down
3 changes: 3 additions & 0 deletions lib/readline/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
Interface: _Interface,
kQuestion,
kQuestionCancel,
kQuestionReject,
} = require('internal/readline/interface');

const {
Expand Down Expand Up @@ -54,6 +55,8 @@ class Interface extends _Interface {
};
}

this[kQuestionReject] = reject;

this[kQuestion](query, cb);
});
}
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-readline-promises-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,22 @@ for (let i = 0; i < 12; i++) {
rli.close();
}

// Aborting a question with ctrl+C
{
const [rli, fi] = getInterface({ terminal: true });
assert.rejects(rli.question('hello?'), { name: 'AbortError' })
.then(common.mustCall());
fi.emit('keypress', '.', { ctrl: true, name: 'c' });
}

// Aborting a question with ctrl+D
{
const [rli, fi] = getInterface({ terminal: true });
assert.rejects(rli.question('hello?'), { name: 'AbortError' })
.then(common.mustCall());
fi.emit('keypress', '.', { ctrl: true, name: 'd' });
}

(async () => {
const [rli] = getInterface({ terminal });
const signal = AbortSignal.abort('boom');
Expand Down

0 comments on commit 5557ce4

Please sign in to comment.