Repository navigation
"drain" event listener leak when unpiping to response #135
Copy link
Copy link
Open
Labels
Description
Activity
there is also a leak caused by mapping ServerResponse.once to ServerResponse.on so there is no easy way to wait for 'drain' events to happen by just using the
response.once('drain', ...)Reacted by Zach Bjornson- changed the title
[-]"drain" event listener leak[/-][+]"drain" event listener leak when unpiping to response[/+]on Mar 7, 2019 - changed the title
[-]"drain" event listener leak when unpiping to response[/-][+]"drain" event listener leak when unpiping to response, using once("drain") or removeListener("drain")[/+]on Mar 7, 2019 - changed the title
[-]"drain" event listener leak when unpiping to response, using once("drain") or removeListener("drain")[/-][+]"drain" event listener leak when unpiping to response[/+]on Mar 7, 2019 - added 12 commits that reference this issue
on Mar 7, 2019 - added 3 commits that reference this issue
on Jul 16, 2020 If anyone came here looking for a workaround for the ability to call
res.once('drain', ...)without have node warn aboutMaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 drain listeners added to [Gzip]. Use emitter.setMaxListeners() to increase limit..This is a bit of a mess, but you can do something like this:
let onDrain; // add a single drain listener early on res.on('drain', () => { if (onDrain) { // call the onDrain callback at most once onDrain(); onDrain = undefined; } }); // later, when you would normally call res.once('drain', ...) onDrain = () => { // resume your input stream or whatever };
I hope that helps someone feeling stuck on this old issue.
Reacted by Matej Volf- added a commit that references this issue
on Sep 16, 2026
This is sort of a weird scenario, but this test case fails:
I hit this in some code that retries creating a read stream until the source exists. We clean up from our side:
rs.on("error", e => { rs.unpipe("res"); }). Seems likecompressionneeds to be cleaning up its listeners when"unpipe"happens.