openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 2.48 kB
JavaScript
import{n as e,t}from"./service-child-group-anchor-errno-CsW5NI8i.mjs";import{i as n}from"./service-child-group-anchor-cli-invocation-DMTcSLIl.mjs";import{readFileSync as r,readdirSync as i}from"node:fs";import{becomeChildSubreaper as a,inspectChildWaitState as o,isChildSubreaper as s,reapChild as c}from"@openclaw/proc-safe/reaper";function childPids(){let t=/* @__PURE__ */ new Set,n=!1;for(let a of i(`/proc/self/task`)){let i;try{i=r(`/proc/self/task/`+a+`/children`,`utf8`),n=!0}catch(t){e(t);continue}for(let e of i.trim().split(/\s+/u).filter(Boolean)){if(!/^\d+$/u.test(e)||!Number.isSafeInteger(Number(e))||Number(e)<=0)throw Error(`Linux process owner could not enumerate its children`);t.add(Number(e))}}return n?[...t]:childPidsFromProcStat()}function childPidsFromProcStat(){let e=[],n=!1;for(let a of i(`/proc`)){if(!/^\d+$/u.test(a))continue;let i;try{i=r(`/proc/`+a+`/stat`,`utf8`)}catch(e){if(Number(a)!==process.pid&&(t(e,`ENOENT`)||t(e,`ESRCH`)))continue;throw e}n=!0;let[,o]=i.slice(i.lastIndexOf(`)`)+2).split(` `),s=Number(o);if(!/^\d+$/u.test(o??``)||!Number.isSafeInteger(s))throw Error(`Linux process owner could not enumerate its children`);s===process.pid&&e.push(Number(a))}if(!n)throw Error(`Linux process owner could not enumerate its children`);return e}function acquireLinuxChildSubreaper(){if(process.platform!==`linux`)throw Error(`Linux child ownership requires Linux`);if(n())throw Error(`Linux child ownership requires a process owner without source-loader hooks`);if(a(),!s())throw Error(`Linux child ownership admission verification failed`);let e=/* @__PURE__ */ new Set,r=/* @__PURE__ */ new Map,i=!1,retainSpawnedChild=(t,n)=>{if(n.pid!==t||!Number.isSafeInteger(t)||t<=0)throw Error(`Linux child ownership requires a spawned root`);e.add(t),i||=(process.on(`SIGCHLD`,reapExitedChildren),!0),n.once(`exit`,()=>{e.delete(t),r.delete(t)})};if(childPids().length>0)throw Error(`Linux child ownership requires a dedicated owner without existing children`);let l=!1,u={retainSpawnedChild,children:childPids,drain(n){if(l)return!0;for(let i of childPids()){let a=o(i);if(a.kind===`none`)continue;let s=r.get(i);if(a.kind===`running`&&n&&s!==n&&s!==`SIGKILL`){try{process.kill(i,n)}catch(e){if(!t(e,`ESRCH`))throw e}r.set(i,n)}a.kind===`exited`&&!e.has(i)&&c(i)&&r.delete(i)}return o().kind===`none`&&(l=!0,process.off(`SIGCHLD`,reapExitedChildren),!0)}},reapExitedChildren=()=>{try{u.drain()}catch{}};return u}export{acquireLinuxChildSubreaper};