Skip to content

fix(api): prevent panic on 204 No Content with Go 1.26#575

Merged
sylr merged 1 commit into
mainfrom
fix/otlp-middleware-go126-no-content-v4
Mar 20, 2026
Merged

fix(api): prevent panic on 204 No Content with Go 1.26#575
sylr merged 1 commit into
mainfrom
fix/otlp-middleware-go126-no-content-v4

Conversation

@sylr
Copy link
Copy Markdown
Contributor

@sylr sylr commented Mar 20, 2026

Summary

  • Fix responseWriter.finalize() in OTLP middleware to skip Write() when buffered data is empty, preventing panic with Go 1.26 which strictly enforces that 204 No Content responses must not have a body
  • Fix NoContent() to call WriteHeader(204) directly instead of routing through writeJSON() which unnecessarily sets Content-Type: application/json

Note: This fix is based on the v4.1.1 tag. The same files were moved to pkg/transport/ in v5 and need the same fix there.

Context

Go 1.26 now panics with http: request method or response status code does not allow body when any write (even empty) is attempted on a 204 response. This breaks formancehq/membership-api unit tests (PR #700) which upgrade to Go 1.26.

Test plan

  • Verified the panic reproduces in membership-api CI with Go 1.26
  • Run go-libs unit tests
  • Verify membership-api tests pass after bumping to the fixed version

🤖 Generated with Claude Code

@sylr sylr requested review from a team as code owners March 20, 2026 10:20
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 20, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 2b6715cf-f015-45ce-bc17-5c12997b3f2c

📥 Commits

Reviewing files that changed from the base of the PR and between 2844b0a and 5df80b3.

📒 Files selected for processing (2)
  • pkg/transport/api/utils.go
  • pkg/transport/httpserver/otlp_middleware.go

Walkthrough

Two HTTP transport utilities were refined: NoContent responses now write only status headers without JSON serialization, and the OTLP response writer's finalization method early-exits when data buffer is empty, preventing unnecessary zero-length writes.

Changes

Cohort / File(s) Summary
Response Handling Simplification
pkg/transport/api/utils.go
Refactored NoContent() to use w.WriteHeader() directly instead of delegating to writeJSON(), eliminating JSON serialization overhead for 204 responses.
Response Writer Optimization
pkg/transport/httpserver/otlp_middleware.go
Added early-exit condition in finalize() to skip writing when buffered data is empty, avoiding unnecessary zero-length writes to underlying response writer.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A NoContent hop, so swift and clean,
No JSON bloat in between,
Empty buffers skip their dance,
Headers fly with graceful glance,
HTTP responses, lean and keen! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main fix: preventing panic on 204 No Content responses with Go 1.26, which is the primary change across both modified files.
Description check ✅ Passed The description is directly related to the changeset, explaining the Go 1.26 panic issue, the two specific fixes made, and providing context about why these changes were necessary.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/otlp-middleware-go126-no-content-v4
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

gfyrag
gfyrag previously approved these changes Mar 20, 2026
Go 1.26 enforces that response status codes like 204 No Content
must not have a body. The OTLP middleware's finalize() method was
calling Write() even with empty data, causing a panic. Additionally,
NoContent() was routing through writeJSON() which unnecessarily set
Content-Type header on bodyless responses.

Constraint: Go 1.26 strictly enforces RFC 7230 no-body status codes
Rejected: Recover from panic in finalize | masks the root cause
Confidence: high
Scope-risk: narrow

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@sylr sylr force-pushed the fix/otlp-middleware-go126-no-content-v4 branch from 908dfdc to 5df80b3 Compare March 20, 2026 10:44
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 20, 2026

Codecov Report

❌ Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 25.82%. Comparing base (2844b0a) to head (5df80b3).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/transport/httpserver/otlp_middleware.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #575      +/-   ##
==========================================
+ Coverage   25.78%   25.82%   +0.03%     
==========================================
  Files         180      180              
  Lines        7065     7067       +2     
==========================================
+ Hits         1822     1825       +3     
+ Misses       5134     5132       -2     
- Partials      109      110       +1     

☔ 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.

@sylr sylr added this pull request to the merge queue Mar 20, 2026
Merged via the queue into main with commit 2c91705 Mar 20, 2026
7 checks passed
@sylr sylr deleted the fix/otlp-middleware-go126-no-content-v4 branch March 20, 2026 12:55
sylr added a commit that referenced this pull request Mar 20, 2026
Go 1.26 enforces that response status codes like 204 No Content
must not have a body. The OTLP middleware's finalize() method was
calling Write() even with empty data, causing a panic. Additionally,
NoContent() was routing through writeJSON() which unnecessarily set
Content-Type header on bodyless responses.

Constraint: Go 1.26 strictly enforces RFC 7230 no-body status codes
Rejected: Recover from panic in finalize | masks the root cause
Confidence: high
Scope-risk: narrow

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
sylr added a commit that referenced this pull request Mar 20, 2026
… (#576)

Go 1.26 enforces that response status codes like 204 No Content
must not have a body. The OTLP middleware's finalize() method was
calling Write() even with empty data, causing a panic. Additionally,
NoContent() was routing through writeJSON() which unnecessarily set
Content-Type header on bodyless responses.

Constraint: Go 1.26 strictly enforces RFC 7230 no-body status codes
Rejected: Recover from panic in finalize | masks the root cause
Confidence: high
Scope-risk: narrow

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

2 participants