-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Only apply drain listener when callback provided #25679
Conversation
LGTM, although it would be better if the code actually checked that |
hmm... according to the docs, |
Agreed, although I'd be concerned that this was a breaking change for anyone currently calling In my specific case, requiring a callback would also mean that the I can't speak for the original intent of the I'm happy to update the docs to reflect |
@joyent/node-tsc ... what do you think? |
this.once('drain', function() { | ||
self.flush(callback); | ||
}); | ||
if (callback) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're going to add this check, might as well make it a check for a function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
I'm also running into this problem (node v4.2.1). Also using flush() with SSE. Disagree with @CraigCav that compression is not useful. My stream goes from 600 KiB down to 34 KiB with compression enabled on an SSE stream. |
@usernameisalreadytaken2014 I wasn't saying that compression is not useful. I was saying that in my specific circumstances, disabling the compression middleware for SSE routes might be an acceptable work-around. Disabling compression for all SSE routes is not going to be an appropriate solution for everyone. I would much rather have the listener only applied if an optional callback is provided. |
@CraigCav - check! I misunderstood, apologies. Did you find a workaround? Does attaching a callback help? (And if so, is it necessary to hold back further write()s until the callback has been activated, or?...) |
I don't think so. The problem as I see it is that, regardless of whether a callback is provided, a I haven't found a workaround (other than removing compression which doesn't sound helpful for your case). It might be possible to remove all |
@CraigCav would it make sense to resubmit this PR against nodejs/node? |
@thealphanerd I was planning to, but my dev machine is broken for a couple days anyways. Feel free to take it if @CraigCav isn't around. :) |
The fix is now on master and v4.x-staging |
When using the compression middleware in express with Server-Sent Events, I've noticed a
possible EventEmitter memory leak detected. 11 drain listeners added.
message appearing from Gzip.addListener:The error can be reproduced using the SSE example on the expressjs/compression readme.
When
stream.flush()
is called without a callback, an empty listener is being added. Since flush may be called multiple times to push SSE's down to the client, multiple noop listeners are being added. This in turn causes thememory leak detected
message.Judging by line 449, an attempt has been made to avoid this situation, however, not all code paths have had the empty listener check applied.