diff --git a/lib/readline.js b/lib/readline.js index 60864f40afdbc1..6a1ed150d72cd2 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -1039,6 +1039,9 @@ function emitKeypressEvents(stream, iface) { } else { stream.on('newListener', onNewListener); } + if (iface) { + iface.once('close', () => { stream.removeListener('data', onData); }); + } } /** diff --git a/test/parallel/test-readline-set-raw-mode.js b/test/parallel/test-readline-set-raw-mode.js index db42a5a9495a9e..ddffd131d0baff 100644 --- a/test/parallel/test-readline-set-raw-mode.js +++ b/test/parallel/test-readline-set-raw-mode.js @@ -74,6 +74,8 @@ assert(!rawModeCalled); assert(resumeCalled); assert(!pauseCalled); +// One data listener for the keypress events. +assert.strictEqual(stream.listeners('data').length, 1); // close() should call setRawMode(false) expectedRawMode = false; @@ -86,5 +88,5 @@ assert(!resumeCalled); assert(pauseCalled); assert.deepStrictEqual(stream.listeners('keypress'), []); -// one data listener for the keypress events. -assert.strictEqual(stream.listeners('data').length, 1); +// Data listener is removed once interface is closed. +assert.strictEqual(stream.listeners('data').length, 0);