@@ -333,6 +333,7 @@ export function scheduleRestartAbortedMainSessionRecovery(params: {
333333 maxRetries ?: number ;
334334 shouldContinue ?: ( ) => boolean ;
335335 stateDir ?: string ;
336+ waitForStart ?: ( ) => Promise < void > ;
336337 gatewayRuntime : GatewayRecoveryRuntime ;
337338} ) : { stop : ( ) => Promise < void > } {
338339 const initialDelay = params . delayMs ?? DEFAULT_RECOVERY_DELAY_MS ;
@@ -343,12 +344,16 @@ export function scheduleRestartAbortedMainSessionRecovery(params: {
343344 let timer : ReturnType < typeof setTimeout > | undefined ;
344345 let queuedAttempt : Promise < void > | undefined ;
345346 let activeAttempt : Promise < void > | undefined ;
347+ let cancelStartWait : ( ( ) => void ) | undefined ;
348+ const startWaitCancelled = new Promise < void > ( ( resolve ) => {
349+ cancelStartWait = resolve ;
350+ } ) ;
346351 const shouldContinue = ( ) =>
347352 ! stopped &&
348353 params . shouldContinue ?.( ) !== false &&
349354 isAgentEventLifecycleGenerationCurrent ( lifecycleGeneration ) ;
350- // Only reconcile rows that existed before this startup recovery was scheduled.
351- // Fresh runs started by this gateway are protected again by the active-run check .
355+ // Capture the cutoff at registration, before any startup gate can release new
356+ // work. Sessions created by this gateway must never become recovery candidates .
352357 const startupRecoveryCutoffMs = Date . now ( ) ;
353358
354359 const runRecoveryAttempt = ( attempt : number , delay : number ) => {
@@ -436,28 +441,36 @@ export function scheduleRestartAbortedMainSessionRecovery(params: {
436441 activeAttempt = trackedAttempt ;
437442 } ;
438443
444+ const queueRecoveryAttempt = ( attempt : number , delay : number ) => {
445+ const pendingStart = Promise . resolve ( ) . then ( async ( ) => {
446+ if ( attempt === 1 && params . waitForStart ) {
447+ // Shutdown must cancel an unresolved startup gate so failed startup and
448+ // same-port replacement cannot hang while joining this lifetime owner.
449+ await Promise . race ( [ params . waitForStart ( ) , startWaitCancelled ] ) ;
450+ }
451+ if ( shouldContinue ( ) ) {
452+ runRecoveryAttempt ( attempt , delay ) ;
453+ }
454+ } ) ;
455+ const trackedStart = pendingStart . finally ( ( ) => {
456+ if ( queuedAttempt === trackedStart ) {
457+ queuedAttempt = undefined ;
458+ }
459+ } ) ;
460+ queuedAttempt = trackedStart ;
461+ } ;
462+
439463 const scheduleAttempt = ( attempt : number , delay : number ) => {
440464 if ( ! shouldContinue ( ) ) {
441465 return ;
442466 }
443467 if ( delay <= 0 ) {
444- // Publish the cancellable handle before immediate startup can claim a session.
445- const pendingStart = Promise . resolve ( ) . then ( ( ) => {
446- if ( shouldContinue ( ) ) {
447- runRecoveryAttempt ( attempt , delay ) ;
448- }
449- } ) ;
450- const trackedStart = pendingStart . finally ( ( ) => {
451- if ( queuedAttempt === trackedStart ) {
452- queuedAttempt = undefined ;
453- }
454- } ) ;
455- queuedAttempt = trackedStart ;
468+ queueRecoveryAttempt ( attempt , delay ) ;
456469 return ;
457470 }
458471 timer = setTimeout ( ( ) => {
459472 timer = undefined ;
460- runRecoveryAttempt ( attempt , delay ) ;
473+ queueRecoveryAttempt ( attempt , delay ) ;
461474 } , delay ) ;
462475 timer . unref ?.( ) ;
463476 } ;
@@ -468,6 +481,7 @@ export function scheduleRestartAbortedMainSessionRecovery(params: {
468481 // Restart recovery belongs to its startup generation; stale timers must
469482 // never claim a session after that gateway begins draining.
470483 stopped = true ;
484+ cancelStartWait ?.( ) ;
471485 if ( timer ) {
472486 clearTimeout ( timer ) ;
473487 timer = undefined ;
0 commit comments