@@ -137,6 +137,12 @@ function gatedChildCommand(gate: string): string {
137137 return `until [ -e ${ JSON . stringify ( gate ) } ]; do i=$((i+1)); [ $i -ge 200 ] && exit 0; sleep 0.05; done` ;
138138}
139139
140+ // One condition for every POSIX signal case below: dispatch disposition,
141+ // process-group `kill`, and a real SIGHUP's default action are POSIX semantics,
142+ // and `exec` refuses to start on a Windows host before claiming anything, so
143+ // these tests would only time out waiting for the claim there.
144+ const IS_WINDOWS = process . platform === 'win32' ;
145+
140146test ( 'holds the claim with the command label while the child runs and releases it on exit' , async ( ) => {
141147 const dirs = useDirs ( ) ;
142148 const observed = path . join ( dirs . stateDir , 'child-saw-claim.txt' ) ;
@@ -287,82 +293,88 @@ test('a foreign session acquisition is refused while the wrapper holds the devic
287293 assert . equal ( acquired . status , 'acquired' ) ;
288294} ) ;
289295
290- test ( 'forwards SIGTERM to the child and exits with the signal code after releasing' , async ( ) => {
291- const dirs = useDirs ( ) ;
292- const gotSignal = path . join ( dirs . stateDir , 'child-got-term.txt' ) ;
293- const grandchildGotSignal = path . join ( dirs . stateDir , 'grandchild-got-term.txt' ) ;
294- const childStarted = path . join ( dirs . stateDir , 'child-started.txt' ) ;
295- // An in-place emit reaches every SIGTERM listener in this worker, so pin that
296- // the wrapper starts from the worker's own baseline and returns to it.
297- const sigtermListenersBefore = process . listenerCount ( 'SIGTERM' ) ;
298- const running = runExec (
299- [
300- 'sh' ,
301- '-c' ,
302- // Arms the trap before announcing itself, then reports its own receipt of
303- // the forwarded signal, so the test only signals a child that can catch it.
304- // The background grandchild proves the forward reaches the whole process
305- // group: a build's toolchain, not just its driver, must die with the claim.
306- // Both loops are bounded so a lost forward ends them instead of leaking.
307- `trap 'echo got > ${ JSON . stringify ( gotSignal ) } ; exit 0' TERM; echo started > ${ JSON . stringify (
308- childStarted ,
309- ) } ; { trap 'echo got > ${ JSON . stringify ( grandchildGotSignal ) } ' TERM; j=0; while [ $j -lt 60 ]; do sleep 0.05; j=$((j+1)); done; } & i=0; while [ $i -lt 60 ]; do sleep 0.05; i=$((i+1)); done`,
310- ] ,
311- dirs ,
312- ) ;
313- await running . inventoryReady ( ) ;
314- await waitFor ( ( ) => fs . existsSync ( childStarted ) ) ;
315- assert . equal ( claimEntry ( ) ?. claim ?. session , 'exec:sh' ) ;
316- // Same in-place signal pattern as the takeover command's tests. The child
317- // exits 0, so only a forwarded SIGTERM can write the receipt, and only the
318- // wrapper's own signal bookkeeping can report 143 rather than 0. A swallowed
319- // group-kill failure would leave both receipts missing, so their presence
320- // also proves the group signal left this worker.
321- process . emit ( 'SIGTERM' ) ;
322- assert . equal ( await running . promise , 128 + 15 ) ;
323- assert . equal ( process . listenerCount ( 'SIGTERM' ) , sigtermListenersBefore ) ;
324- assert . equal ( fs . readFileSync ( gotSignal , 'utf8' ) . trim ( ) , 'got' ) ;
325- await waitFor ( ( ) => fs . existsSync ( grandchildGotSignal ) ) ;
326- assert . equal ( fs . readFileSync ( grandchildGotSignal , 'utf8' ) . trim ( ) , 'got' ) ;
327- assert . equal ( storedClaim ( dirs . claimsDir ) , undefined ) ;
328- } ) ;
329-
330- test ( 'a real SIGHUP stops the child group and releases the claim before the wrapper exits' , async ( ) => {
331- const dirs = useDirs ( ) ;
332- // Only a real signal exercises the wrapper's own default disposition, so this
333- // runs the wrapper as a real process like the SIGKILL case. Before the
334- // SIGHUP handler existed the wrapper died on its default action, leaving the
335- // claim pointing at a dead pid while the child kept running — so the release
336- // and child-liveness assertions below are the fail-before, not the exit code
337- // (a default-action death also reports failure there).
338- const hupFixture = path . join ( import . meta. dirname , 'exec-hup-fixture.ts' ) ;
339- const child = runCmdBackground ( process . execPath , [ '--experimental-strip-types' , hupFixture ] , {
340- cwd : process . cwd ( ) ,
341- env : { ...process . env , EXEC_TEST_DIRS : JSON . stringify ( dirs ) } ,
342- captureOutput : false ,
343- stdio : 'ignore' ,
344- allowFailure : true ,
345- } ) ;
346- try {
347- await waitFor ( ( ) => claimEntry ( ) ?. claim ?. session === 'exec:sh' ) ;
348- const childPidFile = path . join ( dirs . stateDir , 'hup-child.pid' ) ;
349- await waitFor ( ( ) => fs . existsSync ( childPidFile ) ) ;
350- const childPid = Number ( fs . readFileSync ( childPidFile , 'utf8' ) ) ;
351- assert . ok ( Number . isInteger ( childPid ) && childPid > 0 ) ;
352-
353- child . child . kill ( 'SIGHUP' ) ;
354- const exited = await child . wait ;
355- // The rule: the wrapper outlives the signal, waits for the child, and the
356- // claim is already gone once its exit is observable.
296+ test . skipIf ( IS_WINDOWS ) (
297+ 'forwards SIGTERM to the child and exits with the signal code after releasing' ,
298+ async ( ) => {
299+ const dirs = useDirs ( ) ;
300+ const gotSignal = path . join ( dirs . stateDir , 'child-got-term.txt' ) ;
301+ const grandchildGotSignal = path . join ( dirs . stateDir , 'grandchild-got-term.txt' ) ;
302+ const childStarted = path . join ( dirs . stateDir , 'child-started.txt' ) ;
303+ // An in-place emit reaches every SIGTERM listener in this worker, so pin that
304+ // the wrapper starts from the worker's own baseline and returns to it.
305+ const sigtermListenersBefore = process . listenerCount ( 'SIGTERM' ) ;
306+ const running = runExec (
307+ [
308+ 'sh' ,
309+ '-c' ,
310+ // Arms the trap before announcing itself, then reports its own receipt of
311+ // the forwarded signal, so the test only signals a child that can catch it.
312+ // The background grandchild proves the forward reaches the whole process
313+ // group: a build's toolchain, not just its driver, must die with the claim.
314+ // Both loops are bounded so a lost forward ends them instead of leaking.
315+ `trap 'echo got > ${ JSON . stringify ( gotSignal ) } ; exit 0' TERM; echo started > ${ JSON . stringify (
316+ childStarted ,
317+ ) } ; { trap 'echo got > ${ JSON . stringify ( grandchildGotSignal ) } ' TERM; j=0; while [ $j -lt 60 ]; do sleep 0.05; j=$((j+1)); done; } & i=0; while [ $i -lt 60 ]; do sleep 0.05; i=$((i+1)); done`,
318+ ] ,
319+ dirs ,
320+ ) ;
321+ await running . inventoryReady ( ) ;
322+ await waitFor ( ( ) => fs . existsSync ( childStarted ) ) ;
323+ assert . equal ( claimEntry ( ) ?. claim ?. session , 'exec:sh' ) ;
324+ // Same in-place signal pattern as the takeover command's tests. The child
325+ // exits 0, so only a forwarded SIGTERM can write the receipt, and only the
326+ // wrapper's own signal bookkeeping can report 143 rather than 0. A swallowed
327+ // group-kill failure would leave both receipts missing, so their presence
328+ // also proves the group signal left this worker.
329+ process . emit ( 'SIGTERM' ) ;
330+ assert . equal ( await running . promise , 128 + 15 ) ;
331+ assert . equal ( process . listenerCount ( 'SIGTERM' ) , sigtermListenersBefore ) ;
332+ assert . equal ( fs . readFileSync ( gotSignal , 'utf8' ) . trim ( ) , 'got' ) ;
333+ await waitFor ( ( ) => fs . existsSync ( grandchildGotSignal ) ) ;
334+ assert . equal ( fs . readFileSync ( grandchildGotSignal , 'utf8' ) . trim ( ) , 'got' ) ;
357335 assert . equal ( storedClaim ( dirs . claimsDir ) , undefined ) ;
358- await waitFor ( ( ) => ! processAlive ( childPid ) ) ;
359- assert . equal ( exited . exitCode , 128 + 1 ) ;
360- } finally {
361- // The wrapper's child leads its own session, so no test-side group kill can
362- // reach it; the fixture bounds it with `sleep 8` instead.
363- child . child . kill ( 'SIGKILL' ) ;
364- }
365- } ) ;
336+ } ,
337+ ) ;
338+
339+ test . skipIf ( IS_WINDOWS ) (
340+ 'a real SIGHUP stops the child group and releases the claim before the wrapper exits' ,
341+ async ( ) => {
342+ const dirs = useDirs ( ) ;
343+ // Only a real signal exercises the wrapper's own default disposition, so this
344+ // runs the wrapper as a real process like the SIGKILL case. Before the
345+ // SIGHUP handler existed the wrapper died on its default action, leaving the
346+ // claim pointing at a dead pid while the child kept running — so the release
347+ // and child-liveness assertions below are the fail-before, not the exit code
348+ // (a default-action death also reports failure there).
349+ const hupFixture = path . join ( import . meta. dirname , 'exec-hup-fixture.ts' ) ;
350+ const child = runCmdBackground ( process . execPath , [ '--experimental-strip-types' , hupFixture ] , {
351+ cwd : process . cwd ( ) ,
352+ env : { ...process . env , EXEC_TEST_DIRS : JSON . stringify ( dirs ) } ,
353+ captureOutput : false ,
354+ stdio : 'ignore' ,
355+ allowFailure : true ,
356+ } ) ;
357+ try {
358+ await waitFor ( ( ) => claimEntry ( ) ?. claim ?. session === 'exec:sh' ) ;
359+ const childPidFile = path . join ( dirs . stateDir , 'hup-child.pid' ) ;
360+ await waitFor ( ( ) => fs . existsSync ( childPidFile ) ) ;
361+ const childPid = Number ( fs . readFileSync ( childPidFile , 'utf8' ) ) ;
362+ assert . ok ( Number . isInteger ( childPid ) && childPid > 0 ) ;
363+
364+ child . child . kill ( 'SIGHUP' ) ;
365+ const exited = await child . wait ;
366+ // The rule: the wrapper outlives the signal, waits for the child, and the
367+ // claim is already gone once its exit is observable.
368+ assert . equal ( storedClaim ( dirs . claimsDir ) , undefined ) ;
369+ await waitFor ( ( ) => ! processAlive ( childPid ) ) ;
370+ assert . equal ( exited . exitCode , 128 + 1 ) ;
371+ } finally {
372+ // The wrapper's child leads its own session, so no test-side group kill can
373+ // reach it; the fixture bounds it with `sleep 8` instead.
374+ child . child . kill ( 'SIGKILL' ) ;
375+ }
376+ } ,
377+ ) ;
366378
367379test ( 'refuses to run on a Windows host before touching the device or the claim store' , async ( ) => {
368380 const dirs = useDirs ( ) ;
@@ -387,38 +399,41 @@ test('refuses to run on a Windows host before touching the device or the claim s
387399 assert . equal ( storedClaim ( dirs . claimsDir ) , undefined ) ;
388400} ) ;
389401
390- test ( 'forwards a second signal when the child ignored the first, and keeps the first exit code' , async ( ) => {
391- const dirs = useDirs ( ) ;
392- const gotSignal = path . join ( dirs . stateDir , 'child-got-second-term.txt' ) ;
393- const childStarted = path . join ( dirs . stateDir , 'child-started.txt' ) ;
394- const sigintListenersBefore = process . listenerCount ( 'SIGINT' ) ;
395- const sigtermListenersBefore = process . listenerCount ( 'SIGTERM' ) ;
396- const running = runExec (
397- [
398- 'sh' ,
399- '-c' ,
400- // Traps INT without exiting, so the first signal is survivable and only
401- // the forwarded second one can end the child.
402- `trap '' INT; trap 'echo got > ${ JSON . stringify ( gotSignal ) } ; exit 0' TERM; echo started > ${ JSON . stringify (
403- childStarted ,
404- ) } ; i=0; while [ $i -lt 60 ]; do sleep 0.05; i=$((i+1)); done`,
405- ] ,
406- dirs ,
407- ) ;
408- await running . inventoryReady ( ) ;
409- await waitFor ( ( ) => fs . existsSync ( childStarted ) ) ;
410- process . emit ( 'SIGINT' ) ;
411- await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
412- assert . equal ( fs . existsSync ( gotSignal ) , false ) ;
413- process . emit ( 'SIGTERM' ) ;
414- // The first signal owns the exit code even though the second ended the child.
415- assert . equal ( await running . promise , 128 + 2 ) ;
416- // Both signals were emitted, so both handlers must be gone again.
417- assert . equal ( process . listenerCount ( 'SIGINT' ) , sigintListenersBefore ) ;
418- assert . equal ( process . listenerCount ( 'SIGTERM' ) , sigtermListenersBefore ) ;
419- assert . equal ( fs . readFileSync ( gotSignal , 'utf8' ) . trim ( ) , 'got' ) ;
420- assert . equal ( storedClaim ( dirs . claimsDir ) , undefined ) ;
421- } ) ;
402+ test . skipIf ( IS_WINDOWS ) (
403+ 'forwards a second signal when the child ignored the first, and keeps the first exit code' ,
404+ async ( ) => {
405+ const dirs = useDirs ( ) ;
406+ const gotSignal = path . join ( dirs . stateDir , 'child-got-second-term.txt' ) ;
407+ const childStarted = path . join ( dirs . stateDir , 'child-started.txt' ) ;
408+ const sigintListenersBefore = process . listenerCount ( 'SIGINT' ) ;
409+ const sigtermListenersBefore = process . listenerCount ( 'SIGTERM' ) ;
410+ const running = runExec (
411+ [
412+ 'sh' ,
413+ '-c' ,
414+ // Traps INT without exiting, so the first signal is survivable and only
415+ // the forwarded second one can end the child.
416+ `trap '' INT; trap 'echo got > ${ JSON . stringify ( gotSignal ) } ; exit 0' TERM; echo started > ${ JSON . stringify (
417+ childStarted ,
418+ ) } ; i=0; while [ $i -lt 60 ]; do sleep 0.05; i=$((i+1)); done`,
419+ ] ,
420+ dirs ,
421+ ) ;
422+ await running . inventoryReady ( ) ;
423+ await waitFor ( ( ) => fs . existsSync ( childStarted ) ) ;
424+ process . emit ( 'SIGINT' ) ;
425+ await new Promise ( ( resolve ) => setTimeout ( resolve , 50 ) ) ;
426+ assert . equal ( fs . existsSync ( gotSignal ) , false ) ;
427+ process . emit ( 'SIGTERM' ) ;
428+ // The first signal owns the exit code even though the second ended the child.
429+ assert . equal ( await running . promise , 128 + 2 ) ;
430+ // Both signals were emitted, so both handlers must be gone again.
431+ assert . equal ( process . listenerCount ( 'SIGINT' ) , sigintListenersBefore ) ;
432+ assert . equal ( process . listenerCount ( 'SIGTERM' ) , sigtermListenersBefore ) ;
433+ assert . equal ( fs . readFileSync ( gotSignal , 'utf8' ) . trim ( ) , 'got' ) ;
434+ assert . equal ( storedClaim ( dirs . claimsDir ) , undefined ) ;
435+ } ,
436+ ) ;
422437
423438test ( 'a SIGKILLed wrapper leaves a stale claim the next acquirer reconciles' , async ( ) => {
424439 const dirs = useDirs ( ) ;
0 commit comments