Repository navigation
Update http module to return "408 - Request timeout"- when closing idle TCP connections. #38890
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.httpIssues and PRs related to the http subsystem.Issues and PRs related to the http subsystem.
on Jun 2, 2021 I think it should be done via framework.
There's currently no way for a framework to implement this, because http does not emit any event when closing an idle connection, nor provide a way to handle it. And it doesn't provide any way to intercept/override this functionality.
Even setting a timeout in the socket doesn't work, and even if it did, you have no way to know if response has already been sent:
server.on('connection', socket => { socket.on('timeout', () => { // This doesn't work. You are not allowed to write to a socket after a timeout. socket.write('408 - Request timeout\r\nConnection: close\r\n\r\n') console.log(new Date(), 'Socket timeout'); }); });The only alternative would be to override the socket write/close functions. But that would require overriding write function to parse sent data and analyze whether a response is being sent. That would have a significant performance impact.
Reacted by Zach Bjornsonany update?
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.
- addedstaleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.Issues and PRs marked stale due to inactivity and scheduled for automatic closure.
on Apr 26, 2022 There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment.
For more information on how the project manages feature requests, please consult the feature request management document.
Is your feature request related to a problem? YES
Currently, http implementation results in random HTTP errors, as a result of race conditions when closing idle connections.
Node closes idle TCP connections after 5 seconds (configurable using
server.keepAliveTimeout),resulting in a race conditions described at: https://nodejs.org/api/http.html#http_request_reusedsocket
This documentation proposes request might be retried when receiving a
ECONNRESETerror, and that's whatbrowsers do. However, this is not applicable when working with API Gateways, which can't close a downstream
connection (shared by multiple services) when an upstream connection is closed.
Instead, Gateways return a 503 error code when an upstream connection is closed, resulting in a HTTP error.
Additional info here.
The solution would be to configure
server.keepAliveTimeoutto be larger than the client. But Chrome keeps idle connections open for 5 to 6 minutes (according to link, up to 10 minutes). Using such a long timeout in the cloud might not be a good idea.Proposed solution
RFC7231#6.5.7 and Mozilla specify that servers MAY/SHOULD send a
408 – Request Timeoutresponse when closing idle connections, even without a previous request from the server. This would make any further request (if any) to receive a 408 error and would be automatically retried by the browser.Notice that, when using CORS, 408 response MUST include CORS headers (
Access-Control-Allow-Origin: *).Currently, there's no simple way to update http module, to return a 408 error when closing idle connections.
I propose updating http module, to allow handling the idle-connection close event. Eg:
Alternative solution
This feature could also be an option, when creating the HTTP server. Eg: