Skip to content

A brief network drop no longer freezes the download - #7

Merged
pruffit merged 4 commits into
mainfrom
fix/network-drop
Oct 17, 2026
Merged

pruffit merged 4 commits into
mainfrom
fix/network-drop

Conversation

@pruffit

@pruffit pruffit commented Oct 17, 2026 •

Copy link
Copy Markdown
Owner

A user reported that a VPN dropped for a few seconds during a download, and the progress stopped for good (#5).

Cause. request() in src/core.ts retried only when fetch rejected. A request in flight when the connection silently died stayed pending forever: there was no timeout, so the retry loop never ran. The body was also read outside the loop. A drop mid-body could hang the same way or fail the whole book.

Change.

  • src/lib/net.ts: fetchFully makes one attempt, with headers and body under a single idle timer (20 s without a byte fails it). Every await is raced against that timer, so even a fetch that ignores its signal cannot hang the download. A cancel by the user is still a cancel, not a network error.
  • src/core.ts: a dropped network is retried for a stretch of time (up to 2 min), not for a count of attempts. The clock is one per download (createOutage), so a long outage ends every request at once instead of each waiting its own window. Backoff is capped at 8 s, so a network that comes back is noticed quickly. The panel reads "The connection dropped — retrying in N s…". After that the download stops with the existing "The network is not responding" error, still marked network, so no generic re-parse runs. HTTP retries are unchanged: 5 attempts, Retry-After. The rate limiter is penalised once per outage, not on every retry, so the pace after a drop does not collapse to the maximum interval. The idle timer restarts when headers arrive.
  • src/translate/google.ts / book.ts: the same for translation requests, with one clock per translator (one per book). The wait is announced through a new optional notice argument of Translator.translate. If the network stays down past the window, translateBook stops and leaves the remaining chapters in the original, as it does for a refused chapter, instead of waiting out a new window per chapter.

Checked.

  • test/net.ts (16 tests) and test/translate.ts (+1) cover:
    • a fetch that never settles;
    • a body that stalls after the first chunk;
    • a slow but steady body that must not be cut off;
    • a cancel during a hung attempt and during the wait for the network;
    • hang → refusal → page;
    • a long outage ending in a network error;
    • a 404 that is not retried;
    • the translator through a hung request;
    • a fetch that reports the idle timeout as a DOMException AbortError, as browsers do: it is retried, not treated as a cancel;
    • a 503 retried once;
    • the shared clock: once an outage is over, the next request or chapter fails at once;
    • translateBook stopping after a network error;
    • a cancel during a hung translation.
  • The timing margins hold under load: 32 runs at 8 in parallel, no failures.
  • Canary: the old core.ts with a never-settling first request is still pending after 5 s with one attempt, which reproduces the report. The new one finishes on the second attempt.
  • npm run check passes: typecheck, build and 26 + 20 + 36 + 16 tests.

Kept as a trade-off: every fetch rejection counts as an outage. A VPN drop shows up as instant rejections while navigator.onLine stays true, so failing fast on those would defeat the fix. Adapters fetch same-origin, so CORS or ad-blocker rejections are rare.

Not done: chapters already fetched are not kept across a failed download. With a 2-minute grace this only matters for long outages, and keeping them is a bigger change.

Closes #5

🤖 Generated with Claude Code

pruffit and others added 4 commits October 17, 2026 10:38
A request caught in a VPN reconnect could stay pending forever: fetch had no
timeout and retries ran only when it rejected. Headers and body now share an
idle timer (20 s of silence fails the attempt), a dropped network is retried
for up to two minutes with the panel saying so, and a body cut off halfway is
fetched again. The translator gets the same treatment.

Closes #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ys down

Review of the first version: the translator's grace window restarted on every
chapter, so a long outage took chapters x 2 minutes with no word in the panel.
Now one clock serves the whole download (and one per translator), a network
error ends the translation and leaves the remaining chapters in the original,
and the wait is announced while translating too. The idle timer restarts when
headers arrive, the pace is widened once per outage, and the outage is timed
with performance.now(). Tests cover a fetch that reports the timeout as an
AbortError, a 503 retry, the shared clock, and cancelling a hung translation;
timing margins hold under 8x parallel load.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pruffit
pruffit merged commit 3dc2f36 into main Oct 17, 2026
1 check passed
@pruffit
pruffit deleted the fix/network-drop branch October 17, 2026 07:13
@pruffit pruffit mentioned this pull request Oct 17, 2026
Merged
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.

A brief network drop freezes the download for good

1 participant