diff --git a/sidecar/config.ts b/sidecar/config.ts index a7cb60e..5e75ad7 100644 --- a/sidecar/config.ts +++ b/sidecar/config.ts @@ -65,6 +65,21 @@ export const WORK_ROOT = (() => { export const MAX_UPLOAD_BYTES = num("AV_CLUB_MAX_UPLOAD_BYTES", 512 * 1024 * 1024, 1024); +/** + * How much RAM ImageMagick may hold while decoding, before it spills to a temporary file. + * + * Deliberately modest. This is the one operation here that can allocate against the size of the + * PICTURE rather than against anything this service chose, so it is the one that can take a small + * machine down — and taking a machine down is a worse outcome than being slow, every time, because + * the other things on it did not ask to be part of an image conversion. + * + * 256 MB comfortably holds the working set of a phone photograph. Past it nothing fails: ImageMagick + * memory-maps and then falls back to disk, so a genuinely enormous image is slower rather than fatal. + * + * Raise it on a machine with headroom. See sharp/decodable.ts for how it is applied. + */ +export const CONVERT_MEMORY_MB = num("AV_CLUB_CONVERT_MEMORY_MB", 256, 32); + /** A crafted file can make an encoder run effectively forever. The process is killed at this point. */ export const JOB_TIMEOUT_MS = num("AV_CLUB_JOB_TIMEOUT_MS", 120_000, 1_000); diff --git a/sidecar/sharp/decodable.ts b/sidecar/sharp/decodable.ts index b1a4cb3..e5dfe0c 100644 --- a/sidecar/sharp/decodable.ts +++ b/sidecar/sharp/decodable.ts @@ -1,7 +1,7 @@ import { join } from "node:path"; import { mkdtemp, rm } from "node:fs/promises"; import { tmpdir } from "node:os"; -import { JOB_TIMEOUT_MS } from "../config"; +import { CONVERT_MEMORY_MB, JOB_TIMEOUT_MS } from "../config"; import { unprocessable } from "../errors"; /** @@ -52,12 +52,41 @@ export async function decodableInput(path: string, format: string): Promise