-
Notifications
You must be signed in to change notification settings - Fork 108
remove socket error
handler after request is done
#142
Conversation
This LGTM, although I'll ping @othiym23 or @iarna as a backup set of eyes because Although it's a relatively trivial patch, it might be pretty nice to have a test written for it so no one in the distant future decides that "well, the socket is closing, so this cb is unnecessary" :) Thanks a lot for this. It'll be nice to stop having all that EventEmitter spam. 8D |
3012985
to
99628f5
Compare
Thing is, the only observable change that could really be tested for here (I think) is that the warning pops up. I’ve added a check to |
I was thinking of something along the lines of |
Yeah … I’m just afraid it’s going to be tricky (and depending on implementation details?) to get to the emitter itself, and to get a reliable number of event listeners at which to cut off… if you think it’s worth it, I’ll do the digging and see if it’s possible. |
I don't know if it's worth it either. I'll check back in on Monday to finalize this, but tbh it's probably fine without significant testing. :\ |
Is this solution, which feels a little hacky, just a consequence of using Either way, I'm inclined to land this, maybe with a comment added to the code to make clear why this patch was added (and that it might need to be updated, changed, or removed) in the future. |
Are you talking about the setup in
That’s quite understandable, I don’t know why this popped up either (or just became more frequent? I think it’s rather that?).
Hmm, got anything concrete in mind? Otherwise I’d prefer doing some more digging (starting to bisect |
Nope, we got no reports of this prior to the latest
I think it's request/request@bf86f17, which, to be honest, feels like a pretty brute-force fix for request/request#1903. From reading the thread on request/request#1903, it sounds like it's sort of a historical accident that request was removing error handlers in the first place, and it won't be coming back, but I could be wrong. |
`npm-registry-client` uses `request`, which in turn uses an HTTP agent for reusing sockets, so the `error` handlers registered on it in `npm-registry-client` just piled up and kept being attached over the entire lifetime of the socket. This patch seeks to fix this by removing the listener from the socket once the callback is invoked, as keeping it around after that would just be a memory leak.
9c37a04
to
dde0eb7
Compare
Yup, I can confirm that… the removed If I’m understanding it correctly, the |
This fix is almost verbatim what I'd thought would be the approach when I posted #143. It's probably safe to just remove the error handler, since socket errors are proxied to the request object now, and have been since (I think?) node v0.8, but I could be wrong and that should be investigated. |
Since 0.10 is the oldest version of Node the npm CLI team currently supports, and we're sunsetting our support for 0.10 real soon now (along with the Node project), I'm fine with just landing this change. Thanks for the followup, @isaacs! |
I believe this should fix npm/npm#13656 |
Hi y’all! 😄 Carrying this over from nodejs/node#8279 (comment):
npm-registry-client
usesrequest
, which in turn uses an HTTP agent for reusing sockets, so theerror
handlers registered in these lines innpm-registry-client
just pile up and keep being attached over the entire lifetime of the socket:which leads to the famous
Warning: Possible EventEmitter memory leak detected. 11 error listeners added.
warning.This patch seeks to fix this by removing the listener from the socket once the callback is invoked, as keeping it around after that would really just be a memory leak (… I can’t believe that warning message is right for once).
(btw, I’m still not sure having
npm-registry-client
attach error listeners on the socket itself is a correct thing to do, but given that it’s been that way since 2012 and this is a kind of high-profile package, I’m reluctant to do actual behavioural changes…)