Skip to content

remotes: close fetch reader immediately on EOF - #13438

Merged
samuelkarp merged 2 commits into
containerd:mainfrom
samuelkarp:close-on-eof-reader
Jun 2, 2026
Merged

samuelkarp merged 2 commits into
containerd:mainfrom
samuelkarp:close-on-eof-reader

Conversation

@samuelkarp

@samuelkarp samuelkarp commented May 20, 2026 •

Copy link
Copy Markdown
Member

The CRI progress reporter cancels an image pull if it sees no progress
for 5 seconds. It tracks this through active HTTP requests. During
remote fetches, the HTTP response reader is closed via a deferred
call after content.Copy completes.

Diagnosis:
content.Copy handles both downloading the stream and committing
the writer to the content store. Any delays during the database
commit phase (e.g. from database locks, slow disk syncs, or concurrent
pull deduplication blocks) keep the HTTP connection open. The progress
reporter sees the request is still active (activeReqs = 1) but no new
bytes are coming in, leading to a premature timeout cancellation.

Reproduction:
We reproduced this flakiness deterministically on a GCE VM under a
simulated 2 Mbps ingress bandwidth limit using Linux traffic control
ingress policing (tc filter ... action police rate 2mbit). Under this
slowness, the download took longer than the progress timeout during the
slow commit phase, triggering context cancellation and failing the
TestCRIImagePullTimeout/HoldingContentOpenWriterWithLocalPull test.

Solution:
To fix this, we wrap the HTTP reader in a closeOnEOFReader or
closeOnEOFReadSeeker before handing it to content.Copy. If the
underlying connection reader implements io.Seeker, it is dynamically
wrapped in closeOnEOFReadSeeker to forward Seek operations. This
ensures that O(1) Range seeks are fully preserved during network
resumes or retries. The wrappers automatically close the underlying
network stream as soon as Read() returns io.EOF (when the download
completes, before the database commit begins). This drops activeReqs
to 0 early, freeing the socket and preventing progress timeouts
during commits. A sync.Once ensures that subsequent deferred
Close() calls do not double-decrement the reporter.

How it was tested:
Verified the fix on a GCE VM under a simulated 2 Mbps ingress
bandwidth limit. Verified seeker safety via standalone logic audits
and trace proofs.

Assisted-by: Antigravity

Copilot AI review requested due to automatic review settings May 20, 2026 04:18
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review May 20, 2026

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.

Pull request overview

This PR aims to prevent CRI image pulls from being prematurely canceled by the progress timeout when the network download has finished but content.Copy is still committing to the content store (e.g., due to slow/blocked DB commits). It does this by closing the remote fetch HTTP reader immediately upon hitting io.EOF, so the progress reporter no longer sees an “active” HTTP request during the commit phase.

Changes:

  • Wrap the fetcher-provided io.ReadCloser with a closeOnEOFReader in Fetch and defer closing the wrapper.
  • Add a closeOnEOFReader type that closes the underlying reader as soon as Read returns io.EOF, with sync.Once to avoid double-closing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/remotes/handlers.go Resolved
@samuelkarp
samuelkarp force-pushed the close-on-eof-reader branch from 953eebf to e3dbfd5 Compare May 20, 2026 06:45
@fuweid fuweid self-assigned this May 21, 2026
@samuelkarp

Copy link
Copy Markdown
Member Author

@fuweid pointed out that #13304 also attempts to address the same flake.

Copilot AI review requested due to automatic review settings June 2, 2026 19:53
@samuelkarp
samuelkarp force-pushed the close-on-eof-reader branch from e3dbfd5 to 86865d5 Compare June 2, 2026 19:53
@samuelkarp

Copy link
Copy Markdown
Member Author

Per offline comment from @dmcgowan, embedded closeOnEOFReader into closeOnEOFReadSeeker.

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

The CRI progress reporter cancels an image pull if it sees no progress
for 5 seconds. It tracks this through active HTTP requests. During
remote fetches, the HTTP response reader is closed via a deferred
call after `content.Copy` completes.

Diagnosis:
`content.Copy` handles both downloading the stream and committing
the writer to the content store. Any delays during the database
commit phase (e.g. from database locks, slow disk syncs, or concurrent
pull deduplication blocks) keep the HTTP connection open. The progress
reporter sees the request is still active (`activeReqs = 1`) but no new
bytes are coming in, leading to a premature timeout cancellation.

Reproduction:
We reproduced this flakiness deterministically on a GCE VM under a
simulated 2 Mbps ingress bandwidth limit using Linux traffic control
ingress policing (`tc filter ... action police rate 2mbit`). Under this
slowness, the download took longer than the progress timeout during the
slow commit phase, triggering context cancellation and failing the
`TestCRIImagePullTimeout/HoldingContentOpenWriterWithLocalPull` test.

Solution:
To fix this, we wrap the HTTP reader in a `closeOnEOFReader` or
`closeOnEOFReadSeeker` before handing it to `content.Copy`. If the
underlying connection reader implements `io.Seeker`, it is dynamically
wrapped in `closeOnEOFReadSeeker` to forward `Seek` operations. This
ensures that O(1) Range seeks are fully preserved during network
resumes or retries. The wrappers automatically close the underlying
network stream as soon as `Read()` returns `io.EOF` (when the download
completes, before the database commit begins). This drops `activeReqs`
to `0` early, freeing the socket and preventing progress timeouts
during commits. A `sync.Once` ensures that subsequent deferred
`Close()` calls do not double-decrement the reporter.

How it was tested:
Verified the fix on a GCE VM under a simulated 2 Mbps ingress
bandwidth limit. Verified seeker safety via standalone logic audits
and trace proofs.

Assisted-by: Antigravity
Signed-off-by: Samuel Karp <[email protected]>
The TestCRIImagePullTimeout test case "NoDataTransferred" flaked under
constrained networks because the test proxy mirror registry used a
blocking ReadAtLeast call to forward bytes to containerd.

This blocking wait (up to 4KB) meant the mirror registry server
completely stopped forwarding data during network slowness, triggering
containerd's aggressive 5-second progress timeout and canceling the
pull before it could reach its 3MB circuit-breaker limit.

This is resolved by changing the proxy's custom copy loop from
io.ReadAtLeast(src, buf, len(buf)) to standard src.Read(buf). This
streams network chunks to containerd immediately as they arrive,
preventing false timeout cancellations while maintaining correct
circuit-breaker byte tracking.

Assisted-by: Antigravity
Signed-off-by: Samuel Karp <[email protected]>
@samuelkarp
samuelkarp force-pushed the close-on-eof-reader branch from 86865d5 to 45cc0c5 Compare June 2, 2026 21:53
@fuweid

fuweid commented Jun 2, 2026

Copy link
Copy Markdown
Member

@samuelkarp

Copy link
Copy Markdown
Member Author

samuelkarp force-pushed the close-on-eof-reader branch from 86865d5 to 45cc0c5

Rebased (and fixing missing signatures on commits)

@github-project-automation github-project-automation Bot moved this from Needs Triage to Review In Progress in Pull Request Review Jun 2, 2026
@samuelkarp
samuelkarp added this pull request to the merge queue Jun 2, 2026
Merged via the queue into containerd:main with commit 9eed061 Jun 2, 2026
87 of 88 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Jun 2, 2026
@fuweid

fuweid commented Jun 3, 2026

Copy link
Copy Markdown
Member

I think we don't need to add this wrapper based on my existing patch - https://github.com/containerd/containerd/pull/9369/changes

I can confirm the local pull is still working. TestCRIImagePullTimeout/SlowCommitWriter test case is designed for slow committer. The resolver will use wrapper RoundTripper, which means it will decrease request number after EOF.

func (rt *pullRequestReporterRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {

The question is that the transfer service is using different logic which depends on status.

case "downloading":
var curProgress int64
if node, ok := reporter.statuses[p.Name]; !ok {
curProgress = p.Progress
reporter.reqReporter.incRequest()
} else {
curProgress = p.Progress - node.Progress
}
reporter.statuses[p.Name] = &p
if curProgress > 0 {
reporter.IncBytesRead(curProgress)
}

Based on #9369, it will close reader on EOF so we don't need this change actually.

So could we just revert this and apply that change 6c396d0 into this line as well?

if activeReqs == 0 || bytesRead > lastSeenBytesRead {

cc @samuelkarp @dmcgowan

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

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants