diff --git a/TODO.md b/TODO.md index b3a84b2..e0c34ce 100644 --- a/TODO.md +++ b/TODO.md @@ -23,7 +23,11 @@ not reproduced in 3 runs since. The report was lost; the attached logs will show the cause if it happens again. -- `boundary: chromium sends an empty file` timed out in the first two full runs after `backend/`'s - tests were added (2026-10-25), with empty logs on both sides. It passed alone 8 times, and in 7 - full runs since, one of them with a cold Vite cache. The suite without the new tests passed 4 - times. Not reproduced, cause unknown. +- Small downloads (empty, 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 removing the frame before that cancels it. Without a + 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. diff --git a/e2e.spec.ts b/e2e.spec.ts index fb21cea..b82c514 100644 --- a/e2e.spec.ts +++ b/e2e.spec.ts @@ -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)]); diff --git a/src/core.ts b/src/core.ts index dd273d6..321d7f6 100644 --- a/src/core.ts +++ b/src/core.ts @@ -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 | 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;