@@ -136,6 +136,12 @@ const gatewayLog = {
136136 error : vi . fn ( ) ,
137137} ;
138138const flushLogger = vi . fn ( async ( ) => { } ) ;
139+ const cancelShutdownHardExitWatchdog = vi . fn ( ) ;
140+ const armShutdownHardExitWatchdog = vi . fn (
141+ ( _params : { delayMs : number ; onError : ( error : unknown ) => void } ) => ( {
142+ cancel : cancelShutdownHardExitWatchdog ,
143+ } ) ,
144+ ) ;
139145
140146vi . mock ( "../../infra/gateway-lock.js" , ( ) => ( {
141147 acquireGatewayLock : ( opts ?: { port ?: number } ) => acquireGatewayLock ( opts ) ,
@@ -255,6 +261,11 @@ vi.mock("../../gateway/server-reload-contracts.js", () => ({
255261 abortPendingChannelReloads : ( ) => abortPendingChannelReloads ( ) ,
256262} ) ) ;
257263
264+ vi . mock ( "./shutdown-hard-exit.js" , ( ) => ( {
265+ armShutdownHardExitWatchdog : ( params : { delayMs : number ; onError : ( error : unknown ) => void } ) =>
266+ armShutdownHardExitWatchdog ( params ) ,
267+ } ) ) ;
268+
258269const LOOP_SIGNALS = [ "SIGTERM" , "SIGINT" , "SIGUSR1" ] as const ;
259270type LoopSignal = ( typeof LOOP_SIGNALS ) [ number ] ;
260271const originalPlatformDescriptor = Object . getOwnPropertyDescriptor ( process , "platform" ) ;
@@ -383,6 +394,7 @@ function createSignaledStart(close: GatewayCloseFn, startupSettled = Promise.res
383394async function runLoopWithStart ( params : {
384395 start : ReturnType < typeof vi . fn > ;
385396 runtime : LoopRuntime ;
397+ ownsProcessLifecycle ?: boolean ;
386398 lockPort ?: number ;
387399 healthHost ?: string ;
388400 waitForHealthyChild ?: ( port : number , pid ?: number , host ?: string ) => Promise < boolean > ;
@@ -392,6 +404,7 @@ async function runLoopWithStart(params: {
392404 const loopPromise = runGatewayLoop ( {
393405 start : params . start as unknown as Parameters < typeof runGatewayLoop > [ 0 ] [ "start" ] ,
394406 runtime : params . runtime ,
407+ ownsProcessLifecycle : params . ownsProcessLifecycle ,
395408 lockPort : params . lockPort ,
396409 healthHost : params . healthHost ,
397410 waitForHealthyChild : params . waitForHealthyChild ,
@@ -628,6 +641,7 @@ describe("runGatewayLoop", () => {
628641 } ) ;
629642 expect ( runtime . exit ) . toHaveBeenCalledWith ( 0 ) ;
630643 expect ( flushLogger ) . toHaveBeenCalledOnce ( ) ;
644+ expect ( armShutdownHardExitWatchdog ) . not . toHaveBeenCalled ( ) ;
631645 } ) ;
632646 } ) ;
633647
@@ -2411,32 +2425,41 @@ describe("runGatewayLoop", () => {
24112425 await withIsolatedSignals ( async ( { captureSignal } ) => {
24122426 const { start, started } = createSignaledStart ( close ) ;
24132427 const { runtime, exited } = createRuntimeWithExitSignal ( ) ;
2414- await runLoopWithStart ( { start, runtime } ) ;
2428+ await runLoopWithStart ( { start, runtime, ownsProcessLifecycle : true } ) ;
24152429 await waitForStart ( started ) ;
24162430 const sigusr1 = captureSignal ( "SIGUSR1" ) ;
24172431
2418- sigusr1 ( ) ;
2419- await waitForLoopCondition (
2420- ( ) => close . mock . calls . length === 1 ,
2421- "restart close did not start" ,
2422- ) ;
2423- sigusr1 ( ) ;
2424- await waitForLoopCondition (
2425- ( ) =>
2426- gatewayLog . info . mock . calls . some ( ( [ message ] ) =>
2427- String ( message ) . includes ( "upgrading to update.auto" ) ,
2428- ) ,
2429- "accepted restart was not upgraded" ,
2430- ) ;
2432+ try {
2433+ sigusr1 ( ) ;
2434+ await waitForLoopCondition (
2435+ ( ) => close . mock . calls . length === 1 ,
2436+ "restart close did not start" ,
2437+ ) ;
2438+ expect ( armShutdownHardExitWatchdog ) . toHaveBeenCalledTimes ( 1 ) ;
2439+ sigusr1 ( ) ;
2440+ await waitForLoopCondition (
2441+ ( ) =>
2442+ gatewayLog . info . mock . calls . some ( ( [ message ] ) =>
2443+ String ( message ) . includes ( "upgrading to update.auto" ) ,
2444+ ) ,
2445+ "accepted restart was not upgraded" ,
2446+ ) ;
2447+ expect ( cancelShutdownHardExitWatchdog ) . toHaveBeenCalledTimes ( 1 ) ;
2448+ expect ( armShutdownHardExitWatchdog ) . toHaveBeenCalledTimes ( 2 ) ;
24312449
2432- releaseClose ( ) ;
2433- await expect ( exited ) . resolves . toBe ( 0 ) ;
2434- expect ( respawnGatewayProcessForUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
2435- expectRestartHandoffCall ( {
2436- restartKind : "update-process" ,
2437- reason : "update.auto" ,
2438- supervisorMode : "external" ,
2439- } ) ;
2450+ releaseClose ( ) ;
2451+ await expect ( exited ) . resolves . toBe ( 0 ) ;
2452+ expect ( cancelShutdownHardExitWatchdog ) . toHaveBeenCalledTimes ( 2 ) ;
2453+ expect ( respawnGatewayProcessForUpdate ) . toHaveBeenCalledTimes ( 1 ) ;
2454+ expectRestartHandoffCall ( {
2455+ restartKind : "update-process" ,
2456+ reason : "update.auto" ,
2457+ supervisorMode : "external" ,
2458+ } ) ;
2459+ } finally {
2460+ releaseClose ( ) ;
2461+ await exited ;
2462+ }
24402463 } ) ;
24412464 } finally {
24422465 if ( originalPlatformDescriptor ) {
0 commit comments