Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Render: open-ended media ranges are capped, and the frame-duration lookup times out (#196)

Matt Van Horna5a6a041fb38
diff --git a/.changeset/render-media-connection-stalls.md b/.changeset/render-media-connection-stalls.md
new file mode 100644
index 0000000..a4aded2
--- /dev/null
+++ b/.changeset/render-media-connection-stalls.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Rendering a long edit keeps going. An open-ended read of a video is at most 8 MiB, so it cannot hold every connection to the film, and a frame is still drawn when asking how long the video's frames last does not answer within ten seconds.
diff --git a/packages/openfilm/src/host.mjs b/packages/openfilm/src/host.mjs
index c0ea2db..5cab5c6 100644
--- a/packages/openfilm/src/host.mjs
+++ b/packages/openfilm/src/host.mjs
@@ -132,9 +132,12 @@ export function folderFiles(root, { inject = `<script>${HOST_FLAG}</script>`, ti
/* A cancelled preview is normal: pipeline closes its file; its callback deliberately ignores the error. */
const range = /^bytes=(\d*)-(\d*)$/.exec(req.headers.range ?? '');
if (range && (range[1] || range[2])) {
- /* `bytes=-500` is the last 500 bytes; an open end runs to the end of the file */
+ /* `bytes=-500` is the last 500 bytes. An open end (`bytes=X-`) is at most the next 8 MiB: one response must
+ not hold a connection for the rest of a long file. A browser keeps six HTTP/1.1 connections to a host, and
+ a frame's duration lookup waits behind them until one is free. */
const start = range[1] ? Number(range[1]) : Math.max(0, stat.size - Number(range[2]));
- const end = range[1] && range[2] ? Math.min(Number(range[2]), stat.size - 1) : stat.size - 1;
+ const end = range[1] && !range[2] ? Math.min(start + 8 * 1024 * 1024 - 1, stat.size - 1)
+ : range[1] && range[2] ? Math.min(Number(range[2]), stat.size - 1) : stat.size - 1;
if (start > end || start >= stat.size) { res.writeHead(416, { ...headers, 'content-range': `bytes */${stat.size}` }).end(); return true; }
res.writeHead(206, { ...headers, 'content-range': `bytes ${start}-${end}/${stat.size}`, 'content-length': end - start + 1 });
pipeline(createReadStream(file, { start, end }), res, () => {});
diff --git a/packages/openfilm/src/host.test.mjs b/packages/openfilm/src/host.test.mjs
index 48d74b8..46977c9 100644
--- a/packages/openfilm/src/host.test.mjs
+++ b/packages/openfilm/src/host.test.mjs
@@ -2,7 +2,7 @@ import { test } from 'node:test';
import assert from 'node:assert/strict';
import { once } from 'node:events';
import fs from 'node:fs';
-import { mkdirSync, mkdtempSync, realpathSync, symlinkSync, writeFileSync } from 'node:fs';
+import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
import { get } from 'node:http';
import { syncBuiltinESMExports } from 'node:module';
import { tmpdir } from 'node:os';
@@ -61,6 +61,24 @@ test('cancelled media requests close their files so the project can be renamed',
}
});
+test('an open-ended media range returns at most 8 MiB', async () => {
+ const cap = 8 * 1024 * 1024;
+ const dir = mkdtempSync(join(tmpdir(), 'of-host-range-'));
+ const data = Buffer.alloc(cap + 2, '0123456789');
+ writeFileSync(join(dir, 'video.mp4'), data);
+ const site = await serve(dir);
+ try {
+ const res = await fetch(`${site.url}/video.mp4`, { headers: { range: 'bytes=1-' } });
+ assert.equal(res.status, 206);
+ assert.equal(res.headers.get('content-length'), String(cap));
+ assert.equal(res.headers.get('content-range'), `bytes 1-${cap}/${data.length}`);
+ assert.deepEqual(Buffer.from(await res.arrayBuffer()), data.subarray(1, cap + 1));
+ } finally {
+ await site.close();
+ rmSync(dir, { recursive: true, force: true });
+ }
+});
+
test('the served folder is all a page reads: no way out through .., a link or a malformed address', { skip: process.platform === 'win32' && 'links need Developer Mode on Windows' }, async () => {
const scratch = mkdtempSync(join(tmpdir(), 'of-host-'));
const root = join(scratch, 'film');
diff --git a/packages/openfilm/src/timeline.mjs b/packages/openfilm/src/timeline.mjs
index 638f782..0d4cc89 100644
--- a/packages/openfilm/src/timeline.mjs
+++ b/packages/openfilm/src/timeline.mjs
@@ -731,14 +731,15 @@ const CUE_MS = 5000;
const frameLengths = new Map();
/**
* How long a video clip's frames last, as the host that serves the film says (host.mjs, from ffprobe); null from a
- * server that cannot tell. Asked once a file.
+ * server that cannot tell, or that does not answer within 10 s (the request or its body). Asked once a file: a null
+ * stays, so a stalled lookup cannot hold this frame or the next one of the same file. The frame is drawn without it.
*/
function frameOf(c) {
const src = url(c.row.src);
/* a video from another site: not the host's to say */
if (src.origin !== location.origin) return Promise.resolve(null);
if (!frameLengths.has(src.pathname)) {
- frameLengths.set(src.pathname, fetch(new URL(`__film/frame-duration?of=${encodeURIComponent(src.pathname)}`, location.href))
+ frameLengths.set(src.pathname, fetch(new URL(`__film/frame-duration?of=${encodeURIComponent(src.pathname)}`, location.href), { signal: AbortSignal.timeout(10_000) })
.then((r) => (r.ok ? r.json() : null)).then((said) => (said?.seconds > 0 ? said.seconds : null)).catch(() => null));
}
return frameLengths.get(src.pathname);
diff --git a/packages/openfilm/src/timeline.test.mjs b/packages/openfilm/src/timeline.test.mjs
index 2a00709..70aa555 100644
--- a/packages/openfilm/src/timeline.test.mjs
+++ b/packages/openfilm/src/timeline.test.mjs
@@ -1,7 +1,8 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
-import { cpSync, mkdtempSync, writeFileSync } from 'node:fs';
+import { cpSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { FilmPage, launch, serve } from './host.mjs';
@@ -1025,3 +1026,26 @@ window.film = { duration: 4, width: 320, height: 180,
await s.close();
}
});
+
+test('an exact frame is drawn when the frame duration never answers', { skip: !hasFfmpeg && 'no ffmpeg', timeout: 40_000 }, async () => {
+ const dir = footage('of-duration-stall-');
+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ id: 'v', src: 'a.mp4', time: [0, 4] }] }] }));
+ const stall = createServer(() => {});
+ await new Promise((done) => stall.listen(0, '127.0.0.1', done));
+ const s = await open(dir);
+ try {
+ const port = stall.address().port;
+ await s.film.page.route('**/__film/frame-duration?*', (route) => route.continue({ url: `http://127.0.0.1:${port}/stall` }));
+ const cancelled = s.film.page.waitForEvent('requestfailed', {
+ predicate: (request) => request.url() === `http://127.0.0.1:${port}/stall`, timeout: 25_000,
+ });
+ await Promise.all([s.film.seek(0.4), cancelled]);
+ const [video] = await videosNow(s.film);
+ assert.ok(video.shown && video.ready && Math.abs(video.time - 0.4) < 0.01, JSON.stringify(video));
+ } finally {
+ await s.close();
+ stall.closeAllConnections();
+ await new Promise((done) => stall.close(done));
+ rmSync(dir, { recursive: true, force: true });
+ }
+});