Repository navigation
Fix data races and a deadlock in the byte stream helpers - #14085
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
ReadByteStream now forwards non-EOF Send errors from the window-update goroutine (e.g., io.ErrClosedPipe) to Read, which can produce spurious read failures during normal shutdown in the in-repo in-memory stream implementation.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR hardens the byte-stream helpers used by core/transfer/streaming and the streaming proxy client by addressing concurrency hazards (data races) and a flow-control deadlock scenario in the window-update logic.
Changes:
- Make
readByteStream.windowatomic and adjust window accounting to be based on bytes received (not just bytes copied to the caller) to avoid a flow-control deadlock on short reads. - Serialize
clientStream.SendandclientStream.Closeto avoid racing on ttrpc’s unsynchronized internal state. - Add race-focused unit tests covering both fixes.
File summaries
| File | Description |
|---|---|
| core/transfer/streaming/reader.go | Uses an atomic window counter and updates flow-control logic to prevent races/deadlock. |
| core/transfer/streaming/reader_test.go | Adds a regression test that would race under -race without the atomic window fix. |
| core/streaming/proxy/streaming.go | Adds a mutex to serialize Send/Close to prevent ttrpc stream state races. |
| core/streaming/proxy/streaming_test.go | Adds a regression test that would race under -race without Send/Close serialization. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Race: clientStream.Send and Close both reach ttrpc's unsynchronized localClosed flag. readByteStream.window is written by the window goroutine and by Read, so make it atomic. Deadlock: Read the window by bytes copied instead of bytes received, so a short read leaves the window above windowSize. Signed-off-by: Jing Chen <[email protected]>
22d5f2d to
3914a44
Compare
There was a problem hiding this comment.
🟢 Approval recommended
The concurrency fixes are localized and validated by new regression tests, with only minor test robustness nits noted.
Review details
Suppressed comments (1)
core/transfer/streaming/reader_test.go:36
- The writer goroutine ignores errors from io.Copy and Close, which can cause the test to pass (or hang) while the writer actually failed.
go func() {
io.Copy(w, bytes.NewReader(expected))
w.Close()
}()
- Files reviewed: 4/4 changed files
- Comments generated: 2
- Review effort level: Lite
| done := make(chan struct{}) | ||
| go func() { | ||
| defer close(done) | ||
| for range 100 { | ||
| cs.Send(a) | ||
| } | ||
| }() | ||
|
|
||
| if err := cs.Close(); err != nil { | ||
| t.Fatalf("Close: %v", err) | ||
| } | ||
| <-done | ||
| } |
| rs, ws := pipeStream() | ||
| w := WriteByteStream(t.Context(), ws) | ||
| r := ReadByteStream(t.Context(), rs) | ||
|
|
|
@mxpv should this be cherry-picked? I'm working on releases, if this should be included it'd be good to cherry-pick it first. |
|
/cherry-pick release/2.3 |
|
@mxpv: new pull request created: #14094 DetailsIn response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
@samuelkarp I haven't seen anyone reporting this, but I think it worth having this in LTS, let's cherry-pick. |
Race: clientStream.Send and Close both reach ttrpc's unsynchronized localClosed flag. readByteStream.window is written by the window goroutine and by Read, so make it atomic.
Deadlock: Read the window by bytes copied instead of bytes received, so a short read leaves the window above windowSize.