Skip to content

🐛 bug: close BodyStream in adaptor FiberHandler streaming path#4267

Merged
ReneWerner87 merged 1 commit into
mainfrom
fix-bodystream-closing-in-adaptor
May 10, 2026
Merged

🐛 bug: close BodyStream in adaptor FiberHandler streaming path#4267
ReneWerner87 merged 1 commit into
mainfrom
fix-bodystream-closing-in-adaptor

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented May 10, 2026

Motivation

  • The adaptor streaming path forwarded fasthttp BodyStream() data to a net/http ResponseWriter but did not close the underlying fasthttp body stream, which can leak resources and enable DoS via repeated disconnects.

Description

  • Ensure fctx.Response.CloseBodyStream() is always called when the streaming branch is taken by adding a deferred close in middleware/adaptor/adaptor.go so streams are released on EOF, read errors, or downstream write errors.
  • Add regression tests in middleware/adaptor/adaptor_test.go that assert streamed io.ReadCloser instances are closed after normal completion and after a simulated downstream write error using a close-tracking reader and a failing ResponseWriter.
  • Files changed: middleware/adaptor/adaptor.go and middleware/adaptor/adaptor_test.go.

@gaby gaby requested a review from a team as a code owner May 10, 2026 01:10
Copilot AI review requested due to automatic review settings May 10, 2026 01:10
@gaby gaby requested review from ReneWerner87, efectn and sixcolors May 10, 2026 01:10
@gaby gaby added the aardvark label May 10, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 10, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ed196ccd-b1aa-454f-9e3e-6a04c4d9a4a0

📥 Commits

Reviewing files that changed from the base of the PR and between 30b1caa and 2a95bc0.

📒 Files selected for processing (2)
  • middleware/adaptor/adaptor.go
  • middleware/adaptor/adaptor_test.go

Walkthrough

This PR adds resource cleanup for HTTP response body streaming in the fasthttp adaptor middleware. A deferred call now closes the response body stream after request handling completes, preventing stream leaks. Two new tests verify closure in both successful send and write-failure paths.

Changes

Stream Body Closure

Layer / File(s) Summary
Stream Cleanup Implementation
middleware/adaptor/adaptor.go
Deferred CloseBodyStream() call added to release fasthttp response body stream resource after request completion.
Stream Closure Tests
middleware/adaptor/adaptor_test.go
New sync/atomic import, two test functions (Test_FiberHandler_ClosesBodyStream and Test_FiberHandler_ClosesBodyStreamOnWriteError), and helper types (closeTrackingReader and failingStreamWriter) validate stream closure on success and error paths.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • gofiber/fiber#3799: Both PRs modify middleware/adaptor/adaptor.go for BodyStream support; the retrieved PR implements streaming/flushing while this PR ensures proper stream closure and adds test coverage.

Suggested labels

☢️ Bug, v3

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

🐰 A stream once flowed without a care,
Until the rabbit found a leak in there!
With defer close, the resource gleams,
And tests now guard those fleeting streams. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 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 (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title clearly summarizes the main change: ensuring BodyStream is properly closed in the adaptor's FiberHandler streaming path, addressing the resource leak bug described in the PR objectives.
Description check ✅ Passed The pull request description covers the motivation, describes the specific changes made, identifies the files modified, and includes testing performed. It addresses the core requirements of the template sufficiently.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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-bodystream-closing-in-adaptor

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.1)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


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.

@ReneWerner87 ReneWerner87 added this to v3 May 10, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 10, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 fixes a resource leak in the middleware/adaptor net/http adapter by ensuring that fasthttp response body streams are always closed when the adapter takes the streaming copy path, preventing leaked stream resources on EOF or downstream write failures.

Changes:

  • Add a deferred fctx.Response.CloseBodyStream() in the FiberHandler streaming branch so the underlying body stream is released on all exit paths.
  • Add regression tests to assert streamed io.ReadCloser instances are closed both on normal completion and when the downstream ResponseWriter returns a write error.

Reviewed changes

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

File Description
middleware/adaptor/adaptor.go Adds a deferred body-stream close in the streaming response forwarding path.
middleware/adaptor/adaptor_test.go Adds regression tests to verify body-stream closure on success and on write failure.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.27%. Comparing base (6bf2f17) to head (2a95bc0).
⚠️ Report is 29 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4267      +/-   ##
==========================================
+ Coverage   91.18%   91.27%   +0.08%     
==========================================
  Files         129      130       +1     
  Lines       12757    12756       -1     
==========================================
+ Hits        11633    11643      +10     
+ Misses        709      701       -8     
+ Partials      415      412       -3     
Flag Coverage Δ
unittests 91.27% <100.00%> (+0.08%) ⬆️

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request ensures that the response body stream is closed in the adaptor middleware by adding a deferred call to CloseBodyStream. It also includes new unit tests to verify that the stream is correctly closed during both successful operations and write errors. I have no feedback to provide.

@ReneWerner87 ReneWerner87 merged commit 0e5a233 into main May 10, 2026
31 checks passed
@ReneWerner87 ReneWerner87 deleted the fix-bodystream-closing-in-adaptor branch May 10, 2026 12:11
@github-project-automation github-project-automation Bot moved this to Done in v3 May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants