Skip to content

mem: ensure Reader buffers are reused on early return#8886

Merged
arjan-bal merged 2 commits into
grpc:masterfrom
ash2k:always-close-reader
Feb 9, 2026
Merged

mem: ensure Reader buffers are reused on early return#8886
arjan-bal merged 2 commits into
grpc:masterfrom
ash2k:always-close-reader

Conversation

@ash2k

@ash2k ash2k commented Feb 7, 2026

Copy link
Copy Markdown
Contributor

If there was an error, e.g. while decompressing the data, we still want to return all buffers to the pool.

RELEASE NOTES: None

@codecov

codecov Bot commented Feb 7, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 83.14%. Comparing base (0f69b6e) to head (004f71c).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
rpc_util.go 88.88% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8886      +/-   ##
==========================================
- Coverage   83.31%   83.14%   -0.17%     
==========================================
  Files         414      414              
  Lines       32805    32821      +16     
==========================================
- Hits        27330    27288      -42     
- Misses       4069     4099      +30     
- Partials     1406     1434      +28     
Files with missing lines Coverage Δ
rpc_util.go 76.62% <88.88%> (-6.31%) ⬇️

... and 29 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@eshitachandwani eshitachandwani self-requested a review February 9, 2026 09:33
@eshitachandwani eshitachandwani self-assigned this Feb 9, 2026

@eshitachandwani eshitachandwani left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with minor comments. Adding @arjan-bal as a second reviewer.

Comment thread rpc_util.go Outdated
if dc != nil {
uncompressed, err := dc.Do(d.Reader())
r := d.Reader()
defer r.Close() // ensure buffers are reused on early return

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you remove the on early return part?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed. Thanks for review!

Comment thread rpc_util.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since you are already here , do you mind wrapping this comment to make sure it is limited to 80 cols?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@eshitachandwani eshitachandwani added this to the 1.80 Release milestone Feb 9, 2026
@eshitachandwani eshitachandwani added Type: Performance Performance improvements (CPU, network, memory, etc) Type: Internal Cleanup Refactors, etc labels Feb 9, 2026
@eshitachandwani eshitachandwani removed the Type: Performance Performance improvements (CPU, network, memory, etc) label Feb 9, 2026
@ash2k ash2k force-pushed the always-close-reader branch from 38a1412 to f3e854c Compare February 9, 2026 09:56
Comment thread rpc_util.go Outdated
if dc != nil {
uncompressed, err := dc.Do(d.Reader())
r := d.Reader()
defer r.Close() // ensure buffers are reused

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.

I don't think we need a defer here, we can call r.Close() after dc.Do either inside the if err != nil or even outside since the implementaiton of Close is idempotent. defer statements have a very small performance cost, so we should try to avoid them on the RPC path if possible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is even better since that way the buffers are freed ASAP. 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Well, no, those are just constructors of readers. Moved to each if. I think this is worse, more fragile. But I don't mind, up to you.

Comment thread rpc_util.go Outdated
@arjan-bal arjan-bal removed their assignment Feb 9, 2026
@ash2k ash2k force-pushed the always-close-reader branch from 6f21a70 to 015c771 Compare February 9, 2026 11:44
If there was an error, e.g. while decompressing the data, we still want to return all buffers to the pool.
@ash2k ash2k force-pushed the always-close-reader branch from 015c771 to 004f71c Compare February 9, 2026 12:17
@arjan-bal

Copy link
Copy Markdown
Contributor

/gemini review

@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 correctly identifies and fixes a potential buffer leak in the decompress function by ensuring that the mem.Reader is closed on error paths. This prevents buffers from being stranded if decompression fails midway.

I've suggested a small refactoring to use defer for closing the reader. This is a more idiomatic Go approach that simplifies the code and makes it more robust by guaranteeing the cleanup action is executed on all exit paths from the function blocks, not just the error paths.

@arjan-bal arjan-bal 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.

LGTM; ignoring Gemini's suggestion, using defer here is an intentional choice for performance.

@arjan-bal arjan-bal merged commit 7860144 into grpc:master Feb 9, 2026
14 checks passed
@ash2k ash2k deleted the always-close-reader branch February 9, 2026 22:59
mbissa pushed a commit to mbissa/grpc-go that referenced this pull request Feb 10, 2026
If there was an error, e.g. while decompressing the data, we still want
to return all buffers to the pool.

RELEASE NOTES: None
mbissa pushed a commit to mbissa/grpc-go that referenced this pull request Feb 16, 2026
If there was an error, e.g. while decompressing the data, we still want
to return all buffers to the pool.

RELEASE NOTES: None
Pranjali-2501 pushed a commit to Pranjali-2501/grpc-go that referenced this pull request Feb 23, 2026
If there was an error, e.g. while decompressing the data, we still want
to return all buffers to the pool.

RELEASE NOTES: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants