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:
SchluffeandClaude Opus 5.5 committed 2026-10-25 19:55:45 +01:00
1 parent bd0154db68
commit 3c4e706e1f
3 files changed
+52 -9

No files matched your search

+32 -1
View File
@@ -31,11 +31,12 @@ const SIZE = 5_000_000;
const LARGE_SIZE = 2.5 * 2 ** 30;
// Big enough that the transfer still runs while a test cancels or disconnects.
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 PROBE_TIMEOUT = 2000;
const PROOF_TIMEOUT = 10_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.
const HELD_SIZE = 3 * WINDOW;
// 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.
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],
['a one byte file', 'one.bin', 1],
// Firefox would save a % as _.
['a file one byte past the window', "l'été (1) 🚀.bin", WINDOW + 1],
] 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.
test(`session: ${name} a new operation ends the previous one`, async () => {
const [sender, receiver] = await Promise.all([open(name), open(name)]);