[PR #491] [MERGED] fix: add headersSent guard in API route error handler #611

Closed
opened 2026-05-06 13:09:04 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/491
Author: @NathanDrake2406
Created: 3/12/2026
Status: Merged
Merged: 3/12/2026
Merged by: @james-elicx

Base: mainHead: fix/api-handler-headers-sent-guard


📝 Commits (3)

  • 3d12fb9 fix: add headersSent guard in API route error handler
  • ffd0071 fix: end response when headers already sent but stream not closed
  • 0192ebc chore: retrigger CI

📊 Changes

1 file changed (+10 additions, -6 deletions)

View changed files

📝 packages/vinext/src/server/api-handler.ts (+10 -6)

📄 Description

Summary

  • Adds a res.headersSent check in the handleApiRoute catch block before writing error responses (500/413)
  • If an API handler partially writes headers (e.g. res.writeHead(200)) and then throws, the catch block would crash with ERR_HTTP_HEADERS_SENT — now it silently skips the error response since the connection is already committed
  • Matches the existing guard pattern already used in prod-server.ts:688 and index.ts:3518

Test plan

  • pnpm test tests/pages-router.test.ts — 166/166 passing
  • pnpm run fmt:check — clean
  • CI (format, lint, typecheck, vitest, playwright)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/491 **Author:** [@NathanDrake2406](https://github.com/NathanDrake2406) **Created:** 3/12/2026 **Status:** ✅ Merged **Merged:** 3/12/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/api-handler-headers-sent-guard` --- ### 📝 Commits (3) - [`3d12fb9`](https://github.com/cloudflare/vinext/commit/3d12fb9690ed1bc06fad0b5cef0e32097d24f83c) fix: add headersSent guard in API route error handler - [`ffd0071`](https://github.com/cloudflare/vinext/commit/ffd0071a25c778ff4e6d643903b2879869b2fcc5) fix: end response when headers already sent but stream not closed - [`0192ebc`](https://github.com/cloudflare/vinext/commit/0192ebc646bd82fdac21bf66e61a4a0c8b93fc16) chore: retrigger CI ### 📊 Changes **1 file changed** (+10 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/server/api-handler.ts` (+10 -6) </details> ### 📄 Description ## Summary - Adds a `res.headersSent` check in the `handleApiRoute` catch block before writing error responses (500/413) - If an API handler partially writes headers (e.g. `res.writeHead(200)`) and then throws, the catch block would crash with `ERR_HTTP_HEADERS_SENT` — now it silently skips the error response since the connection is already committed - Matches the existing guard pattern already used in `prod-server.ts:688` and `index.ts:3518` ## Test plan - [x] `pnpm test tests/pages-router.test.ts` — 166/166 passing - [x] `pnpm run fmt:check` — clean - [x] CI (format, lint, typecheck, vitest, playwright) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:09:04 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#611
No description provided.