Skip to content

Commit

Permalink
readline: clean up event listener in onNewListener
Browse files Browse the repository at this point in the history
Once the Readline interface is closed, the 'data' event listener should
be removed.

PR-URL: nodejs#13266
Ref: nodejs#9447 (comment)
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
gibfahn authored and jasnell committed Jun 5, 2017
1 parent bccda4f commit 81ddeb9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,9 @@ function emitKeypressEvents(stream, iface) {
} else {
stream.on('newListener', onNewListener);
}
if (iface) {
iface.once('close', () => { stream.removeListener('data', onData); });
}
}

/**
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-readline-set-raw-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

0 comments on commit 81ddeb9

Please sign in to comment.