Keep a finished download's frame for a moment
Small downloads, empty or one byte, were sometimes never saved, though the worker reported them finished and both sides logged success. The browser can read the whole response before it hands the navigation to its download manager, and the frame was removed right then, which cancels it. Without a delay an empty file failed 8 of 10 runs, a one byte file 4 of 10. The download's frame now stays for DOWNLOAD_GRACE (1 s) after finished, the frame that holds the worker still goes right away. No event tells when the browser took the download, so the 1 s is empirical. A new test checks the retention on a paused clock, it fails without the delay and with the worker's frame held too. The browser's timing it can't cover, TODO.md keeps a record of that. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd0154db68
commit
3c4e706e1f
3 files changed
+52
-9
No files matched your search
+12
-4
@@ -27,6 +27,10 @@ const WINDOW = 8 * 1024 * 1024;
|
||||
// Firefox stops a service worker after 30 s without events, also in the middle of a download, and
|
||||
// then saves the truncated file as complete. A stalled transfer sends no chunks, so the receiver pings.
|
||||
const KEEPALIVE = 10_000;
|
||||
// The browser may read all of a small file, e.g. an empty one, before it hands the navigation to its
|
||||
// download manager. Its frame removed before that, the download never shows up, though the worker
|
||||
// reported it finished. No event tells when the browser took it, 1 s was always enough in the tests.
|
||||
const DOWNLOAD_GRACE = 1000;
|
||||
const ICE_GRACE = 300;
|
||||
const ICE_TIMEOUT = 5000;
|
||||
// A STUN server that reports no public address by then gets passed over, see stunServer.
|
||||
@@ -631,10 +635,14 @@ function receiveFile(
|
||||
|
||||
let keepalive: ReturnType<typeof setInterval> | undefined;
|
||||
|
||||
// Releases the worker, so a waiting update can take over.
|
||||
function release() {
|
||||
// Releases the worker, so a waiting update can take over. A finished download keeps its own frame
|
||||
// for DOWNLOAD_GRACE, that one doesn't hold the worker.
|
||||
function release(finished = false) {
|
||||
clearInterval(keepalive);
|
||||
for (const frame of frames) frame.remove();
|
||||
const [hold, download] = frames;
|
||||
hold?.remove();
|
||||
if (finished) setTimeout(() => download?.remove(), DOWNLOAD_GRACE);
|
||||
else download?.remove();
|
||||
}
|
||||
|
||||
// The worker answers with failed, only then the frames go: removed earlier, Chromium keeps the
|
||||
@@ -687,7 +695,7 @@ function receiveFile(
|
||||
}
|
||||
ended = true;
|
||||
channel.close();
|
||||
release();
|
||||
release(data === 'finished');
|
||||
};
|
||||
worker.postMessage({ id, ...meta }, [port2]);
|
||||
return worker;
|
||||
|
||||
Reference in new issue
Block a user