nitrojs / nitrojs/nitro

bug: manipulating cookies in the error hook gives the error `Cannot set headers after they are sent to the client`

Open
#2,771 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Environment

Node.js: 18.20.3
Nitropack: 2.9.7

Reproduction

https://stackblitz.com/edit/github-dyj94s

Describe the bug

I created a plugin and set up an error hook, in the error hook I return a promise and use setTimeout to execute resolve, simulating the situation of awaiting a promise (possibly performing redis operations, etc).

However, it seems that when executing the error hook, the response headers were sent directly without waiting for the hook to finish executing, which resulted in a Cannot set headers after they are sent to the client error in the setCookie in the setTimeout.

I don't know if this is intentional or if it's a hookable/h3 package error, but since I'm writing a session package, I need to handle session changes as well when the error occurs. Or am I using it incorrectly?

I will provide additional information if needed. Thank you.

Additional context

No response

Logs
handler false
promise false
timeout true
[nitro] [uncaughtException] Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at __node_internal_captureLargerStackTrace2 (https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:101:5335)
    at new NodeError (https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:101:4149)
    at ServerResponse.setHeader (https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:6:8796)
    at Module.setCookie (file:///home/projects/github-dyj94s/node_modules/h3/dist/index.mjs:734:18)
    at Timeout.eval [as _onTimeout] (file:///home/projects/github-dyj94s/.nitro/dev/index.mjs:693:74)
    at https://githubdyj94s-cjd3.w-credentialless-staticblitz.com/builtins.ddb8d84d.js:246:4692 {
  code: 'ERR_HTTP_HEADERS_SENT'

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked StackBlitz reproduction with the reported Nitro and Node.js versions. Trace the plugin error hook, its delayed promise resolution, and h3's setCookie call to determine whether the response is sent before the hook completes. Done means the expected async behavior is established and the headers error is prevented or clearly documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.