Skip to content

json: avoid duplicate response after write failures - #1100

Merged
keegancsmith merged 1 commit into
mainfrom
k/fix-json-writeheader
Jul 28, 2026
Merged

json: avoid duplicate response after write failures#1100
keegancsmith merged 1 commit into
mainfrom
k/fix-json-writeheader

Conversation

@keegancsmith

@keegancsmith keegancsmith commented Jul 28, 2026

Copy link
Copy Markdown
Member

The JSON search and list handlers currently treat every encoder error as though an HTTP 500 can still be sent. In practice, the response types are known to serialize successfully, while transport failures occur after ResponseWriter.Write has already committed HTTP 200. Calling jsonError at that point cannot change the response and causes the http: superfluous response.WriteHeader call warning reported in #1099.

This routes both successful response types through a small helper that encodes directly to the response writer and intentionally ignores encoder errors. Direct encoding retains the encoder’s pooled buffer behavior and avoids the response-sized copy that pre-marshalling would require. The existing application/json header is set rather than appended.

Fixes #1099

JSON response writes may fail only after the successful status has already been committed. Treat the controlled response types as serializable and avoid attempting a second error response, which cannot change the status and produces a superfluous WriteHeader warning.

Amp-Thread-ID: https://ampcode.com/threads/T-019fa815-b759-72ab-9771-e4008fbbf378
@keegancsmith keegancsmith changed the title fix/json: avoid duplicate response after write failures json: avoid duplicate response after write failures Jul 28, 2026
@keegancsmith
keegancsmith marked this pull request as ready for review July 28, 2026 10:48
@keegancsmith
keegancsmith merged commit 09ad25a into main Jul 28, 2026
7 checks passed
@keegancsmith
keegancsmith deleted the k/fix-json-writeheader branch July 28, 2026 11:39
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.

zoekt-webserver logs "superfluous response.WriteHeader call" from internal/json.jsonError (json.go:107)

2 participants