Skip to content

fix(server): avoid superfluous response.WriteHeader call in EncodeJSONResponse - #543

Open
Kubudak90 wants to merge 1 commit into
coinbase:masterfrom
Kubudak90:fix/encode-json-response-superfluous-writeheader
Open

fix(server): avoid superfluous response.WriteHeader call in EncodeJSONResponse#543
Kubudak90 wants to merge 1 commit into
coinbase:masterfrom
Kubudak90:fix/encode-json-response-superfluous-writeheader

Conversation

@Kubudak90

Copy link
Copy Markdown

Fixes #407

EncodeJSONResponse was calling w.WriteHeader(status) before json.NewEncoder(w).Encode(i). If encoding failed after partially writing to the response, http.Error would attempt a second WriteHeader, producing the "http: superfluous response.WriteHeader call" log message seen in high-concurrency environments.

The fix encodes to a bytes.Buffer first, writes the header only on success, and returns a clean 500 when encoding fails.

Changes:

  • Encode JSON to an intermediate buffer before writing headers
  • Add unit tests for success, custom status, and encode-error cases

…NResponse

EncodeJSONResponse was calling w.WriteHeader(status) before
json.NewEncoder(w).Encode(i). If encoding failed after partially writing
to the response, http.Error would attempt a second WriteHeader, producing
the "http: superfluous response.WriteHeader call" log message seen in
high-concurrency environments.

The fix encodes to a bytes.Buffer first, writes the header only on success,
and returns a clean 500 when encoding fails.

Changes:
- Encode JSON to an intermediate buffer before writing headers
- Add unit tests for success, custom status, and encode-error cases

Fixes coinbase#407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant