mirror of
https://github.com/morluto/rea
synced 2026-10-26 06:16:17 -04:00
66c21572de
* fix(bun): report the real runtime and enforce worker memory budgets on Windows Under Bun, REA recorded Node/V8 identity it did not observe and its JavaScript helper processes ran without a memory bound, because Bun ignores the V8 heap flags REA passes. Source-map trace Evidence claimed runtime node-v8 with Bun's synthetic V8 heap limit (the decoder then rejected that limit, so trace_web_source_location always failed), analysis resource failures reported heap limits Bun does not enforce, the HAR decoder claimed a fixed 192 MiB heap, and doctor reported Bun's Node.js compatibility version as the Node version. Identify the runtime once from process.versions. Source-map reports record id bun with the Bun and Node.js compatibility versions and a null v8_heap_limit_bytes; the decoder checks the heap budget only for V8 reports and rejects a report from a runtime other than the launching one. The analysis worker reports a null heap limit under Bun, HAR limitations and failures omit heap sizes under Bun, and the doctor node check reports Bun and passes when its Node.js compatibility version is supported. On Windows under Bun, each helper's owned Job Object now carries a commit limit of its heap budget plus a measured 512 MiB Bun runtime reserve (704 MiB for the codec and HAR decoder, 1536 MiB for the default analyzer). The native addon accepts an optional page-aligned job memory limit on process_spawn, verifies it by readback, associates a private completion port before the child is created, and reports a sticky memoryLimitReached and peak job commit on each poll; inspect reports jobMemoryLimit. A requested limit with an artifact lacking the control refuses the launch, and non-Windows launchers refuse a limit. Reaching the limit is classified by the job notification rather than Bun's exit status: the analyzer fails the current source with a memory resource failure reporting worker_process_memory_limit_bytes, the codec returns a memory resource failure, and the HAR decoder returns its memory failure with process_memory_limit_bytes. Bun source-map Evidence records process_memory_limit_bytes, verified against the launcher. Node.js launches and outputs are unchanged. * fix(windows): drain memory notifications after observing exit --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
42 lines
1.7 KiB
TypeScript
42 lines
1.7 KiB
TypeScript
import { javaScriptRuntime } from "../domain/runtimeVersion.js";
|
|
|
|
const MIB = 1024 * 1024;
|
|
|
|
/**
|
|
* Commit reserved for Bun itself above a worker's heap budget.
|
|
*
|
|
* Measured on Windows x64 (Bun 1.4.2, 32 logical processors) as the peak Job
|
|
* Object commit of each REA worker on a trivial input: `bun -e 1` commits
|
|
* 52 MiB, while the source-map codec (278-368 MiB), HAR decoder (300-345 MiB)
|
|
* and JavaScript analysis worker (321-346 MiB) commit up to 368 MiB before any
|
|
* real workload because JavaScriptCore commits parsed and compiled module code
|
|
* and per-thread runtime state. 512 MiB covers that peak with about 40%
|
|
* margin for hosts with other processor counts or module layouts.
|
|
*/
|
|
export const BUN_WORKER_RUNTIME_RESERVE_BYTES = 512 * MIB;
|
|
|
|
/** Runtime facts that decide whether V8 heap flags bound a worker. */
|
|
export interface JavaScriptWorkerHost {
|
|
readonly platform: NodeJS.Platform;
|
|
readonly versions: {
|
|
readonly node: string;
|
|
readonly bun?: string | undefined;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Process commit limit for a JavaScript worker launched on `process.execPath`.
|
|
*
|
|
* Node.js enforces each worker's V8 heap flags, so it receives no process
|
|
* limit (null) and its launch is unchanged. Bun ignores those flags; on Windows
|
|
* the owned Job Object then enforces the worker's heap budget plus Bun's
|
|
* runtime reserve. Bun elsewhere has no owned enforcement and also gets null.
|
|
*/
|
|
export const javaScriptWorkerMemoryLimitBytes = (
|
|
heapBudgetMiB: number,
|
|
host: JavaScriptWorkerHost = process,
|
|
): number | null =>
|
|
host.platform === "win32" && javaScriptRuntime(host.versions).id === "bun"
|
|
? heapBudgetMiB * MIB + BUN_WORKER_RUNTIME_RESERVE_BYTES
|
|
: null;
|