22import { spawnSync } from "node:child_process" ;
33import path from "node:path" ;
44import { pathToFileURL } from "node:url" ;
5- import { describe , expect , it } from "vitest" ;
5+ import { expect , it } from "vitest" ;
66
77const WATCHDOG_DELAY_MS = 100 ;
88const CHILD_TIMEOUT_MS = 3_000 ;
99const watchdogModuleUrl = pathToFileURL (
1010 path . resolve ( "src/cli/gateway-cli/shutdown-hard-exit.ts" ) ,
1111) . href ;
1212
13- function runWatchdogChild ( source : string ) {
13+ function runWatchdogChild ( ) {
1414 const script = `
15+ import { writeSync } from "node:fs";
1516 import { armShutdownHardExitWatchdog } from ${ JSON . stringify ( watchdogModuleUrl ) } ;
16- ${ source }
17+
18+ const fail = (message) => {
19+ writeSync(2, \`error:\${message}\\n\`);
20+ process.exit(2);
21+ };
22+ const firstWatchdog = armShutdownHardExitWatchdog({
23+ delayMs: ${ WATCHDOG_DELAY_MS } ,
24+ onError: (error) => fail(String(error)),
25+ });
26+ if (firstWatchdog === null) {
27+ fail("first-watchdog-not-armed");
28+ }
29+ firstWatchdog.cancel();
30+ await new Promise((resolve) => setTimeout(resolve, ${ WATCHDOG_DELAY_MS * 2 } ));
31+ writeSync(1, "cancelled-watchdog-deadline-survived\\n");
32+
33+ process.once("beforeExit", () => {
34+ // Reaching beforeExit proves the cancelled worker no longer retains process liveness.
35+ writeSync(1, "cancelled-watchdog-released-liveness\\n");
36+ const secondWatchdog = armShutdownHardExitWatchdog({
37+ delayMs: ${ WATCHDOG_DELAY_MS } ,
38+ onError: (error) => fail(String(error)),
39+ });
40+ if (secondWatchdog === null) {
41+ fail("second-watchdog-not-armed");
42+ }
43+ writeSync(1, "second-watchdog-armed\\n");
44+ while (true) {}
45+ });
1746 ` ;
1847 const startedAt = Date . now ( ) ;
1948 const result = spawnSync (
@@ -30,38 +59,34 @@ function runWatchdogChild(source: string) {
3059 return { elapsedMs : Date . now ( ) - startedAt , result } ;
3160}
3261
33- describe ( "shutdown hard-exit watchdog" , ( ) => {
34- it ( "kills a process whose main thread is synchronously blocked" , ( ) => {
35- const { elapsedMs, result } = runWatchdogChild ( `
36- armShutdownHardExitWatchdog({
37- delayMs: ${ WATCHDOG_DELAY_MS } ,
38- onError: (error) => console.error(error),
39- });
40- while (true) {}
41- ` ) ;
42-
43- expect ( result . error ) . toBeUndefined ( ) ;
44- expect ( result . signal ) . toBe ( "SIGKILL" ) ;
45- expect ( elapsedMs ) . toBeLessThan ( CHILD_TIMEOUT_MS ) ;
46- } ) ;
47-
48- it ( "lets a cancelled process survive beyond the deadline and exit cleanly" , ( ) => {
49- const { elapsedMs, result } = runWatchdogChild ( `
50- const watchdog = armShutdownHardExitWatchdog({
51- delayMs: ${ WATCHDOG_DELAY_MS } ,
52- onError: (error) => {
53- console.error(error);
54- process.exitCode = 2;
55- },
56- });
57- watchdog?.cancel();
58- await new Promise((resolve) => setTimeout(resolve, ${ WATCHDOG_DELAY_MS * 2 } ));
59- ` ) ;
62+ it ( "cancels one watchdog before another kills a blocked main thread" , ( ) => {
63+ const { elapsedMs, result } = runWatchdogChild ( ) ;
64+ const diagnostics = JSON . stringify (
65+ {
66+ elapsedMs,
67+ status : result . status ,
68+ signal : result . signal ,
69+ error : result . error ? String ( result . error ) : undefined ,
70+ stdout : result . stdout ,
71+ stderr : result . stderr ,
72+ } ,
73+ null ,
74+ 2 ,
75+ ) ;
76+ const markers = [
77+ "cancelled-watchdog-deadline-survived" ,
78+ "cancelled-watchdog-released-liveness" ,
79+ "second-watchdog-armed" ,
80+ ] ;
81+ let previousMarkerIndex = - 1 ;
82+ for ( const marker of markers ) {
83+ const markerIndex = result . stdout . indexOf ( marker ) ;
84+ expect ( markerIndex , diagnostics ) . toBeGreaterThan ( previousMarkerIndex ) ;
85+ previousMarkerIndex = markerIndex ;
86+ }
6087
61- expect ( result . error ) . toBeUndefined ( ) ;
62- expect ( result . status ) . toBe ( 0 ) ;
63- expect ( result . signal ) . toBeNull ( ) ;
64- expect ( elapsedMs ) . toBeGreaterThanOrEqual ( WATCHDOG_DELAY_MS * 2 ) ;
65- expect ( elapsedMs ) . toBeLessThan ( CHILD_TIMEOUT_MS ) ;
66- } ) ;
88+ expect ( result . error , diagnostics ) . toBeUndefined ( ) ;
89+ expect ( result . status , diagnostics ) . toBeNull ( ) ;
90+ expect ( result . signal , diagnostics ) . toBe ( "SIGKILL" ) ;
91+ expect ( elapsedMs , diagnostics ) . toBeLessThan ( CHILD_TIMEOUT_MS ) ;
6792} ) ;
0 commit comments