UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

84 lines (83 loc) • 3.07 kB
import { u as readChildRuntimeViability } from "./child-runtime-viability-DFyU3D5D.mjs"; import { a as raiseChildOomScore, i as isWrapped, n as OOM_SCORE_WRAP_SCRIPT, r as OOM_SCORE_WRAP_SHELL, s as shouldAdjustChildOomScore, t as OOM_SCORE_RESTORE_EXEC_ENV_SCRIPT } from "./linux-oom-score-spawn-yS50xku9.mjs"; import fs from "node:fs"; //#region src/process/linux-oom-score.ts /** * Linux children inherit the Gateway's OOM score, leaving the larger Gateway * vulnerable under memory pressure (#70404). Raise children to 1000 through a * shell that execs the command, or after native spawn when argv0 must be retained. * Raising a same-user child's score needs no CAP_SYS_RESOURCE. * OPENCLAW_CHILD_OOM_SCORE_ADJ=0 (also false/no/off) opts out per child or process. */ const SHELL_INIT_ENV_CARRIERS = [ ["BASH_ENV", "OC_INTERNAL_OOM_EXEC_BASH_ENV"], ["ENV", "OC_INTERNAL_OOM_EXEC_ENV"], ["CDPATH", "OC_INTERNAL_OOM_EXEC_CDPATH"], ["PS4", "OC_INTERNAL_OOM_EXEC_PS4"] ]; const NON_RESTORABLE_BASH_ENV_KEY = /^(?:SHELLOPTS|BASHOPTS|BASH_FUNC_.*)$/u; let cachedShellAvailable = null; function defaultShellAvailable() { if (cachedShellAvailable !== null) return cachedShellAvailable; try { cachedShellAvailable = fs.statSync(OOM_SCORE_WRAP_SHELL).isFile(); } catch { cachedShellAvailable = false; } return cachedShellAvailable; } function hardenShellEnv(e) { const t = { ...e ?? process.env }; for (const [e] of SHELL_INIT_ENV_CARRIERS) delete t[e]; for (const e of Object.keys(t)) if (NON_RESTORABLE_BASH_ENV_KEY.test(e)) delete t[e]; return t; } function prepareOomScoreAdjustedSpawnWithExecEnvPolicy(e, t = [], n, r = false) { const i = [...t]; const a = { command: e, args: i, ...n?.argv0 === void 0 ? {} : { argv0: n.argv0 }, env: n?.env, wrapped: false }; if (!e || e.startsWith("-") || !shouldAdjustChildOomScore(n) || e === process.execPath && !readChildRuntimeViability().available) return a; if (n?.argv0 !== void 0 && n.argv0 !== e) return { ...a, afterSpawn: raiseChildOomScore }; if (!(n?.shellAvailable ?? defaultShellAvailable)()) return a; if (!r && isWrapped(e, i)) return { command: e, args: i, env: hardenShellEnv(n?.env), wrapped: true }; const o = n?.env ?? process.env; if (r && Object.entries(o).some(([e, t]) => t !== void 0 && NON_RESTORABLE_BASH_ENV_KEY.test(e))) return a; const s = hardenShellEnv(o); if (r) for (const [e, t] of SHELL_INIT_ENV_CARRIERS) { if (o[t] !== void 0) return a; delete s[t]; if (o[e] !== void 0) s[t] = o[e]; } return { command: OOM_SCORE_WRAP_SHELL, args: [ "-c", r ? OOM_SCORE_RESTORE_EXEC_ENV_SCRIPT : OOM_SCORE_WRAP_SCRIPT, e, ...i ], env: s, wrapped: true }; } function prepareOomScoreAdjustedSpawn(e, t = [], n) { return prepareOomScoreAdjustedSpawnWithExecEnvPolicy(e, t, n); } function prepareOomScoreAdjustedSpawnPreservingExecEnv(e, t = [], n) { return prepareOomScoreAdjustedSpawnWithExecEnvPolicy(e, t, n, true); } //#endregion export { prepareOomScoreAdjustedSpawnPreservingExecEnv as n, prepareOomScoreAdjustedSpawn as t };