Skip to content

fix(segments): never run more connections than allowed when gaps remain - #637

Merged
linroid merged 1 commit into
mainfrom
fix/segments-respect-connection-limit
Oct 23, 2026
Merged

linroid merged 1 commit into
mainfrom
fix/segments-respect-connection-limit

Conversation

@linroid

@linroid linroid commented Oct 23, 2026

Copy link
Copy Markdown
Owner

Problem

SegmentedDownloadHelper ran one connection per incomplete segment. SegmentCalculator.resegment can't join the gaps that finished ranges leave between them, so it keeps at least one segment per gap (coerceAtLeast(merged.size)). A batch could therefore run more connections than its effective count.

This is worst after a 429:

  1. DownloadExecution halves the connections, down to one.
  2. A download with three gaps still sends three requests at once, and the server answers 429 again.
  3. That fails the batch and cancels the transfers it did serve, after a few kilobytes each.
  4. Each round leaves more and smaller segments.

I found this with a local server that allows two concurrent requests and answers 429 otherwise. A 64 MB download:

  • went through more than 600 segments, mostly 8–80 KB;
  • made 300 requests in 150 s without finishing;
  • ran at 20–40 KB/s.

Fix

  • SegmentLedger now starts at most the batch's effective connections (connections, from effectiveConnections(requestedAtStart)).
  • The remaining incomplete lanes wait (SegmentLane.waiting). The first connection that frees up takes each one whole, before splitting any other lane.
  • Waiting lanes are handed out whatever the connection received last, so a batch never ends with one left.

Same server and file after the fix: done in 15 s with 11 requests, SHA-256 matching, while another download was competing for the same two slots.

Tests

  • SegmentedDownloadHelperTest.downloadAll_moreGapsThanConnections_runsOnlyTheConnections:
    • Five segments (three gaps) and one connection.
    • Never more than one transfer at a time.
    • The gaps are fetched whole and in order, and every byte is written once.
    • Fails without the fix.
  • The JVM tests of core, ftp, metalink, webdav and ktor pass.

A batch ran one connection per incomplete segment. Resegmenting to
fewer connections cannot join the gaps finished ranges leave, so after
a 429 cut the connections to one, a download with three gaps still sent
three requests at once: the server answered 429 again, which cancelled
the two transfers it served after a few kilobytes, and every round left
more, smaller segments. Against a server allowing two connections, a
64 MB download made 300 requests in 150 s and never finished, its
segments growing past 600.

The ledger now starts at most the batch's effective connections; the
other lanes wait and go whole to the first connection free, before any
split. The same download now finishes in 15 s with 11 requests.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T19:16:50.333904Z 6f7cd03 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@claude

claude Bot commented Oct 23, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 2 200 files  ±0   2 228 suites  ±0   56m 55s ⏱️ +46s
15 381 tests +4  15 113 ✅ +4  268 💤 ±0  0 ❌ ±0 
18 746 runs  +4  18 478 ✅ +4  268 💤 ±0  0 ❌ ±0 

Results for commit 6f7cd03. ± Comparison against base commit 0599fdb.

@linroid
linroid merged commit 1c83b5f into main Oct 23, 2026
15 checks passed
@linroid
linroid deleted the fix/segments-respect-connection-limit branch October 23, 2026 19:49
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.

1 participant