Skip to content

Add warning when writing response body multiple times (fixes #4477) - #4483

Open
raju-mechatronics wants to merge 8 commits into
gin-gonic:masterfrom
raju-mechatronics:master
Open

raju-mechatronics wants to merge 8 commits into
gin-gonic:masterfrom
raju-mechatronics:master

Conversation

@raju-mechatronics

Copy link
Copy Markdown
Contributor

Add warning for multiple response writes

Fixes #4477

Description

Adds a warning when the response body is written multiple times in the same request. This prevents silent concatenation of multiple JSON/XML responses, which often produces invalid output.

Changes

  • Added warning in Context.Render() when response body already written
  • Consistent with existing header write warning behavior
  • Non-breaking change (debug mode only)

Example

router.GET("/example", func(c *gin.Context) {
    c.JSON(200, gin.H{"first": "response"})
    c.JSON(200, gin.H{"second": "response"})
})

Output:

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

Checklist

  • Open your pull request against the master branch.
  • All tests pass in available continuous integration systems (e.g., GitHub Actions).
  • Tests are added or modified as needed to cover code changes.
  • If the pull request introduces a new feature, the feature is documented in the docs/doc.md.

@codecov

codecov Bot commented Dec 28, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.65%. Comparing base (0f09c3a) to head (78e39e7).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4483   +/-   ##
=======================================
  Coverage   98.65%   98.65%           
=======================================
  Files          48       48           
  Lines        3188     3192    +4     
=======================================
+ Hits         3145     3149    +4     
  Misses         43       43           
Flag Coverage Δ
--ldflags="-checklinkname=0" -tags sonic 98.64% <100.00%> (+<0.01%) ⬆️
-tags go_json 98.57% <100.00%> (+<0.01%) ⬆️
-tags nomsgpack 98.63% <100.00%> (+<0.01%) ⬆️
go-1.26 98.33% <100.00%> (+<0.01%) ⬆️
go-1.27 98.67% <100.00%> (+<0.01%) ⬆️
macos-latest 98.65% <100.00%> (+<0.01%) ⬆️
ubuntu-latest 98.65% <100.00%> (+<0.01%) ⬆️

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

☔ View full report in Codecov by Harness.
📢 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.

@raju-mechatronics

Copy link
Copy Markdown
Contributor Author

@appleboy can you please take a look at this pr

@Nurysso

Nurysso commented Jan 1, 2026

Copy link
Copy Markdown
Contributor

Hey nice addition but when the warning fires the code still continues to render, which means the response could still gets corrupted, right? Shouldn't we return after the warning to actually prevent the invalid output?

@raju-mechatronics

Copy link
Copy Markdown
Contributor Author

Hey nice addition but when the warning fires the code still continues to render, which means the response could still gets corrupted, right? Shouldn't we return after the warning to actually prevent the invalid output?

I was waiting for reviewer response on this but it's for backward compatibility. Watch #4477 for details. It's seems multiple response is allowed in gin.

@appleboy appleboy added this to the v1.12 milestone Jan 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a debug-mode warning when a handler attempts to write a response body more than once during the same request, addressing silent concatenation (e.g., multiple JSON/XML writes) that can produce invalid output.

Changes:

  • Add a debug warning in Context.Render() when a second render occurs after the response has already been written.
  • Exempt code == -1 (used for SSE/stream-like cases) and sse.Event from the warning.
  • Add tests to validate the warning for multiple JSON writes and ensure no warning for repeated SSE writes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.

File Description
context.go Emits a debug warning when rendering after the response is already written, with special-casing for SSE/-1 code.
context_test.go Adds tests covering the new warning behavior for JSON and SSE.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread context.go Resolved
Comment thread context.go Resolved
Comment thread context_test.go Resolved
Comment thread context_test.go Resolved
Comment thread context_test.go Resolved
Comment thread context_test.go Resolved
@appleboy

Copy link
Copy Markdown
Member

move to the next milestone

@appleboy appleboy modified the milestones: v1.12, v1.x, v1.13 Feb 28, 2026
Comment thread context.go Fixed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add warning for multiple response writes

5 participants