-
-
Notifications
You must be signed in to change notification settings - Fork 150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: race by deferring the return of buf to sync.Pool when using RawBody #542
Conversation
WalkthroughThe changes enhance resource management and testing capabilities in the codebase. The Changes
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- huma.go (1 hunks)
Additional comments not posted (1)
huma.go (1)
1295-1296
: Use ofdefer
for buffer management is appropriate.The changes to use
defer
forbufPool.Put(buf)
andbuf.Reset()
ensure that the buffer is properly reset and returned to the pool, regardless of how the function exits. This improves resource management and reduces the risk of resource leaks.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #542 +/- ##
=======================================
Coverage 92.74% 92.75%
=======================================
Files 22 22
Lines 3872 3876 +4
=======================================
+ Hits 3591 3595 +4
Misses 236 236
Partials 45 45 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (2)
- huma.go (1 hunks)
- huma_test.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
- huma.go
Additional comments not posted (1)
huma_test.go (1)
2163-2185
: LGTM! Verify the correctness ofRawBody
modification.The test setup for detecting race conditions looks good. Ensure that modifying
RawBody
is intentional and does not affect other tests or production code.Consider verifying that the
RawBody
modification is safe and does not introduce unintended side effects.Verification successful
Modification of
RawBody
is safe and confined to tests.The modification of
RawBody
in theTestBodyRace
function is isolated to test scenarios and does not affect production code. This approach effectively tests for race conditions without introducing side effects.
- The
RawBody
field is primarily used in test files (huma_test.go
).- The production code (
huma.go
) handlesRawBody
without modifications leading to race conditions.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify the usage of `RawBody` in the codebase to ensure safe modification. # Test: Search for `RawBody` usage. Expect: Only test-related modifications. rg --type go -A 5 $'RawBody'Length of output: 7860
@nunoo thank you for the PR! I added a commit with a test to trigger the issue, and slightly modified your fix to only apply when |
@danielgtaylor thanks for the quick review and additional context to the changes you made! I'm excited to have contributed to the code base! |
You are still using the result of buf.Bytes() in body, later in this function. This defers until completion. Alternatively we can make body a copy of the bytes from the buffer.
https://www.captaincodeman.com/golang-buffer-pool-gotcha#the-gotcha---were-still-using-it
Summary by CodeRabbit
Chores
Tests