11import { spawn , spawnSync } from "node:child_process" ;
2+ import { randomUUID } from "node:crypto" ;
23import { once } from "node:events" ;
34import fs from "node:fs" ;
45import path from "node:path" ;
56import { setTimeout as delay } from "node:timers/promises" ;
67import { fileURLToPath } from "node:url" ;
7- import { getProcessStartTime , isPidAlive } from "../../../src/shared/pid-alive.ts" ;
8+ import { getProcessStartTime } from "../../../src/shared/pid-alive.ts" ;
89
910const [ mode , root , policyScenario , ...args ] = process . argv . slice ( 2 ) ;
1011const linux = policyScenario . startsWith ( "linux:" ) ;
1112const scenario = linux ? policyScenario . slice ( "linux:" . length ) : policyScenario ;
1213const fixture = fileURLToPath ( import . meta. url ) ;
14+ const instance = randomUUID ( ) ;
1315const workspace = path . join ( root , "workspace" ) ;
1416const lease = path . join ( root , "lease" ) ;
1517const recordsDir = path . join ( root , "pids" ) ;
@@ -41,7 +43,7 @@ function publish(name, value) {
4143}
4244
4345function record ( pid , role , attempt = 0 ) {
44- publish ( `pids/${ pid } .json` , { pid, role, attempt } ) ;
46+ publish ( `pids/${ pid } .json` , { pid, role, attempt, instance : ` ${ instance } - ${ pid } ` } ) ;
4547}
4648
4749function records ( ) {
@@ -54,29 +56,51 @@ function records() {
5456}
5557
5658function liveRecords ( ) {
57- const owned = records ( ) ;
58- if ( process . platform === "win32" ) {
59- return owned . filter ( ( entry ) => isPidAlive ( entry . pid ) ) ;
60- }
61- // PID existence includes macOS zombies; observe active writers in one POSIX snapshot.
62- const result = spawnSync ( "/bin/ps" , [ "-axo" , "pid=,stat=" ] , {
63- encoding : "utf8" ,
64- timeout : 1_000 ,
65- } ) ;
66- if ( result . error || result . status !== 0 ) {
67- throw new Error ( `Fixture process census failed (${ result . error ?. code ?? result . status } )` ) ;
68- }
59+ const owned = records ( ) . filter (
60+ ( entry ) => ! fs . existsSync ( path . join ( recordsDir , `${ entry . instance } .dead` ) ) ,
61+ ) ;
6962 const alive = new Set ( ) ;
70- for ( const line of result . stdout . trim ( ) . split ( "\n" ) ) {
71- const [ pid , state ] = line . trim ( ) . split ( / \s + / u) ;
72- if ( ! Number . isInteger ( Number ( pid ) ) || ! state ) {
73- throw new Error ( "Fixture process census returned an invalid row" ) ;
63+ if ( process . platform === "win32" ) {
64+ for ( const { pid } of owned ) {
65+ try {
66+ process . kill ( pid , 0 ) ;
67+ alive . add ( pid ) ;
68+ } catch ( error ) {
69+ if ( error . code === "EPERM" ) {
70+ alive . add ( pid ) ;
71+ } else if ( error . code !== "ESRCH" ) {
72+ throw error ;
73+ }
74+ }
75+ }
76+ } else {
77+ // PID existence includes macOS zombies; observe active writers in one POSIX snapshot.
78+ const result = spawnSync ( "/bin/ps" , [ "-axo" , "pid=,stat=" ] , {
79+ encoding : "utf8" ,
80+ timeout : 1_000 ,
81+ } ) ;
82+ if ( result . error || result . status !== 0 ) {
83+ throw new Error ( `Fixture process census failed (${ result . error ?. code ?? result . status } )` ) ;
7484 }
75- if ( ! state . startsWith ( "Z" ) ) {
76- alive . add ( Number ( pid ) ) ;
85+ for ( const line of result . stdout . trim ( ) . split ( "\n" ) ) {
86+ const [ pid , state ] = line . trim ( ) . split ( / \s + / u) ;
87+ if ( ! Number . isInteger ( Number ( pid ) ) || ! state ) {
88+ throw new Error ( "Fixture process census returned an invalid row" ) ;
89+ }
90+ if ( ! state . startsWith ( "Z" ) ) {
91+ alive . add ( Number ( pid ) ) ;
92+ }
7793 }
7894 }
79- return owned . filter ( ( entry ) => alive . has ( entry . pid ) ) ;
95+ return owned . filter ( ( entry ) => {
96+ if ( alive . has ( entry . pid ) ) {
97+ return true ;
98+ }
99+ // Separate command processes share this observed-dead fact. PID reuse cannot
100+ // revive that instance, while a newly registered instance is still checked.
101+ fs . writeFileSync ( path . join ( recordsDir , `${ entry . instance } .dead` ) , "" ) ;
102+ return false ;
103+ } ) ;
80104}
81105
82106function boundary ( name ) {
0 commit comments