Skip to content

Resume blob downloads after a dropped connection - #949

Open
nandan-varma wants to merge 2 commits into
apple:mainfrom
nandan-varma:resume-blob-download
Open

nandan-varma wants to merge 2 commits into
apple:mainfrom
nandan-varma:resume-blob-download

Conversation

@nandan-varma

@nandan-varma nandan-varma commented Oct 7, 2026 •

Copy link
Copy Markdown

If the connection drops while downloading a layer, the pull fails and all progress on that layer is lost. This is easy to hit with large images (apple/container#2264).

With this change, fetchBlob retries after a network error and asks for the remaining bytes with a Range header. Resumes count against the existing RetryOptions.maxRetries. If the registry ignores the range, the download fails instead of starting over. The response length must match the remaining bytes exactly.

Added tests that use a local server which drops the connection partway through a download. I also tried a real container image pull through a proxy that cut the connection twice. It resumed and finished. Without this change it fails with stream ended at an unexpected time.

Fixes apple/container#2264

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

Thank you for the contribution! 👍

jlbauss pushed a commit to jlbauss/konrad that referenced this pull request Oct 27, 2026
`konrad update` on a Mac failed after about ten minutes, every time,
with `StreamClosed(… HTTP2ErrorCode<0x1 ProtocolError>)`. ghcr's CDN
resets slow blob downloads mid-stream (reproduced with a throttled curl:
three of three slow downloads cut after 4 to 9 minutes, the same
PROTOCOL_ERROR), and apple/container gives up on the first cut: its
registry client retries only before the response starts, and a failed
pull cancels its ingest session, throwing away every layer already
fetched (apple/container#2264). So on a slow link a large pull never
finishes, and re-running it starts from zero.

When `container image pull` fails, eng_pull now falls back to
container_pull_resumable: it reads the image's manifest itself, fetches
the config and layers the engine's content store lacks with `curl -C -`,
resuming each cut download until the blob is whole and its digest
matches, and hands them to `container image load` as an OCI archive
(layers already in the store are skipped by the load as by a pull).
Partial downloads stay under the state dir, so an interrupted fallback
resumes on the next try. All three pull paths (first run, update, the
background refresh) get it through eng_pull.

This is custom code for an upstream bug, tracked in ROADMAP → Upstream
workarounds: remove it once apple/containerization#949, which resumes a
dropped blob download, ships in a container release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nandan-varma

Copy link
Copy Markdown
Author

This has an approval and CI is green. Could someone take a look to merge. Happy to rebase or make changes if needed

}

guard expectedBytes <= descriptor.size else {
guard bodyOffset + expectedBytes <= descriptor.size else {

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.

This can trap and crash. Please rework it to guard against this possibility.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, it no longer adds to the offset so it can't overflow. Added a test.

return received
}
// Only count consecutive attempts that made no progress.
if received > receivedBeforeAttempt {

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.

The retry loop has no total bound, so that a range-ignoring registry can force a large number of downloads.

The PR resets the retry count here even if we receive one byte of data from the registry followed by a connection drop.

There is no overall deadline or retry count limit.

If the registry ignores Range and returns 200, each attempt re-downloads and discards everything received so far. The skipped bytes count toward nothing, so the transfer grows quadratically.

I think we want something simple here for retry policy, that ensures a decent probability of transferring a "large" layer on an "unreliable" channel. Ideally we'd have a sense for what we mean by "large" (10GB? 100GB?) and "unreliable" (error rate). It may be a simple cap here could suffice to cover the majority of cases (10 retry limit for example, but we might want to consider the case of a 200 reply to a range request differently).

Interested to see what you think on this one.

@nandan-varma nandan-varma Dec 3, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a simple version to start from: resumes count against maxRetries and the count never resets, so a blob is attempted at most maxRetries + 1 times.

On large vs unreliable: since each resume continues where it stopped, the limit is really about how many times one layer can drop, not its size. With the default of 3, a layer survives 3 drops. That felt fine for flaky Wi-Fi, but for 10GB+ layers on a bad link a separate cap (say 10, as you suggested) might be better. Happy to go either way.

For a 200 reply to a Range request I went with failing instead of restarting, so we never download the same bytes twice. That's the same as before this PR for registries that ignore Range.

I left out an overall deadline since #1028 covers a stalled body with a read timeout, but can add one here if you'd prefer.

throw ContainerizationError(
.invalidArgument,
message: "declared blob length \(expectedBytes) exceeds descriptor size \(descriptor.size) for \(descriptor.digest)"
message: "declared range end \(bodyOffset + expectedBytes) exceeds descriptor size \(descriptor.size) for \(descriptor.digest)"

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.

Also, the expected total bytes being less than the descriptor size should always result in a hash check error later. I don't think we want to tolerate a server that can't count properly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it now has to match exactly.

@jglogan

jglogan commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

@nandan-varma Thanks for the ping. Dmitry covered the functional aspects of the change already, but I've pointed out some areas we need to harden before this can be merged.

The declared length must now equal the bytes still missing, so a short
or oversized Content-Length fails right away and the check can no
longer overflow. Resumes count against maxRetries without resetting,
and a 200 reply to a Range request fails instead of downloading the
blob again.
@nandan-varma

Copy link
Copy Markdown
Author

Thanks @jglogan, addressed all three.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Retry on network errors before giving up

3 participants