From 135709484b48e85dc5bd70647c82dcce801029d3 Mon Sep 17 00:00:00 2001 From: Eugene Chapko Date: Sun, 5 Jun 2022 10:51:55 +0400 Subject: [PATCH] readline: fix question stack overflow 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. --- lib/readline.js | 3 ++- test/parallel/test-readline-interface.js | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/readline.js b/lib/readline.js index 0957de58506a66..ad858e73a7b502 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -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; } diff --git a/test/parallel/test-readline-interface.js b/test/parallel/test-readline-interface.js index 82e9d9c2791e48..25d099f23c025d 100644 --- a/test/parallel/test-readline-interface.js +++ b/test/parallel/test-readline-interface.js @@ -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 });