Skip to content

fix: close Reader on File::Reset#3784

Closed
arturmelanchyk wants to merge 3 commits into
gofiber:mainfrom
arturmelanchyk:reader-close
Closed

fix: close Reader on File::Reset#3784
arturmelanchyk wants to merge 3 commits into
gofiber:mainfrom
arturmelanchyk:reader-close

Conversation

@arturmelanchyk

@arturmelanchyk arturmelanchyk commented Oct 5, 2025

Copy link
Copy Markdown
Contributor

Description

Fix to close file descriptors on Request::Reset()

Currently file can be opened here and never closed

fiber/client/hooks.go

Lines 288 to 294 in a33bab1

// If reader is not set, open the file.
if v.reader == nil {
v.reader, err = os.Open(v.path)
if err != nil {
return fmt.Errorf("open file error: %w", err)
}
}

Changes introduced

List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.

  • Benchmarks: Describe any performance benchmarks and improvements related to the changes.
  • Documentation Update: Detail the updates made to the documentation and links to the changed files.
  • Changelog/What's New: Include a summary of the additions for the upcoming release notes.
  • Migration Guide: If necessary, provide a guide or steps for users to migrate their existing code to accommodate these changes.
  • API Alignment with Express: Explain how the changes align with the Express API.
  • API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.
  • Examples: Provide examples demonstrating the new features or changes in action.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Performance improvement (non-breaking change which improves efficiency)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Commit formatting

Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md

Signed-off-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Oct 5, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Adds a safeguard in File.Reset() to close an existing non-nil reader before setting it to nil.

Changes

Cohort / File(s) Summary
Request file lifecycle
client/request.go
Update File.Reset(): if f.reader != nil, call reader.Close() then set f.reader = nil; previously only set to nil.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant File
  participant Reader

  Caller->>File: Reset()
  alt reader is non-nil
    File->>Reader: Close()
    Reader-->>File: close result
    File->>File: set reader = nil
  else reader is nil
    File->>File: no-op
  end
  File-->>Caller: return
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A bunny taps the Reset key,
Closes streams before they flee—
No leaks now, tidy as can be,
Burrows clear, memory free.
Thump-thump! Another hop of glee.

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Description Check ⚠️ Warning The description retains the template prompts without filling in the “Fixes #” issue reference or detailing the specific changes in the “Changes introduced” section, so it does not meet the required template structure and lacks concrete information about the PR’s impact. Please add the missing “Fixes #” line if relevant, provide a concrete list of changes under “Changes introduced,” and complete or remove unused checklist items so the description clearly documents the modifications and their benefits.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title succinctly and accurately summarizes the primary change of ensuring the reader is closed during File::Reset, matching the modifications in client/request.go.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

@codecov

codecov Bot commented Oct 5, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.68%. Comparing base (e8f639a) to head (04f7fbe).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3784   +/-   ##
=======================================
  Coverage   91.67%   91.68%           
=======================================
  Files         113      113           
  Lines       11959    11962    +3     
=======================================
+ Hits        10964    10967    +3     
  Misses        731      731           
  Partials      264      264           
Flag Coverage Δ
unittests 91.68% <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 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.

arturmelanchyk and others added 2 commits October 5, 2025 17:08
Signed-off-by: Artur Melanchyk <13834276+arturmelanchyk@users.noreply.github.com>
@arturmelanchyk
arturmelanchyk marked this pull request as ready for review October 5, 2025 15:26
@arturmelanchyk
arturmelanchyk requested a review from a team as a code owner October 5, 2025 15:26

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
client/request.go (1)

957-965: Update SetReader doc comment. Replace the comment on line 951 to note that the reader is closed in both the parserBody hook and in Reset.

🧹 Nitpick comments (1)
client/request.go (1)

951-951: Update comment to reflect that Reset() also closes the reader.

The comment states the reader "will be closed in the parserBody hook," but with the changes in this PR, Reset() also closes the reader. Consider updating this comment to accurately reflect both cleanup paths.

Apply this diff to update the comment:

-// SetReader sets the file's reader, which will be closed in the parserBody hook.
+// SetReader sets the file's reader, which will be closed in the parserBody hook or during Reset().
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e8f639a and 04f7fbe.

📒 Files selected for processing (1)
  • client/request.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

**/*.go: Format Go code using gofumpt (enforced via make format)
Ensure code passes golangci-lint checks (enforced via make lint)
Optimize struct field alignment using betteralign (enforced via make betteralign)
Modernize Go code using gopls modernize (enforced via make modernize)

Files:

  • client/request.go

@arturmelanchyk

Copy link
Copy Markdown
Contributor Author

I think benchmark result is not relevant in this case

@arturmelanchyk
arturmelanchyk marked this pull request as draft October 5, 2025 15:45
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.

1 participant