fix(documents): read and render PDFs in a bounded child process

pdf.js decodes an image at the size the file declares, whatever size the
page is drawn at, and the server opened documents without an image limit.
A small upload declaring a very large image made the web process allocate
gigabytes until the container was killed, for every user at once, and the
thumbnail job ran the same document again after each restart. An image
whose soft mask declares the size does the same past pdf.js' own limit, and
nothing bounded how long a text read could take.

pdf.js is now opened with maxImageSize (20 million pixels, which keeps one
image inside the reader's ceiling and admits a 300 dpi A3 scan), and every
read and render runs in a child process, one at a time: its V8 heap is
capped, its resident memory is sampled and the child killed past 640 MB, on
Linux it volunteers to the out-of-memory killer first, a time limit ends a
read that runs on, and it starts with an almost empty environment. The
loader and the rendering moved into pdf-engine.mjs, which the child runs
with plain Node and which is traced into the standalone image.

A document that crosses a bound answers resource-limit. The thumbnail job
records it as a final failure, so the backfill does not queue it again.
This commit is contained in:
Marc-André Bombeck committed 2026-10-25 13:52:04 +01:00
1 parent 953c64f7a4
commit 518ce2fd37
12 files changed
+1240 -494

No files matched your search

+5 -4
View File
@@ -26,9 +26,10 @@
* which is how the server finds it.
* - No runtime module under `src/` imports pdf-parse, and none imports
* pdfjs-dist (a type-only import included) except
* `src/lib/documents/pdfjs.ts`, so the text read and the rasterizer open
* documents with the same options. Comments are stripped first, so prose
* that names a package does not count.
* `src/lib/documents/pdf-engine.mjs`, so the text read and the rasterizer
* open documents with the same options, under the same image limit, in
* the bounded child process. Comments are stripped first, so prose that
* names a package does not count.
*
* It does not prove the image carries one copy; the Dockerfile hoists the
* pinned version by name and the tracing glob can only find what the lockfile
@@ -49,7 +50,7 @@ import {
const ROOT = process.cwd();
/** Relative to `src/`. */
const LOADER = "lib/documents/pdfjs.ts";
const LOADER = "lib/documents/pdf-engine.mjs";
const pkg = JSON.parse(readSourceText(join(ROOT, "package.json"))) as {
dependencies?: Record<string, string>;