Repository navigation
app/vmauth: fix data race when retrying requests on failed backends (#11508) - #11552
Closed
justonemorenight wants to merge 1 commit into
Closed
justonemorenight wants to merge 1 commit into
justonemorenight wants to merge 1 commit into
Conversation
Closed
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 3 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
justonemorenight
force-pushed
the
fix-vmauth-data-race-11508
branch
from
September 11, 2026 13:10
97a11a1 to
dc6035b
Compare
…ictoriaMetrics#11508) When a backend returns an error or a retryable status code (such as 503) early before consuming the request body, net/http's transport background writeLoop may still be writing or closing the request body when RoundTrip returns. Previously, req.Body and r.Body shared the same *bufferedBody instance without synchronization, causing a data race between net/http's Body.Close() and httpserver.GetRequestURI(r) / resetReader() / subsequent retries. This commit fixes the data race by: 1. Providing an isolated bytes.Reader for each backend request attempt and for r.Body when canRetry is true. 2. Setting r.Body to http.NoBody when canRetry is false, since unbuffered streaming bodies cannot be retried or re-read by GetRequestURI. 3. Adding sync.Mutex to bufferedBody for safe concurrent access. 4. Adding regression test TestBufferedBody_DataRace verifying race-free behavior under concurrent writes and early 503 responses.
justonemorenight
force-pushed
the
fix-vmauth-data-race-11508
branch
from
September 18, 2026 03:08
dc6035b to
11f3f9e
Compare
Member
|
@justonemorenight, thank you for your effort, but this time we'd like to address the issue ourselves. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #11508
When an upstream backend returns an error or a retryable status code (such as 503) early before consuming the request body,
net/http's transport backgroundwriteLoopmay still be writing or closing the request body whenRoundTripreturns.Previously,
req.Bodyandr.Bodyshared the same*bufferedBodyinstance without synchronization. WhenRoundTripreturned early:httpserver.GetRequestURI(r)calledr.ParseForm(), which readr.Body(bb.Read()).tryProcessingRequestcalledbb.resetReader().bbto another backend.Meanwhile, the first backend's
writeLoopconcurrently calledbb.Close()(and was potentially still readingbb), causing a data race detected by Go's race detector.Solution
canRetryis true (the entire request body was buffered intobb.buf), provide an isolatedio.NopCloser(bytes.NewReader(bb.buf))for each backend request attempt as well as forr.Body. This ensures concurrent reads/closes across attempts andGetRequestURI(r)never race on shared reader offsets.canRetryis false (unbuffered streaming request body), setr.Body = http.NoBodyafter handing it off toRoundTrip, since unbuffered streams cannot be retried or re-read byGetRequestURI.sync.MutextobufferedBodyfor safe access tobufOffset,cannotRetry, andr.TestBufferedBody_DataRaceverifying race-free behavior under concurrent writes and early 503 responses for both retryable and unbuffered payloads.CHANGELOG.md.Testing
go test -race ./app/vmauth/...passed cleanly without any data races.go vet ./app/vmauth/...passed.