Fix large file download

This commit is contained in:
Schluffe committed 2026-10-22 22:57:41 +02:00
1 parent aea75fff92
commit ff7b84af02
4 files changed
+403 -146

No files matched your search

+15 -48
View File
@@ -1,57 +1,24 @@
# TODO
## Next: service worker streaming download
## Ideas
Stream received bytes straight into the browser's download manager instead of staging them in OPFS.
Needs one new file (service workers can only be registered from a real same-origin script).
- Removes the OPFS staging copy, so the file no longer takes twice the disk space.
- Removes the OPFS quota cap (~10 GiB per site, 2000 MiB in Chromium incognito).
- Fixes "Opening another tab breaks existing downloads" below completely.
## Review findings
### [P2] Opening another tab breaks existing downloads
`clearStorage()` runs on startup and deletes every OPFS entry. OPFS is shared by all tabs of the
origin, so it also deletes the file behind another tab's not yet clicked download link.
- Real fix: Web Locks. Each tab holds a lock named after every file it received (released by the
browser when the tab closes or crashes). Cleanup only removes entries whose lock is free
(`navigator.locks.request(name, { ifAvailable: true }, ...)`).
- Fixed by the service worker download: no OPFS staging, nothing to clean up.
### [P2] Disk writes have no flow control
Chunks go data channel → main thread → worker message queue → disk without waiting for writes. If
storage is slower than the network (SD card, HDD, throttled background tab), queued buffers grow
without bound in RAM.
- Real fix: application level acknowledgements. The receiving side cannot pause a data channel,
so the worker reports its written offset, the receiver sends it to the sender over the data
channel, and the sender keeps at most a fixed window (e.g. 8 MiB) unacknowledged, on top of the
existing `bufferedAmount` check.
- Not fixed by the service worker: stream backpressure stops at the receiver, it still has to
reach the sender.
- Bonus: the acknowledged offset is the basis for resuming interrupted transfers.
### [P2] Storage failures don't stop the transfer
A write failure is only logged on the receiver. The sender keeps sending and reports "sent", which
only means "handed to the send buffer".
- Real fix:
1. Check `navigator.storage.estimate()` against the announced size before accepting, optionally
`navigator.storage.persist()` (raises Firefox's limit after a prompt).
2. Report errors to the sender (`{ error }` message or closing the channel), sender stops.
3. Receiver sends "done" only after the final flush, sender shows "delivered" only then.
- Partly fixed by the service worker: the quota failure goes away, but download errors and user
cancels still have to be reported to the sender.
- Points 2 and 3 share the receiver → sender control channel with the flow control fix, build them
together.
- Resume interrupted transfers: the receiver already reports how much the download manager has
read (`{ read }`), which is where a new connection could continue. The browser can't append to a
finished or failed download though, so a resume would have to start a new one.
## Open observations
- Firefox ignores an errored service worker response stream: when the sender vanishes, the receiver
logs "download failed: connection lost", but the download stays "in progress" until the user
cancels it. Closing the stream instead is worse, Firefox then reports the truncated file as
complete despite `Content-Length`. Chromium fails the download, but only once the connection
counts as failed (~16 s), because its data channel never fires `close` for a vanished peer.
- Firefox also saves a truncated file as complete when it stops the service worker in the middle of
a download (after 30 s without events). The receiver's pings (`KEEPALIVE`) prevent that, covered by
`large: firefox survives a 40 s stall`. A browser that stops it anyway would still never show
"download complete" or "delivered", only its download manager would be wrong.
- `large: firefox sends 2.5 GiB` hit the 5 minute timeout once (first run after the normal suite),
not reproduced in 3 runs since. The report was lost; the attached logs will show the cause if it
happens again.