Skip to content

fix: commit response on Flush, pass through 1xx, and clear stale error headers - #3243

Merged
vishr merged 4 commits into
masterfrom
fix/response-commit-and-error-headers
Nov 20, 2026
Merged

vishr merged 4 commits into
masterfrom
fix/response-commit-and-error-headers

Conversation

@vishr

@vishr vishr commented Nov 20, 2026

Copy link
Copy Markdown
Member

Fixes #3240, #3241 and #3242. All three were found while reviewing #3239.

#3240: Response.Flush commits the response

  • When nothing has been written yet, Flush now commits through WriteHeader with the current status (default 200), the same way Write does. Before hooks run, Committed is accurate, and the error handler no longer writes a body after headers were sent.
  • If the writer can't flush, Flush panics before committing, so Recover can still send a 500. The check uses the same writer lookup as http.ResponseController, except that it looks through *Response.
  • delayedStatusWriter.Flush (used by c.JSON) now sends its pending status before flushing, so c.JSON(201, …) with a serializer that flushes no longer sends 200.

#3241: 1xx status codes

  • Response.WriteHeader sends 100–199 (except 101 Switching Protocols) straight to the underlying writer. It doesn't run Before hooks or change Status/Committed, so a handler can send 103 Early Hints and then its final status.
  • The HEAD wrapper (headResponseWriter), delayedStatusWriter and the Gzip writer pass 1xx codes through too, instead of storing them as the final status.

#3242: stale headers on error responses

  • DefaultHTTPErrorHandler and ProblemDetailsHTTPErrorHandler now remove Content-Encoding, Content-Length and Content-Disposition before writing the error body. These headers always describe the body the handler meant to send, not the error body. Before, an error after c.Attachment downloaded the error JSON as the attachment.
  • Content-Type, Content-Range and validators (ETag, Last-Modified) are kept, because they can be meant for the error response (a vendor media type, a 416, a 304).

Behavior changes

  • A middleware that sets Content-Encoding before the body is written and compresses everything sent through it will lose that header on error responses written while it is active (for example by RequestLogger with HandleError: true). Echo's own Gzip sets the header only when it starts compressing, so it is not affected.
  • Custom HTTPErrorHandlers are unchanged. They still need to clear these headers themselves.

Testing

  • go test -race ./... passes and make lint is clean.
  • Each new test was checked to fail without its fix.
  • The branch went through four high-effort review rounds. The remaining findings are minor, involve broken third-party writers, or would need new public API.

🤖 Generated with Claude Code

vishr and others added 4 commits November 20, 2026 07:32
…r headers

Response.Flush now commits an uncommitted response through WriteHeader,
so Before hooks run, Committed is accurate, and the error handler no
longer writes a body after the headers were already sent.

Response.WriteHeader forwards informational status codes (1xx except
101) without committing the response, so a handler can send 103 Early
Hints and still set its final status.

DefaultHTTPErrorHandler removes Content-Length before writing its body,
and removes Content-Encoding when no middleware wraps the response.

Fixes #3240
Fixes #3241
Fixes #3242

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Both DefaultHTTPErrorHandler and ProblemDetailsHTTPErrorHandler now
  clear the headers that described the failed response
  (Content-Encoding, Content-Length, Content-Type, Content-Disposition,
  Content-Range, ETag, Last-Modified), whatever wraps the writer.
- Response.Flush panics before committing when the writer cannot flush,
  so Recover can still send a 500.
- headResponseWriter, delayedStatusWriter and the Gzip writer forward
  1xx codes instead of treating them as the final status.
- delayedStatusWriter.Flush sends its delayed status before flushing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ppers

- Restore DefaultHTTPErrorHandler's doc comment, which the helper had
  displaced.
- Keep Content-Range, which a 416 error response carries.
- supportsFlush now follows Unwrap to the innermost writer, so a
  wrapper that defines Flush (such as delayedStatusWriter over
  Response) no longer commits before a flush that will panic. The check
  runs only before the first commit.
- Close the test transport's idle connections.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ookup

- Error handlers now clear only Content-Encoding, Content-Length and
  Content-Disposition. Content-Type and validators such as ETag can be
  meant for the error response (a vendor media type, a 304), so they
  are kept as before.
- supportsFlush follows the same lookup as http.ResponseController and
  only looks through *Response, whose Flush just delegates. A wrapper
  that flushes itself is no longer rejected because the writer it wraps
  cannot flush.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@vishr
vishr merged commit fb1d59d into master Nov 20, 2026
11 checks passed
@vishr
vishr deleted the fix/response-commit-and-error-headers branch November 20, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Response.WriteHeader treats 1xx informational status codes as the final status Response.Flush does not mark the response as committed

1 participant