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
@@ -23,7 +23,11 @@
|
|||||||
not reproduced in 3 runs since. The report was lost; the attached logs will show the cause if it
|
not reproduced in 3 runs since. The report was lost; the attached logs will show the cause if it
|
||||||
happens again.
|
happens again.
|
||||||
|
|
||||||
- `boundary: chromium sends an empty file` timed out in the first two full runs after `backend/`'s
|
- Small downloads (empty, one byte) were sometimes never saved, though the worker reported them
|
||||||
tests were added (2026-10-25), with empty logs on both sides. It passed alone 8 times, and in 7
|
finished and both sides logged success: the browser can read the whole response before it hands
|
||||||
full runs since, one of them with a cold Vite cache. The suite without the new tests passed 4
|
the navigation to its download manager, and removing the frame before that cancels it. Without a
|
||||||
times. Not reproduced, cause unknown.
|
delay `boundary: … sends an empty file` failed 8 of 10 runs, the one byte file 4 of 10. The
|
||||||
|
workaround `DOWNLOAD_GRACE` keeps the download's frame 1 s after finished. No event tells when the
|
||||||
|
browser took the download, so the 1 s is empirical: `boundary: … keeps a finished download's frame
|
||||||
|
for DOWNLOAD_GRACE` covers the retention, not the browser's timing. A small download that goes
|
||||||
|
missing again means it's too short.
|
||||||
+32
-1
@@ -31,11 +31,12 @@ const SIZE = 5_000_000;
|
|||||||
const LARGE_SIZE = 2.5 * 2 ** 30;
|
const LARGE_SIZE = 2.5 * 2 ** 30;
|
||||||
// Big enough that the transfer still runs while a test cancels or disconnects.
|
// Big enough that the transfer still runs while a test cancels or disconnects.
|
||||||
const RUNNING_SIZE = 100_000_000;
|
const RUNNING_SIZE = 100_000_000;
|
||||||
// WINDOW, PROBE_TIMEOUT, PROOF_TIMEOUT, and CONNECT_TIMEOUT in src/core.ts.
|
// WINDOW, PROBE_TIMEOUT, PROOF_TIMEOUT, CONNECT_TIMEOUT and DOWNLOAD_GRACE in src/core.ts.
|
||||||
const WINDOW = 8 * 2 ** 20;
|
const WINDOW = 8 * 2 ** 20;
|
||||||
const PROBE_TIMEOUT = 2000;
|
const PROBE_TIMEOUT = 2000;
|
||||||
const PROOF_TIMEOUT = 10_000;
|
const PROOF_TIMEOUT = 10_000;
|
||||||
const CONNECT_TIMEOUT = 30_000;
|
const CONNECT_TIMEOUT = 30_000;
|
||||||
|
const DOWNLOAD_GRACE = 1000;
|
||||||
// Enough for a transfer whose receiver holds back its reads to pause at the window.
|
// Enough for a transfer whose receiver holds back its reads to pause at the window.
|
||||||
const HELD_SIZE = 3 * WINDOW;
|
const HELD_SIZE = 3 * WINDOW;
|
||||||
// What the fake STUN server reports as public address, reserved for documentation.
|
// What the fake STUN server reports as public address, reserved for documentation.
|
||||||
@@ -672,7 +673,9 @@ for (const name of Object.keys(BROWSERS)) {
|
|||||||
|
|
||||||
// The edges of the chunking and the window, and a name the Content-Disposition header has to encode.
|
// The edges of the chunking and the window, and a name the Content-Disposition header has to encode.
|
||||||
for (const [what, file, size] of [
|
for (const [what, file, size] of [
|
||||||
|
// Read whole before the browser takes the download, see DOWNLOAD_GRACE in src/core.ts.
|
||||||
['an empty file', 'empty.bin', 0],
|
['an empty file', 'empty.bin', 0],
|
||||||
|
['a one byte file', 'one.bin', 1],
|
||||||
// Firefox would save a % as _.
|
// Firefox would save a % as _.
|
||||||
['a file one byte past the window', "l'été (1) 🚀.bin", WINDOW + 1],
|
['a file one byte past the window', "l'été (1) 🚀.bin", WINDOW + 1],
|
||||||
] as const) {
|
] as const) {
|
||||||
@@ -682,6 +685,34 @@ for (const name of Object.keys(BROWSERS)) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When the browser takes a download is beyond the test, how long the frame stays is not: on the
|
||||||
|
// paused clock the download's frame stays, so the download has to arrive, and goes after exactly
|
||||||
|
// DOWNLOAD_GRACE. The frame holding the worker goes right away, it would block an update.
|
||||||
|
test(`boundary: ${name} keeps a finished download's frame for DOWNLOAD_GRACE`, async () => {
|
||||||
|
const [sender, receiver] = await Promise.all([open(name), open(name)]);
|
||||||
|
await receiver.clock.install();
|
||||||
|
|
||||||
|
await withLogs(sender, receiver, async () => {
|
||||||
|
await connect('offline', sender, receiver, await payload(Buffer.alloc(0), 'empty.bin'));
|
||||||
|
await sender.click('#confirm');
|
||||||
|
await receiver.click('#confirm');
|
||||||
|
await expect(receiver.locator('#incoming')).toContainText('empty.bin');
|
||||||
|
await receiver.clock.pauseAt(await receiver.evaluate(() => Date.now() + 1));
|
||||||
|
const started = downloadStarts(receiver);
|
||||||
|
await receiver.click('#download');
|
||||||
|
await expect(receiver.locator('#log')).toContainText('download complete');
|
||||||
|
const frames = receiver.locator('iframe');
|
||||||
|
await expect(frames).toHaveCount(1);
|
||||||
|
await expect(frames).toHaveAttribute('src', /^download\/./);
|
||||||
|
const { download } = await started;
|
||||||
|
expect(await hash(await download.path())).toBe(await hash(test.info().outputPath('empty.bin')));
|
||||||
|
await receiver.clock.runFor(DOWNLOAD_GRACE - 1);
|
||||||
|
await expect(frames).toHaveCount(1);
|
||||||
|
await receiver.clock.runFor(1);
|
||||||
|
await expect(frames).toHaveCount(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
// Share, Receive and the manual exchange each start a session, a new one ends the one before.
|
// Share, Receive and the manual exchange each start a session, a new one ends the one before.
|
||||||
test(`session: ${name} a new operation ends the previous one`, async () => {
|
test(`session: ${name} a new operation ends the previous one`, async () => {
|
||||||
const [sender, receiver] = await Promise.all([open(name), open(name)]);
|
const [sender, receiver] = await Promise.all([open(name), open(name)]);
|
||||||
|
|||||||
+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
|
// 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.
|
// then saves the truncated file as complete. A stalled transfer sends no chunks, so the receiver pings.
|
||||||
const KEEPALIVE = 10_000;
|
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_GRACE = 300;
|
||||||
const ICE_TIMEOUT = 5000;
|
const ICE_TIMEOUT = 5000;
|
||||||
// A STUN server that reports no public address by then gets passed over, see stunServer.
|
// 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;
|
let keepalive: ReturnType<typeof setInterval> | undefined;
|
||||||
|
|
||||||
// Releases the worker, so a waiting update can take over.
|
// Releases the worker, so a waiting update can take over. A finished download keeps its own frame
|
||||||
function release() {
|
// for DOWNLOAD_GRACE, that one doesn't hold the worker.
|
||||||
|
function release(finished = false) {
|
||||||
clearInterval(keepalive);
|
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
|
// The worker answers with failed, only then the frames go: removed earlier, Chromium keeps the
|
||||||
@@ -687,7 +695,7 @@ function receiveFile(
|
|||||||
}
|
}
|
||||||
ended = true;
|
ended = true;
|
||||||
channel.close();
|
channel.close();
|
||||||
release();
|
release(data === 'finished');
|
||||||
};
|
};
|
||||||
worker.postMessage({ id, ...meta }, [port2]);
|
worker.postMessage({ id, ...meta }, [port2]);
|
||||||
return worker;
|
return worker;
|
||||||
|
|||||||
Reference in new issue
Block a user