You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HTTP request setTimeout(0) does not remove timeout listener #25499
Unless we're mistaken, setTimeout(0) on an HTTP request (and then socket) can lead to a timeout listener not being removed. This is problematic because combined with a keepalive agent, it can lead to a memory leak.
This Gist by Szymon illustrates the issue. For convenience, I'm also quoting his inline explanation here:
In this case this never happens, because we set the timeout on that particular event. The correct behaviour would be to prevent calling listenSocketTimeout if msecs is 0.
Is there any reason for using req.setTimeout(0) after response ends?
When the response ends (or the user cancels the request), got clears all timeouts to make sure no TimeoutError will be thrown. There are some custom timeouts - see this timed-out module.
Activity
addaleax commentedon Jan 14, 2019
@nodejs/http
lpinca commentedon Jan 16, 2019
Is there any reason for using
req.setTimeout(0)after response ends?This re-adds the listener right after it is removed
node/lib/_http_client.js
Lines 591 to 594 in 968e901
because the
timeoutCbflag is cleared when response ends.I think we can make
req.setTimeout()a noop if used after the'end'event is emitted on the response.szmarczak commentedon Jan 16, 2019
When the response ends (or the user cancels the request),
gotclears all timeouts to make sure no TimeoutError will be thrown. There are some custom timeouts - see thistimed-outmodule.timdp commentedon Jan 16, 2019
And even if @szmarczak didn't have a valid reason to do so, a bit of state checking that prevents the memory leak would be cool. 🙂
lpinca commentedon Jan 16, 2019
Yes I agree. If you have some spare time to open a PR please do.
timdp commentedon Jan 16, 2019
Spare time is debatable but I've created #25536. (Sorry about the force-push spam.)
http: make ClientRequest#setTimeout() a no-op after end
http: make ClientRequest#setTimeout() noop at end
http: make ClientRequest#setTimeout() noop at end
http: make ClientRequest#setTimeout() noop at end
http: make ClientRequest#setTimeout() noop at end
http: make ClientRequest#setTimeout() noop at end