pillarjs / pillarjs/finalhandler
ERR_HTTP_HEADERS_SENT when headers are sent while finalhandler waits for the request to finish
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 241
- Forks
- 54
- Avg merge
- 7h 21m
- Merged PRs (30d)
- 1
Description
When a request has an unread body, finalhandler waits for the request to finish before sending the response.
If another middleware or asynchronous operation sends the response headers during this wait, res.headersSent can be false when finalhandler is called and true when the onFinished callback runs.
The callback does not check res.headersSent again. It attempts to modify the already-sent headers with res.removeHeader(), causing the following exception:
ERR_HTTP_HEADERS_SENT
Cannot remove headers after they are sent to the client
If the exception is not caught, the Node.js process may exit.
Conditions
- The request body has not been fully read.
- finalhandler registers a response write with
onFinished. - Another operation sends the response headers before the request finishes.
- The request finishes and the registered write runs.
Expected behavior
If the response headers have already been sent when the onFinished callback runs, finalhandler should leave the response unchanged and should not throw an exception.
Related
#36 explains why finalhandler waits for the request body to be fully read before sending the response.
This issue concerns the response state changing during that wait, rather than the wait itself.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at finalhandler's response-writing path and the onFinished callback described in the issue. Reproduce the unread-request-body sequence with another operation sending response headers during the wait, then verify the callback leaves an already-sent response unchanged and no ERR_HTTP_HEADERS_SENT exception occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 62/100