UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

122 lines (121 loc) • 3.75 kB
import { n as hasUnjoinedWork } from "./managed-child-process-DkJbQpWl.mjs"; import fs from "node:fs"; import { fileURLToPath } from "node:url"; import path, { extname } from "node:path"; import { randomUUID } from "node:crypto"; import { spawnSync } from "node:child_process"; import { setTimeout } from "node:timers/promises"; //#region scripts/lib/managed-memory-entrypoint.mts const managedMemoryEntrypoint = { currentModuleUrl: import.meta.url, sourceWorkerName: "managed-memory-launcher", sourceExtension: ".mts", distWorkerPath: "tooling/managed-memory-launcher.js" }; /** Resolve the standalone launcher in source checkouts and packaged tooling. */ function resolveManagedMemoryEntrypointUrl() { const e = new URL(import.meta.url); const t = e.pathname.lastIndexOf("/dist/"); return t < 0 ? new URL(`./managed-memory-launcher${extname(fileURLToPath(e))}`, e) : new URL(e.pathname.slice(0, t + 6) + managedMemoryEntrypoint.distWorkerPath, e); } //#endregion //#region scripts/lib/managed-memory.mts /** systemd owns the cgroup; the managed child owner still owns signals and output. */ async function runLinuxMemoryCommand(e, t) { const { memoryLimitBytes: n, onMemoryScope: r, ...i } = e; const a = "openclaw-check-" + randomUUID() + ".scope"; e.signal?.throwIfAborted(); const control = (t) => spawnSync("systemctl", [ "--user", ...t, a ], { encoding: "utf8", timeout: 5e3, killSignal: "SIGKILL", env: e.env, stdio: [ "ignore", "pipe", "pipe" ] }); const o = control(["show", "--property=LoadState"]); if (o.error || !o.stdout?.includes("LoadState=not-found")) throw new Error("[memory] A cgroup-v2 systemd user manager is required. Use a bounded Crabbox worker."); r?.(a); e.signal?.throwIfAborted(); const s = { ...e.env ?? process.env }; s.OPENCLAW_MANAGED_NODE_OPTIONS = s.NODE_OPTIONS ?? ""; delete s.NODE_OPTIONS; const cleanupScope = async (e) => { control([ "kill", "--kill-whom=all", "--signal=SIGKILL" ]); control(["stop"]); const t = Date.now() + 5e3; let n; do { const e = control([ "show", "--property=LoadState", "--property=ActiveState", "--property=ControlGroup" ]); const t = Object.fromEntries((e.stdout ?? "").trim().split("\n").map((e) => e.split("="))); n = !e.error && t.LoadState === "not-found"; if (!e.error && t.ControlGroup?.endsWith("/" + a)) try { n = /^populated 0$/mu.test(fs.readFileSync(path.join("/sys/fs/cgroup", t.ControlGroup, "cgroup.events"), "utf8")); } catch (e) { n = Boolean(e && typeof e === "object" && "code" in e && e.code === "ENOENT" && t.ActiveState === "inactive"); } if (n) break; await setTimeout(50); } while (Date.now() < t); if (!n) throw Object.assign(new Error("Memory scope cleanup could not be verified: " + a, { cause: e }), { code: "EPROCESSGROUP_CLEANUP_FAILED", processTreeState: "indeterminate" }); if (hasUnjoinedWork(e)) throw Object.assign(new Error(e instanceof Error ? e.message : String(e)), { code: "EPROCESSGROUP_CLEANUP_FAILED", processTreeState: "terminated" }); }; let c; try { return await t({ ...i, requireProcessTreeExit: true, bin: "systemd-run", shell: false, env: s, args: [ "--user", "--scope", "--collect", "--quiet", "--expand-environment=no", "--unit=" + a, "--property=MemoryMax=" + n, "--property=MemorySwapMax=0", "--property=OOMPolicy=kill", "--", process.execPath, fileURLToPath(resolveManagedMemoryEntrypointUrl()), String(n), a, String(e.shell ?? false), e.bin, ...e.args ?? [] ] }); } catch (e) { c = e; throw e; } finally { await cleanupScope(c); } } //#endregion export { runLinuxMemoryCommand };