Skip to content

client: download in batches, resume after an interruption - #57

Merged
dubadub merged 1 commit into
mainfrom
fix/batched-download
Nov 19, 2026
Merged

dubadub merged 1 commit into
mainfrom
fix/batched-download

Conversation

@dubadub

@dubadub dubadub commented Nov 19, 2026

Copy link
Copy Markdown
Member

Fixes the "Unexpected sync error" over an empty recipe list reported in cooklang/cookcli#849. Related: #4.

What went wrong

A first sync asked for every chunk of every file in one request, under the general 60 s request timeout, and wrote the files only after the last chunk had arrived. A library that did not fit in 60 s never finished: each attempt was cut off, kept nothing, and started again from the beginning. reqwest reports a body cut by the timeout as "error decoding response body", which the apps show as "Unexpected sync error".

Two things made 60 s too short:

  • everything went into one request;
  • the multipart parser searched its whole buffer again, and copied it, on every network read. A local server sends a 60 MB test library in 0.26 s; the client needed about 26 s to read it.

What changes

  • Batches. The listing is applied lowest id first, in batches of at most 2,000 chunks or about 16 MB (a file is never split). Each batch is fetched, written to disk and recorded before the next one starts, so files appear as they arrive.
  • Resume. A retry asks the server only for what is still missing.
  • Parser. MultipartParser looks at each byte once. The same 60 MB library now downloads in about 2 s.
  • Timeout. A chunk download has a limit of its own, 300 s.
  • Duplicates. A chunk used several times is requested once.
  • Stopping. A batch in flight stops when sync is cancelled, and a stopped download no longer reports idle.
  • Download first. Downloads and uploads take turns, and the first turn is a download. A commit records an id above everything on the server, and the next download resumes after the highest id recorded locally; a commit landing between two batches would have moved that point past files not fetched yet. A test shows exactly that happening without the turn-taking.

No change to the server, the wire format, the local database or the FFI surface.

How it was checked

  • cargo test --workspace: 180 pass. New tests cover batch splitting, ordering, keeping finished batches when a later one fails, duplicate chunks, the parser at read sizes from one byte up, stopping mid-download, and download-before-upload.
  • download_batch_outlives_the_general_request_timeout is #[ignore]d because it waits 65 s; it passes, and fails without the timeout change.
  • cargo build -p cooklang-sync-client --features ffi compiles.
  • Against a local server with 40 photos and 120 recipes (60 MB): a fresh folder fills progressively and ends identical to the source; killing the client at 32 of 160 files and restarting it resumes from record 32 and ends identical; a local-only file in the fresh folder is uploaded after the download.

Not checked: nothing has run on a phone, and I have not watched files appear in the apps.

Left for later

  • The resume point is still "highest id recorded locally", uploads included. If another device commits in the short window between this client's last listing and its own commit, that file is skipped. This existed before; the cure is a download position that uploads do not move, which needs a migration of the client database.
  • Downloaded chunks stay in memory for the whole run.
  • Files are still written in place (Atomic downloads: write to a temp file and rename #50).

A first sync asked for every chunk of every file in one request and wrote
the files only after the last chunk had arrived. A library that did not fit
the 60 s request timeout never finished: each attempt was cut off, kept
nothing and started again from the beginning, and the app showed
"Unexpected sync error" over an empty list (cooklang/cookcli#849).

- The listing is applied lowest id first, in batches of at most 2,000
  chunks or about 16 MB. Each batch is fetched, written to disk and
  recorded before the next one starts, so files appear as they arrive and
  a retry asks the server only for what is still missing.
- The multipart parser searched its whole buffer again, and copied it, on
  every network read. It now looks at each byte once: a 60 MB test library
  downloads from a local server in about 2 s instead of 26 s.
- A chunk download has a time limit of its own (300 s) instead of the
  general 60 s, and a chunk used several times is requested once.
- A batch in flight stops when sync is cancelled, and a stopped download
  no longer reports idle.
- Downloads and uploads take turns, and the first turn is a download. A
  commit records an id above everything on the server, and the next
  download resumes after the highest id recorded locally: a commit landing
  between two batches would have moved that point past files not fetched
  yet.
@dubadub
dubadub merged commit f065fdd into main Nov 19, 2026
6 of 7 checks passed
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