@@ -38,7 +38,7 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
3838 const cronEnabled = env . OPENCLAW_SKIP_CRON !== "1" && params . cfg . cron ?. enabled !== false ;
3939 let loaded : LoadedGatewayCronState | null = null ;
4040 let stopped = false ;
41- let preserveExitWatchersOnStop = false ;
41+ let exitWatcherHandoff : GatewayCronExitWatcherHandoff | undefined ;
4242 let exitWatcherHandoffStop : Promise < void > | undefined ;
4343 let lifecycleGeneration = 0 ;
4444 let schedulingPaused = false ;
@@ -88,17 +88,13 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
8888 return await cronStateLoader . load ( ) ;
8989 } ;
9090
91- const stopResolvedCron = async (
92- resolved : LoadedGatewayCronState ,
93- preserveExitWatchers : boolean ,
94- ) : Promise < void > => {
91+ const stopResolvedCron = async ( resolved : LoadedGatewayCronState ) : Promise < void > => {
9592 resolved . phase = "stopped" ;
9693 resolved . underlyingStarted = false ;
97- const handoff = preserveExitWatchers
98- ? await resolved . state . prepareExitWatcherHandoff ?.( )
99- : undefined ;
100- if ( handoff ) {
101- await handoff . stopOwner ( ) ;
94+ if ( exitWatcherHandoff ) {
95+ // A cancelled startup must join the exact prepared owner's drain, not
96+ // prepare or stop another owner after its watchers have been adopted.
97+ await ( exitWatcherHandoffStop ??= exitWatcherHandoff . stopOwner ( ) ) ;
10298 } else if ( resolved . state . cron . stopAndDrain ) {
10399 await resolved . state . cron . stopAndDrain ( ) ;
104100 } else {
@@ -107,26 +103,15 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
107103 }
108104 } ;
109105
110- const stopResolvedCronOnce = (
111- resolved : LoadedGatewayCronState ,
112- preserveExitWatchers : boolean ,
113- ) : Promise < void > => {
114- if ( ! preserveExitWatchers ) {
115- return stopResolvedCron ( resolved , false ) ;
116- }
117- exitWatcherHandoffStop ??= stopResolvedCron ( resolved , true ) ;
118- return exitWatcherHandoffStop ;
119- } ;
120-
121- const stopLoadedCronAndDrain = async ( preserveExitWatchers = false ) : Promise < void > => {
106+ const stopLoadedCronAndDrain = async ( handoff ?: GatewayCronExitWatcherHandoff ) : Promise < void > => {
122107 stopped = true ;
123- preserveExitWatchersOnStop ||= preserveExitWatchers ;
108+ exitWatcherHandoff ??= handoff ;
124109 lifecycleGeneration += 1 ;
125110 releaseSchedulingResumeWaiters ( ) ;
126111 const loading = cronStateLoader . peek ( ) ;
127112 const resolved = loaded ?? ( loading ? await loading : null ) ;
128113 if ( resolved ) {
129- await stopResolvedCronOnce ( resolved , preserveExitWatchersOnStop ) ;
114+ await stopResolvedCron ( resolved ) ;
130115 }
131116 } ;
132117
@@ -184,7 +169,7 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
184169 resolved . underlyingStartInFlight = false ;
185170 }
186171 if ( startCancelled ( ) ) {
187- await stopResolvedCronOnce ( resolved , preserveExitWatchersOnStop ) ;
172+ await stopResolvedCron ( resolved ) ;
188173 return ;
189174 }
190175 if ( schedulingPaused ) {
@@ -205,7 +190,7 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
205190 throw err ;
206191 }
207192 if ( startCancelled ( ) ) {
208- await stopResolvedCronOnce ( resolved , preserveExitWatchersOnStop ) ;
193+ await stopResolvedCron ( resolved ) ;
209194 return ;
210195 }
211196 resolved . phase = "started" ;
@@ -354,7 +339,7 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
354339 return {
355340 ...handoff ,
356341 stopOwner : async ( ) => {
357- await stopLoadedCronAndDrain ( true ) ;
342+ await stopLoadedCronAndDrain ( handoff ) ;
358343 } ,
359344 } ;
360345 } ,
@@ -364,9 +349,6 @@ export function createLazyGatewayCronState(params: LazyGatewayCronParams): Gatew
364349 async reconcileExitWatchers ( ) {
365350 await ( await load ( ) ) . state . reconcileExitWatchers ( ) ;
366351 } ,
367- stopExitWatchers ( ) {
368- loaded ?. state . stopExitWatchers ( ) ;
369- } ,
370352 async reconcileStreamWatchers ( ) {
371353 await ( await load ( ) ) . state . reconcileStreamWatchers ( ) ;
372354 } ,
0 commit comments