Skip to content

Fix data races and a deadlock in the byte stream helpers - #14085

Merged
mxpv merged 1 commit into
containerd:mainfrom
milantracy:fix-streaming-races
Sep 2, 2026
Merged

mxpv merged 1 commit into
containerd:mainfrom
milantracy:fix-streaming-races

Conversation

@milantracy

@milantracy milantracy commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

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.

Avoid hangs and data races when streaming container standard I/O in CRI

Copilot AI 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.

🟡 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.window atomic 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.Send and clientStream.Close to 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.

Comment thread core/transfer/streaming/reader.go Resolved
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]>
Copilot AI review requested due to automatic review settings September 2, 2026 05:54

Copilot AI 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.

🟢 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

Comment on lines +49 to +61
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
}
Comment on lines +29 to +32
rs, ws := pipeStream()
w := WriteByteStream(t.Context(), ws)
r := ReadByteStream(t.Context(), rs)

@github-project-automation github-project-automation Bot moved this from Needs Triage to Review In Progress in Pull Request Review Sep 2, 2026
@mxpv
mxpv added this pull request to the merge queue Sep 2, 2026
Merged via the queue into containerd:main with commit fb6dc53 Sep 2, 2026
55 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Sep 2, 2026
@samuelkarp

Copy link
Copy Markdown
Member

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

@mxpv

mxpv commented Sep 3, 2026

Copy link
Copy Markdown
Member

/cherry-pick release/2.3

@k8s-infra-cherrypick-robot

Copy link
Copy Markdown

@mxpv: new pull request created: #14094

Details

In response to this:

/cherry-pick release/2.3

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.

@mxpv

mxpv commented Sep 3, 2026

Copy link
Copy Markdown
Member

@samuelkarp I haven't seen anyone reporting this, but I think it worth having this in LTS, let's cherry-pick.

@fuweid fuweid added the cherry-picked/2.3.x PR commits are cherry picked into release/2.3 branch label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Runtime cherry-picked/2.3.x PR commits are cherry picked into release/2.3 branch size/L

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants