mirror of
https://github.com/morluto/rea.git
synced 2026-11-03 05:16:16 +00:00
fix(bun): report the real runtime and enforce worker memory budgets on Windows (#1730)
* 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>
This commit is contained in:
1 parent
d412def722
commit
66c21572de
48 files changed
+1630
-196
No files matched your search
@@ -5,6 +5,7 @@ import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.
|
||||
import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
import { spawnOwnedProviderProcess } from "../../../src/process/ProviderProcess.js";
|
||||
import { javaScriptWorkerMemoryLimitBytes } from "../../../src/process/JavaScriptWorkerMemoryLimit.js";
|
||||
import type { JavaScriptArtifactFile } from "../../../src/domain/javascript/javascriptArtifactFiles.js";
|
||||
|
||||
const source = (path: string, value: string): JavaScriptArtifactFile => ({
|
||||
@@ -18,6 +19,9 @@ const source = (path: string, value: string): JavaScriptArtifactFile => ({
|
||||
text: { included: true, value },
|
||||
});
|
||||
|
||||
/** The Job Object limit the worker requests for its 128 MiB heap budget, if any. */
|
||||
const processLimit = javaScriptWorkerMemoryLimitBytes(128);
|
||||
|
||||
it("reuses one real owned analyzer while retaining distinct source identities and exported returns", async () => {
|
||||
const launches: number[] = [];
|
||||
const worker = new JavaScriptAnalysisWorker(
|
||||
@@ -146,3 +150,131 @@ it("preserves a failed owned cleanup retry when the next caller cancels before a
|
||||
await next.close();
|
||||
}
|
||||
});
|
||||
|
||||
// The handshake mirrors the real server: only Node.js reports an enforced V8 heap limit.
|
||||
const resourceFailureWorker = (heapLimit: string) =>
|
||||
new JavaScriptAnalysisWorker({ heapMb: 128, timeoutMs: 30_000 }, (request) =>
|
||||
spawnOwnedProviderProcess({
|
||||
...request,
|
||||
arguments: [
|
||||
"-e",
|
||||
`const heap = ${heapLimit};
|
||||
process.stdout.write(JSON.stringify({ kind: "ready", heap_limit_bytes: heap }) + "\\n");
|
||||
require("node:readline").createInterface({ input: process.stdin }).once("line", (line) => {
|
||||
const { id } = JSON.parse(line);
|
||||
process.stdout.write(JSON.stringify({ kind: "failure", id, reason: "resource", resource: "memory", limits: null, message: "fixture" }) + "\\n");
|
||||
});`,
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
it("reports the worker heap limit only when the runtime enforces V8 heap flags", async () => {
|
||||
const worker = resourceFailureWorker(
|
||||
'process.versions.bun === undefined ? require("node:v8").getHeapStatistics().heap_size_limit : null',
|
||||
);
|
||||
try {
|
||||
const result = await worker.analyze(
|
||||
source("app.mjs", "export const observed = 7;"),
|
||||
100,
|
||||
);
|
||||
if (result.ok) throw new Error("expected a reported resource failure");
|
||||
expect(result.error.error).toMatchObject({
|
||||
_tag: "AnalysisResourceConstraintError",
|
||||
reportedLimits:
|
||||
process.versions.bun === undefined
|
||||
? {
|
||||
worker_old_space_mb: 128,
|
||||
worker_heap_limit_bytes: expect.any(Number),
|
||||
worker_timeout_ms: 30_000,
|
||||
}
|
||||
: {
|
||||
worker_heap_limit_bytes: null,
|
||||
...(processLimit === null
|
||||
? {}
|
||||
: { worker_process_memory_limit_bytes: processLimit }),
|
||||
worker_timeout_ms: 30_000,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
await worker.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects a worker heap report that contradicts the launching runtime", async () => {
|
||||
const worker = resourceFailureWorker(
|
||||
"process.versions.bun === undefined ? null : 1",
|
||||
);
|
||||
try {
|
||||
const result = await worker.analyze(
|
||||
source("app.mjs", "export const observed = 7;"),
|
||||
100,
|
||||
);
|
||||
if (result.ok) throw new Error("expected a handshake failure");
|
||||
expect(result.error.error._tag).toBe("AnalysisOutputError");
|
||||
} finally {
|
||||
await worker.close();
|
||||
}
|
||||
});
|
||||
|
||||
// The launcher runs a real child without a job limit, so any artifact can run
|
||||
// it, and its handle reports the notification an owned Windows job records
|
||||
// before exit. Bun exits 3 (panic) or 1 (exception) at a reached job limit.
|
||||
it.each([true, false])(
|
||||
"classifies a worker exit by the job memory notification (reached: %s)",
|
||||
async (reached) => {
|
||||
const worker = new JavaScriptAnalysisWorker(
|
||||
{ heapMb: 128, timeoutMs: 30_000 },
|
||||
async ({ memoryLimitBytes, ...request }) => {
|
||||
expect(memoryLimitBytes).toBe(processLimit ?? undefined);
|
||||
const spawned = await spawnOwnedProviderProcess({
|
||||
...request,
|
||||
arguments: [
|
||||
"-e",
|
||||
`const heap = process.versions.bun === undefined ? require("node:v8").getHeapStatistics().heap_size_limit : null;
|
||||
process.stdout.write(JSON.stringify({ kind: "ready", heap_limit_bytes: heap }) + "\\n");
|
||||
require("node:readline").createInterface({ input: process.stdin }).once("line", () => {
|
||||
process.stderr.write("panic: out of memory");
|
||||
process.exit(3);
|
||||
});`,
|
||||
],
|
||||
});
|
||||
let exited = false;
|
||||
spawned.process.once("exit", () => {
|
||||
exited = true;
|
||||
});
|
||||
Object.defineProperty(spawned.process, "memoryLimitReached", {
|
||||
get: () => reached && exited,
|
||||
});
|
||||
return spawned;
|
||||
},
|
||||
);
|
||||
try {
|
||||
const result = await worker.analyze(
|
||||
source("app.mjs", "export const observed = 7;"),
|
||||
100,
|
||||
);
|
||||
if (result.ok) throw new Error("expected the worker to fail");
|
||||
expect(result.error.error).toMatchObject(
|
||||
reached
|
||||
? {
|
||||
_tag: "AnalysisResourceConstraintError",
|
||||
resource: "memory",
|
||||
reason: expect.stringContaining("process memory limit"),
|
||||
reportedLimits:
|
||||
processLimit === null
|
||||
? expect.not.objectContaining({
|
||||
worker_process_memory_limit_bytes: expect.anything(),
|
||||
})
|
||||
: { worker_process_memory_limit_bytes: processLimit },
|
||||
capturedOutput: { stderr: "panic: out of memory" },
|
||||
}
|
||||
: {
|
||||
_tag: "ProviderAdapterError",
|
||||
diagnostics: { exit_code: 3 },
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await worker.close();
|
||||
}
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user