Skip to content

Video as code: canvas scenes rendered with WebCodecs in headless Chrome, a review viewer, and agent skills that build the video from your references.

Chrome: pass --no-sandbox when running as root (#2)

Rifkiend39963c2cec1d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 060f70d..efc628f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,10 @@ Changes are listed by release, newest first. The sections dated before 0.1.0 rec
## Unreleased
+### Fixed
+
+- Running motlab as root, as in a container or on a VPS logged in as root, failed at the first scene with "Chrome exited early (code 1): Running as root without --no-sandbox is not supported", because Chrome refuses to start as root and motlab passed no such flag. Chrome now gets `--no-sandbox` when motlab runs as root, and only then; motlab only opens the user's own pages. Checked on Ubuntu 24.04 as root, no GPU, Google Chrome 157: `motlab render example --scale 1` went from failing at once to a 9 s, 1080p render in 13.6 s (8.5 s on a repeat run, with the encoder probe cached), and `npm test` passed 34/34. Not tried: a non-root user in a container where Chrome's sandbox still fails.
+
### Changed
- The copyright line in `LICENSE` names Stanyslaus Hary (motlab) instead of "motlab contributors". The licence is still MIT.
diff --git a/docs/cli.md b/docs/cli.md
index de5aa26..dd52da2 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -52,7 +52,7 @@ When the page throws while loading, the error names the file, line and column, f
| `--chrome` | Path to Chrome. Default: found automatically, or the `CHROME` environment variable |
| `--format` | `png` or `jpg`, for `stills` |
-`--gpu` is accepted and ignored, so older commands that pass it keep working. Chrome always runs with `--enable-gpu --force_high_performance_gpu --enable-unsafe-swiftshader`; the last lets WebGL fall back to software where there is no GPU.
+`--gpu` is accepted and ignored, so older commands that pass it keep working. Chrome always runs with `--enable-gpu --force_high_performance_gpu --enable-unsafe-swiftshader`; the last lets WebGL fall back to software where there is no GPU. When motlab runs as root, as in a container or on a VPS logged in as root, Chrome also gets `--no-sandbox`, because Chrome refuses to start as root without it; motlab only opens the user's own pages.
## Environment
diff --git a/docs/rendering.md b/docs/rendering.md
index 1998d2f..705196b 100644
--- a/docs/rendering.md
+++ b/docs/rendering.md
@@ -5,7 +5,7 @@ How `motlab render`, `motlab video` and Export turn scene pages into an MP4, how
## Steps
1. A small static server serves the project folder (the nearest folder above the page with a `project.json`, else the page's folder, or `--root`) on `127.0.0.1`.
-2. Chrome starts with `--headless=new --enable-gpu --force_high_performance_gpu --enable-unsafe-swiftshader`. The GPU actually in use is read from `WEBGL_debug_renderer_info` and printed at the start.
+2. Chrome starts with `--headless=new --enable-gpu --force_high_performance_gpu --enable-unsafe-swiftshader`, plus `--no-sandbox` when motlab runs as root. The GPU actually in use is read from `WEBGL_debug_renderer_info` and printed at the start.
3. `src/page/runtime.js` is injected into the page. For each frame it calls `renderAt`, makes a `VideoFrame` from the canvas and feeds it to a `VideoEncoder`.
4. Mediabunny muxes the encoded chunks into an MP4 inside the page, which is POSTed to Node. If that fails, the engine retries with a raw Annex B bitstream.
5. ffmpeg copies the stream (`-c copy`) into the final file. This pass writes BT.709 tags with the encoder's range into the bitstream, joins the parts from parallel mode, adds audio and moves `moov` to the front. It always runs, even after a successful Mediabunny mux, because the encoders tag colour inconsistently (see quirks).
diff --git a/src/chrome.js b/src/chrome.js
index 9ad8c50..347e055 100644
--- a/src/chrome.js
+++ b/src/chrome.js
@@ -24,13 +24,19 @@ export function findChrome(explicit) {
// only opens the user's own pages.
export const GPU_FLAGS = ['--enable-gpu', '--force_high_performance_gpu', '--enable-unsafe-swiftshader', ...(process.platform === 'darwin' ? ['--use-angle=metal'] : [])];
+// Chrome refuses to start as root without --no-sandbox ("Running as root without
+// --no-sandbox is not supported"), which is how it runs in a container or on a
+// VPS logged in as root. motlab only opens the user's own pages, so the sandbox
+// buys nothing here; add the flag when root, and only then.
+export const SANDBOX_FLAGS = typeof process.getuid === 'function' && process.getuid() === 0 ? ['--no-sandbox'] : [];
+
const live = new Set();
process.on('exit', () => { for (const c of live) c.killSync(); });
export async function launchChrome({ chromePath, extraArgs = [] }) {
const profile = mkdtempSync(join(tmpdir(), 'motlab-chrome-'));
const proc = spawn(chromePath, [
- '--headless=new', ...GPU_FLAGS,
+ '--headless=new', ...GPU_FLAGS, ...SANDBOX_FLAGS,
'--remote-debugging-port=0', `--user-data-dir=${profile}`,
'--no-first-run', '--no-default-browser-check', '--hide-scrollbars', '--mute-audio',
'--disable-background-timer-throttling', '--disable-renderer-backgrounding', '--disable-backgrounding-occluded-windows',
diff --git a/src/cli.js b/src/cli.js
index 623beb7..369e36b 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -47,7 +47,7 @@ const USAGE = `motlab: render a canvas page (canvas #c, renderAt(t), TOTAL, read
--root folder to serve (default: the nearest folder above the page with a project.json, else the page's folder)
--chrome path to Chrome (default: autodetect, or the CHROME environment variable)
-Chrome always runs with --enable-gpu --force_high_performance_gpu --enable-unsafe-swiftshader (--gpu is accepted and ignored, so older commands
+Chrome always runs with --enable-gpu --force_high_performance_gpu --enable-unsafe-swiftshader, plus --no-sandbox when running as root (--gpu is accepted and ignored, so older commands
that pass it keep working). Needs ffmpeg and ffprobe on PATH.
Engine folder: ${fileURLToPath(new URL('..', import.meta.url))} (README.md, example/, src/page/footage.js)`;
diff --git a/test/chrome.test.js b/test/chrome.test.js
new file mode 100644
index 0000000..ede01e8
--- /dev/null
+++ b/test/chrome.test.js
@@ -0,0 +1,10 @@
+// Chrome's launch flags: --no-sandbox is added when, and only when, motlab
+// runs as root, because Chrome refuses to start as root without it.
+import assert from 'node:assert/strict';
+import { test } from 'node:test';
+import { SANDBOX_FLAGS } from '../src/chrome.js';
+
+test('--no-sandbox is passed exactly when running as root', () => {
+ const root = typeof process.getuid === 'function' && process.getuid() === 0;
+ assert.deepEqual(SANDBOX_FLAGS, root ? ['--no-sandbox'] : []);
+});