@@ -21,7 +21,10 @@ import {
2121 type WindowsListeningPidsResult ,
2222} from "./windows-port-pids.js" ;
2323
24- const SPAWN_TIMEOUT_MS = 2000 ;
24+ // macOS lsof needs seconds on hosts with many mounted volumes; keep that
25+ // allowance separate so process and ancestor probes retain their tighter bound.
26+ const INITIAL_LSOF_TIMEOUT_MS = 5000 ;
27+ const PROCESS_INSPECTION_TIMEOUT_MS = 2000 ;
2528const STALE_SIGTERM_WAIT_MS = 600 ;
2629const STALE_SIGKILL_WAIT_MS = 400 ;
2730/**
@@ -31,7 +34,7 @@ const STALE_SIGKILL_WAIT_MS = 400;
3134 * `systemctl restart`). Without this wait the new process races the dying
3235 * process for the port and systemd enters an EADDRINUSE restart loop.
3336 *
34- * POLL_SPAWN_TIMEOUT_MS is intentionally much shorter than SPAWN_TIMEOUT_MS
37+ * POLL_SPAWN_TIMEOUT_MS is intentionally much shorter than the initial scan
3538 * so that a single slow or hung lsof invocation does not consume the entire
3639 * polling budget. At 400 ms per call, up to five independent lsof attempts
3740 * fit within PORT_FREE_TIMEOUT_MS = 2000 ms, each with a definitive outcome.
@@ -154,7 +157,9 @@ function readParentPidFromPs(pid: number, spawnTimeoutMs: number): number | null
154157 * `/proc/<pid>/status` payloads) — there is no reachable override for
155158 * runtime callers to mutate.
156159 */
157- export function getSelfAndAncestorPidsSync ( spawnTimeoutMs = SPAWN_TIMEOUT_MS ) : Set < number > {
160+ export function getSelfAndAncestorPidsSync (
161+ spawnTimeoutMs = PROCESS_INSPECTION_TIMEOUT_MS ,
162+ ) : Set < number > {
158163 const pids = new Set < number > ( [ process . pid ] ) ;
159164 const immediateParent = getParentPid ( ) ;
160165 if ( ! Number . isFinite ( immediateParent ) || immediateParent <= 0 ) {
@@ -202,7 +207,7 @@ function getExcludedGatewayPidsSync(spawnTimeoutMs: number, protectedPid?: numbe
202207 * `MAX_ANCESTOR_WALK_DEPTH` entries from `/proc/<pid>/status`; each read is
203208 * a virtual-filesystem access (no disk I/O, no external process), wrapped
204209 * in try/catch and degrades silently. On macOS the lookup shells out to `ps`
205- * with the caller's spawn timeout. Windows only uses the in-memory direct
210+ * with the process-inspection timeout. Windows only uses the in-memory direct
206211 * parent from `process.ppid`.
207212 */
208213function parseLsofEntries ( stdout : string ) : Array < { pid : number ; cmd ?: string } > {
@@ -295,7 +300,7 @@ function parsePidsFromLsofOutput(
295300 * `getSelfAndAncestorPidsSync`).
296301 */
297302function filterVerifiedWindowsGatewayPids ( rawPids : number [ ] , protectedPid ?: number ) : number [ ] {
298- const excluded = getExcludedGatewayPidsSync ( SPAWN_TIMEOUT_MS , protectedPid ) ;
303+ const excluded = getExcludedGatewayPidsSync ( PROCESS_INSPECTION_TIMEOUT_MS , protectedPid ) ;
299304 return uniqueValues ( rawPids )
300305 . filter ( ( pid ) => Number . isFinite ( pid ) && pid > 0 && ! excluded . has ( pid ) )
301306 . filter ( ( pid ) => {
@@ -309,7 +314,7 @@ function filterVerifiedWindowsGatewayPidsResult(
309314 processArgsResult : ( pid : number ) => WindowsProcessArgsResult ,
310315 protectedPid ?: number ,
311316) : WindowsListeningPidsResult {
312- const excluded = getExcludedGatewayPidsSync ( SPAWN_TIMEOUT_MS , protectedPid ) ;
317+ const excluded = getExcludedGatewayPidsSync ( PROCESS_INSPECTION_TIMEOUT_MS , protectedPid ) ;
313318 const verified : number [ ] = [ ] ;
314319 for ( const pid of uniqueValues ( rawPids ) ) {
315320 if ( ! Number . isFinite ( pid ) || pid <= 0 || excluded . has ( pid ) ) {
@@ -364,7 +369,8 @@ function findVerifiedWindowsGatewayPidsOnPortResultSync(
364369
365370function findGatewayPidsOnPortWithProtectedPidSync (
366371 port : number ,
367- spawnTimeoutMs : number ,
372+ lsofTimeoutMs : number ,
373+ processInspectionTimeoutMs : number ,
368374 options ?: CleanStaleGatewayProcessesOptions ,
369375) : number [ ] {
370376 if ( process . platform === "win32" ) {
@@ -375,7 +381,7 @@ function findGatewayPidsOnPortWithProtectedPidSync(
375381 const lsof = resolveLsofCommandSync ( ) ;
376382 const res = spawnSync ( lsof , [ "-nP" , `-iTCP:${ port } ` , "-sTCP:LISTEN" , "-Fpc" ] , {
377383 encoding : "utf8" ,
378- timeout : spawnTimeoutMs ,
384+ timeout : lsofTimeoutMs ,
379385 } ) ;
380386 if ( res . error ) {
381387 const code = ( res . error as NodeJS . ErrnoException ) . code ;
@@ -404,7 +410,7 @@ function findGatewayPidsOnPortWithProtectedPidSync(
404410 }
405411 return parsePidsFromLsofOutput (
406412 res . stdout ,
407- spawnTimeoutMs ,
413+ processInspectionTimeoutMs ,
408414 resolveProtectedPidAfterEnumeration ( options ) ,
409415 ) ;
410416}
@@ -413,11 +419,14 @@ function findGatewayPidsOnPortWithProtectedPidSync(
413419 * Find PIDs of gateway processes listening on the given port using synchronous lsof.
414420 * Returns only PIDs that belong to openclaw gateway processes (not the current process).
415421 */
416- export function findGatewayPidsOnPortSync (
417- port : number ,
418- spawnTimeoutMs = SPAWN_TIMEOUT_MS ,
419- ) : number [ ] {
420- return findGatewayPidsOnPortWithProtectedPidSync ( port , spawnTimeoutMs ) ;
422+ export function findGatewayPidsOnPortSync ( port : number , spawnTimeoutMs ?: number ) : number [ ] {
423+ // An explicit timeout keeps the existing contract: it bounds every child
424+ // process used by this probe. Only the default path splits slow lsof from ps.
425+ return findGatewayPidsOnPortWithProtectedPidSync (
426+ port ,
427+ spawnTimeoutMs ?? INITIAL_LSOF_TIMEOUT_MS ,
428+ spawnTimeoutMs ?? PROCESS_INSPECTION_TIMEOUT_MS ,
429+ ) ;
421430}
422431
423432/**
@@ -657,7 +666,12 @@ export function cleanStaleGatewayProcessesSync(
657666 waitForPortFreeSync ( port ) ;
658667 return [ ] ;
659668 } ) ( )
660- : findGatewayPidsOnPortWithProtectedPidSync ( port , SPAWN_TIMEOUT_MS , options ) ;
669+ : findGatewayPidsOnPortWithProtectedPidSync (
670+ port ,
671+ INITIAL_LSOF_TIMEOUT_MS ,
672+ PROCESS_INSPECTION_TIMEOUT_MS ,
673+ options ,
674+ ) ;
661675 if ( stalePids . length === 0 ) {
662676 return [ ] ;
663677 }
0 commit comments