Skip to content

feat: add warning for multiple response writes#4606

Open
xingzihai wants to merge 1 commit intogin-gonic:masterfrom
xingzihai:feat-multi-write-warning
Open

feat: add warning for multiple response writes#4606
xingzihai wants to merge 1 commit intogin-gonic:masterfrom
xingzihai:feat-multi-write-warning

Conversation

@xingzihai
Copy link
Copy Markdown

Summary

Adds a warning when attempting to write a response body multiple times. This helps developers debug issues where response bodies are written more than once.

Problem

When calling response write methods multiple times (e.g., c.Render() followed by another write), the behavior can be confusing. Developers may not realize they're overwriting a previous response.

Solution

  • Added a check in Context.Render() to detect if response body was already written
  • When detected, prints a warning using debugPrint in debug mode
  • No behavior change in release mode

Changes

  • context.go (+4 lines) - Added multiple write detection
  • context_test.go (+44 lines) - Added tests for debug and release modes

Testing

  • Added TestRenderMultipleWritesWarning - verifies warning in debug mode
  • Added TestRenderMultipleWritesNoWarningInReleaseMode - verifies no warning in release mode

Fixes #4477

When the response body is written multiple times in a single request,
Gin now prints a warning in debug mode. This helps developers identify
a common mistake where middleware or handlers accidentally write
responses more than once, resulting in invalid concatenated output.

The warning is printed via debugPrint() and only appears in debug mode,
making this change non-breaking and backwards compatible.

Fixes gin-gonic#4477

Example warning output:
[GIN-debug] [WARNING] Response body already written. Attempting to write again with status code 200

Changes:
- Added check in Context.Render() to detect multiple writes
- Added unit tests to verify warning behavior in debug mode
- Added test to verify no warning in release mode
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.43%. Comparing base (3dc1cd6) to head (ec2c838).
⚠️ Report is 275 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4606      +/-   ##
==========================================
- Coverage   99.21%   98.43%   -0.78%     
==========================================
  Files          42       48       +6     
  Lines        3182     3140      -42     
==========================================
- Hits         3157     3091      -66     
- Misses         17       40      +23     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.42% <100.00%> (?)
-tags go_json 98.29% <100.00%> (?)
-tags nomsgpack 98.35% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.43% <100.00%> (?)
go-1.26 98.37% <100.00%> (?)
macos-latest 98.37% <100.00%> (-0.84%) ⬇️
ubuntu-latest 98.43% <100.00%> (-0.78%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Add warning for multiple response writes

1 participant