Skip to content

fix(engine): retry a connection that stops sending - #1

Merged
troke12 merged 3 commits into
troke12:mainfrom
rigaz29:engine-stall-timeout
Oct 19, 2026
Merged

troke12 merged 3 commits into
troke12:mainfrom
rigaz29:engine-stall-timeout

Conversation

@rigaz29

@rigaz29 rigaz29 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A connection that accepts a range and then sends no further bytes used to block its worker inside Body.Read. The wait for response headers could block the same way, inside Client.Do. Neither reached the retry loop, so the transfer sat there until the whole download was paused.

Both waits are now bounded. A connection that goes quiet mid-body is closed after 30 seconds and its chunk is retried from the last byte already written. A connection still waiting for headers gets 5 minutes, because some servers build the file (an archive, an export) before they answer. The other connections of the same file keep going. Pause still cancels the whole transfer and does not count as a stall.

Behavior

  • Body idle limit: DefaultStallTimeout (30 s). It is one time.AfterFunc per connection, armed only around each Read, so time spent in the speed limiter between reads is not counted as a stall.
  • Header limit: DefaultHeaderTimeout (5 min). It is stopped once the headers arrive, so a long transfer is not cut off.
  • A stall returns errStalled. The existing retry loop (up to MaxRetries, with the same backoff) requests the chunk again from chunk.Next().
  • Cancelling the download context returns that context error immediately, so a pause does not burn retries.
  • Requests go through d.guardedClient() (the local-network guard on main), not d.client.

Commits

  • be198b8, a8da922 (Ryan Bagas): the original change, rebased onto current main. The conflict resolution routes exchange through guardedClient().
  • b0783bb: review changes. Adds the separate header limit, replaces the per-read goroutine+timer with one AfterFunc, and asserts the retry Range.

Not changed

  • Inspect (the probe) still has no header limit. Left for a follow-up.

Test plan

  • go test ./... and go test -race ./internal/engine
  • TestStalledConnectionRetriesFromLastByte: after 4 KiB and a stall, the retry asks for bytes=4096-… and the file matches the source.
  • TestHeaderStallRetries: no headers within the header limit retries the chunk.
  • TestSlowHeadersWithinHeaderLimit: headers slower than the stall limit but within the header limit succeed on the first transfer. This test fails if the header wait reuses the stall limit.
  • TestPauseDuringHeaderWaitIsPaused
  • Download a large file with several connections and confirm one quiet connection does not stop the others.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)

@troke12 troke12 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks correct overall (timer/goroutine cleanup and pause semantics are good). Two requests:

  1. The 30 s limit also bounds the wait for response headers. Servers that build the file before replying (on-the-fly archive/export endpoints) will now fail after 8 retries, where they work on main. Please use a separate, longer header timeout (or make it a setting).
  2. TestStalledConnectionRetriesFromLastByte never asserts that the retry asked for bytes=4096-; it would still pass if the chunk restarted from 0.

Minor: one goroutine + timer per 64 KiB read is on the hottest path; a single time.AfterFunc(idle, body.Close) reset after each read would be cheaper. Inspect (probe) is still unbounded.


Note for the whole set (#1–#11): thanks a lot for these! They are all branched from 5350f79 independently, so every pair conflicts on CHANGELOG.md and most on download.go. main has also moved (e830f5d adds a stream downloader that touches download.go). Could you rebase onto current main and stack them in this order: #1 → #2 → #10 → #8 → #5/#6/#7/#11? #2, #3 and #10 each add their own pass loop / transferCancel in Start; one shared design would be much easier to merge.

rigaz29 added a commit to rigaz29/odm that referenced this pull request Oct 15, 2026
Brings the eleven engine changes (troke12/odm troke12#1-troke12#11) onto upstream
v0.3.0-rc1. Conflicts in CHANGELOG.md and internal/engine/download.go
keep both sides: the stream fields and the d.Stream branch from upstream
run before the engine's pass loop, and State saves both the validators
and the stream job.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@troke12
troke12 force-pushed the engine-stall-timeout branch from 57e7ff5 to b0783bb Compare October 19, 2026 02:27
@troke12

troke12 commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Rebased onto current main and applied the review: response headers now have their own 5-minute limit (DefaultHeaderTimeout) so build-then-answer endpoints keep working, the body idle limit is one AfterFunc per connection armed around each Read, and the stall test asserts the retry asks for bytes=4096-. exchange now goes through guardedClient() from main. Your two commits are kept with you as author; the review changes are a separate commit on top. Thanks!

rigaz29 and others added 3 commits October 19, 2026 09:32
A worker blocked in the response body when a server accepted the range and then sent no further bytes. Close that body after 30 seconds without a read and retry the chunk from the last written offset. Pause still cancels the whole transfer, and the other connections are left running.
The stall limit now covers the wait until response headers arrive.
It is stopped once they do, so a long body is still limited only by
idle reads. A pause during that wait is not a stall.
Review changes on top of the previous two commits:

- The wait for response headers is bounded by DefaultHeaderTimeout
  (5 minutes) instead of the 30 s stall limit. A server that builds the
  file before answering (an on-the-fly archive or export) sends nothing
  until it is done; with the stall limit it failed after 8 retries, where
  it works on main. TestSlowHeadersWithinHeaderLimit covers it.
- The body's idle limit is one time.AfterFunc per connection, armed only
  around each Read, instead of a goroutine and a timer per 64 KiB read.
  Time spent in the speed limiter between reads no longer counts as idle.
- TestStalledConnectionRetriesFromLastByte asserts the retry asks for
  bytes=4096-, so it fails if the chunk restarted from 0.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
@troke12
troke12 force-pushed the engine-stall-timeout branch from b0783bb to 4a48126 Compare October 19, 2026 02:33
@troke12
troke12 merged commit 2314ca0 into troke12:main Oct 19, 2026
5 checks passed
troke12 pushed a commit to rigaz29/odm that referenced this pull request Oct 19, 2026
Review changes on top of the previous commit:

- TestProbeFirstByteSingleChunk covers the regression the review found:
  a server answering 206 to bytes=0-0 but 200 to a range over the whole
  file, with one connection or a file too small to split. With troke12#10 on
  main the whole-file 200 is kept, so it completes in one request.
- Test servers no longer tell the probe by its open-ended range, which
  bytes=0-0 broke (troke12#1, troke12#7 and troke12#10 fixtures): isProbe and probeRange in
  engine_test.go, and testRange reports the probe as not explicit.
- The PR's test helper is renamed testWholeSpan (wholeFileRange is now a
  production function) and the test moves to probe_byte_test.go, as
  probe_test.go exists on main.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
troke12 pushed a commit that referenced this pull request Oct 19, 2026
Review changes on top of the previous commit:

- TestProbeFirstByteSingleChunk covers the regression the review found:
  a server answering 206 to bytes=0-0 but 200 to a range over the whole
  file, with one connection or a file too small to split. With #10 on
  main the whole-file 200 is kept, so it completes in one request.
- Test servers no longer tell the probe by its open-ended range, which
  bytes=0-0 broke (#1, #7 and #10 fixtures): isProbe and probeRange in
  engine_test.go, and testRange reports the probe as not explicit.
- The PR's test helper is renamed testWholeSpan (wholeFileRange is now a
  production function) and the test moves to probe_byte_test.go, as
  probe_test.go exists on main.

✨ Generated with Claude Code (claude.ai/claude-code) 2.1.303 (claude-opus-5-5)
@rigaz29
rigaz29 deleted the engine-stall-timeout branch October 19, 2026 05:01
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.

2 participants