Skip to content

fix(buffered_reader): Fixing a memory leak in buffered reader#4638

Merged
abhishek10004 merged 4 commits into
masterfrom
abhishek/buffered_reader_fix
Apr 21, 2026
Merged

fix(buffered_reader): Fixing a memory leak in buffered reader#4638
abhishek10004 merged 4 commits into
masterfrom
abhishek/buffered_reader_fix

Conversation

@abhishek10004

@abhishek10004 abhishek10004 commented Apr 21, 2026

Copy link
Copy Markdown
Collaborator

Description

Fixes a memory leak in the buffered reader code during multi-block reads. Previously, if an initial block downloaded successfully but a later block failed (triggering a fallback), the reference counts on the successfully downloaded blocks were never decremented. This PR addresses the leak by catching gcsx.FallbackToAnotherReader errors and calling a new releaseInflightBlocks helper to immediately invoke callbacks and release references for any in-flight blocks.

Link to the issue in case of a bug fix.

b/503335735

Testing details

  1. Manual - Done
  2. Unit tests - Done
  3. Integration tests - Done

Any backward incompatible change? If so, please explain.

@abhishek10004 abhishek10004 requested a review from vipnydav April 21, 2026 09:16
@abhishek10004 abhishek10004 requested a review from a team as a code owner April 21, 2026 09:16
@abhishek10004 abhishek10004 requested a review from charith87 April 21, 2026 09:16
@abhishek10004 abhishek10004 added the execute-integration-tests Run only integration tests label Apr 21, 2026
@github-actions github-actions Bot added the remind-reviewers Auto remind reviewers in attention set for review post 24hrs of inactivity on PR. label Apr 21, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical memory leak within the buffered reader component. The leak occurred in scenarios where a multi-block read operation partially succeeded but then encountered an error, triggering a fallback mechanism. The fix ensures that any blocks successfully acquired before the fallback are promptly released, preventing resource accumulation and improving the stability of the reader.

Highlights

  • Memory Leak Fix: Addressed a memory leak in the buffered reader where reference counts for successfully downloaded blocks were not decremented if a subsequent block failed, leading to a fallback to another reader.
  • New Helper Function: Introduced a new helper function, releaseInflightBlocks, to immediately invoke callbacks and release references for any in-flight blocks when a fallback error occurs.
  • Error Handling Enhancement: Modified the ReadAt function to catch gcsx.FallbackToAnotherReader errors within its defer block, ensuring releaseInflightBlocks is called and the response is cleared before returning.
  • New Test Case: Added TestReadAtFallbackOnSecondBlockDownloadFailure to verify that the buffered reader correctly falls back and releases resources when a multi-block read fails after the first block.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors the ReadAt method in internal/bufferedread/buffered_reader.go to use named return parameters and a deferred function for populating the response and handling resource cleanup. It also introduces a releaseInflightBlocks helper to ensure blocks are released during fallback scenarios. A critical issue was identified in the deferred logic where memory leaks could occur on non-fallback errors because blocks are not released, and the gcsx.Reader interface is violated by returning partial data alongside errors.

Comment thread internal/bufferedread/buffered_reader.go
@codecov

codecov Bot commented Apr 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.65%. Comparing base (b426baf) to head (f475a66).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4638      +/-   ##
==========================================
+ Coverage   83.62%   83.65%   +0.02%     
==========================================
  Files         164      164              
  Lines       20278    20287       +9     
==========================================
+ Hits        16958    16971      +13     
+ Misses       2678     2676       -2     
+ Partials      642      640       -2     
Flag Coverage Δ
unittests 83.65% <100.00%> (+0.02%) ⬆️

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.

@charith87 charith87 removed their request for review April 21, 2026 09:59

@vadlakondaswetha vadlakondaswetha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

providing approval based on Vipins review

@vadlakondaswetha vadlakondaswetha left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

providing approval based on Vipins review

@abhishek10004 abhishek10004 merged commit 90d49c5 into master Apr 21, 2026
16 checks passed
@abhishek10004 abhishek10004 deleted the abhishek/buffered_reader_fix branch April 21, 2026 12:16
vadlakondaswetha pushed a commit that referenced this pull request Apr 23, 2026
Fixes a memory leak in the buffered reader code during multi-block reads. Previously, if an initial block downloaded successfully but a later block failed (triggering a fallback), the reference counts on the successfully downloaded blocks were never decremented. This PR addresses the leak by catching gcsx.FallbackToAnotherReader errors and calling a new releaseInflightBlocks helper to immediately invoke callbacks and release references for any in-flight blocks.
PranjalC100 pushed a commit that referenced this pull request Apr 24, 2026
Fixes a memory leak in the buffered reader code during multi-block reads. Previously, if an initial block downloaded successfully but a later block failed (triggering a fallback), the reference counts on the successfully downloaded blocks were never decremented. This PR addresses the leak by catching gcsx.FallbackToAnotherReader errors and calling a new releaseInflightBlocks helper to immediately invoke callbacks and release references for any in-flight blocks.
PranjalC100 added a commit that referenced this pull request Apr 29, 2026
* feat: making direct-path verification non-fatal until dummy-stat calls becomes reliable (#4628)

* feat: add skipDirectPathEnforcement parameter to createGRPCClientHandle to allow conditional DirectPath enforcement

* chore: increase directPathDetectionTimeout from 10 seconds to 5 minutes

* simplifying a bit

* making direct-path verification non-fatal

* removing the timeout from the client creation

* minor change

* removing empty line

* fix(buffered_reader): Fixing a memory leak in buffered reader (#4638)

Fixes a memory leak in the buffered reader code during multi-block reads. Previously, if an initial block downloaded successfully but a later block failed (triggering a fallback), the reference counts on the successfully downloaded blocks were never decremented. This PR addresses the leak by catching gcsx.FallbackToAnotherReader errors and calling a new releaseInflightBlocks helper to immediately invoke callbacks and release references for any in-flight blocks.

* fix(direct path verification): Updating direct path enforcement strategy (#4635)

Updating the direct path enforcement strategy:

- For zonal buckets, log direct path verification status but do not block client creation on it
- For non-zonal buckets, block grpc client creation on direct path verification status. In case of failure in detecting direct path status, fallback to http client would happen based on the set grpc-strategy

Also, updated the timeout to 15 seconds for direct path verification.

* feat(dp-check): removing older cp check utility (#4494)

* feat(dp-check): removing older cp check utility

* validation package is already disabled hence removing the skip

---------

Co-authored-by: Prince Kumar <princer@google.com>
Co-authored-by: Abhishek Gupta <abhishekmgupta@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

execute-integration-tests Run only integration tests remind-reviewers Auto remind reviewers in attention set for review post 24hrs of inactivity on PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants