fix: properly handle prematurely closed requests#4772
Merged
mnaamani merged 2 commits intoJoystream:masterfrom Jul 12, 2023
Merged
fix: properly handle prematurely closed requests#4772mnaamani merged 2 commits intoJoystream:masterfrom
mnaamani merged 2 commits intoJoystream:masterfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
mnaamani
reviewed
Jun 12, 2023
| * closed prematurely). Otherwise, the request handler would try to set header or send the response | ||
| * again, which would result in an error e.g., "Cannot set headers after they are sent to the client" | ||
| * */ | ||
| if (!res.headersSent) { |
Member
There was a problem hiding this comment.
This should not be necessary. My hunch is that the root cause of this behavior is the invocation of res.end() in the close event handler on line 26. It just looks wrong. I'll test and confirm
Member
There was a problem hiding this comment.
confirmed that by removing the res.end() and the newly added conditional on res.headerSent, invoking socket.destroy(), execution does not reach invokation of await handler(req, res, next).
Also confirmed the log correctly includes the meta information for prematurelyClosed:
2023-06-12 18:57:17 2023-06-12 14:57:17:5717 PublicApi http: HTTP GET /api/v1/status
2023-06-12 18:57:17 {
2023-06-12 18:57:17 "meta": {
2023-06-12 18:57:17 "req": {
2023-06-12 18:57:17 "url": "/api/v1/status",
2023-06-12 18:57:17 "headers": {
2023-06-12 18:57:17 "host": "localhost:3334",
2023-06-12 18:57:17 "user-agent": "curl/8.1.2",
2023-06-12 18:57:17 "accept": "*/*"
2023-06-12 18:57:17 },
2023-06-12 18:57:17 "method": "GET",
2023-06-12 18:57:17 "httpVersion": "1.1",
2023-06-12 18:57:17 "originalUrl": "/api/v1/status",
2023-06-12 18:57:17 "query": {}
2023-06-12 18:57:17 },
2023-06-12 18:57:17 "res": {
2023-06-12 18:57:17 "statusCode": 200
2023-06-12 18:57:17 },
2023-06-12 18:57:17 "responseTime": 60,
2023-06-12 18:57:17 "prematurelyClosed": true
2023-06-12 18:57:17 }
2023-06-12 18:57:17 }
mnaamani
requested changes
Jun 12, 2023
Member
mnaamani
left a comment
There was a problem hiding this comment.
A simpler solution is to just not call res.end() in the closed event handler.
mnaamani
approved these changes
Jul 12, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
addresses #4760
Problem
Sometimes a request can be prematurely closed by client or due to bad network conditions. Whenever that happens, an event handler on response object (
res), manually closes the response by callingres.end(). However, the problem is that even if the response closes the the control flow is passed the request handlers which then tries to set headers on a closed response, and hence the error.How to reproduce
In routeWrapper function, manually close the response by calling
res.socket?.destory(),protected routeWrapper(handler: express.RequestHandler) { return async (req: express.Request, res: express.Response, next: express.NextFunction): Promise<void> => { // Fix for express-winston in order to also log prematurely closed requests + res.socket.?destroy() res.on('close', () => { res.locals.prematurelyClosed = !res.writableFinished res.end() }) + await sleep(1000) // sleep for 1 second to make sure that request does not finish before socket destroy event is emitted try { await handler(req, res, next) } catch (err) { next(err) } } }