Skip to content

Commit

Permalink
readline: fix question stack overflow
Browse files Browse the repository at this point in the history
This commit fixes readline interface's question callback wrapping when
it is being called with `signal` option. Previous version completely
overwrites passed callback and throws "Maximum call stack size exceeded"
error.
  • Loading branch information
chapko committed Jun 5, 2022
1 parent 45d7ca9 commit 1357094
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ Interface.prototype.question = function(query, options, cb) {
const cleanup = () => {
options.signal.removeEventListener(onAbort);
};
const originalCb = cb;
cb = typeof cb === 'function' ? (answer) => {
cleanup();
return cb(answer);
return originalCb(answer);
} : cleanup;
}

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

// Calling the question callback with abort signal
{
const [rli] = getInterface({ terminal });
const signal = new AbortController().signal;
rli.question('foo?', { signal }, common.mustCall((answer) => {
assert.strictEqual(answer, 'bar');
}));
rli.write('bar\n');
rli.close();
}

// Calling the question multiple times
{
const [rli] = getInterface({ terminal });
Expand Down

0 comments on commit 1357094

Please sign in to comment.