@@ -62,6 +62,7 @@ const diagnosticFor = (reason: StopReason, detail?: string) => Buffer.from(
6262const retainCleanupOwnership = ( profile : ContainerProfile , detail : string , register = true ) : InvocationHandle => {
6363 const invocation = assertPhasePolicy ( profile . policy ) ;
6464 let resolveSettled ! : ( result : InvocationResult ) => void , cleaning = false , complete = false ;
65+ let cancelReason : StopReason | undefined ;
6566 let handle ! : InvocationHandle ;
6667 let timer : ReturnType < typeof setTimeout > | undefined ;
6768 const settled = new Promise < InvocationResult > ( resolve => { resolveSettled = resolve ; } ) ;
@@ -81,8 +82,8 @@ const retainCleanupOwnership = (profile: ContainerProfile, detail: string, regis
8182 activeProfiles . delete ( profile ) ;
8283 cleanupRecoveries . delete ( handle ) ;
8384 resolveSettled ( Object . freeze ( { attemptId : invocation . attemptId , context : invocation . context ,
84- exitCode : null , signal : null , stopReason : 'capture-failure' , stdout : '' ,
85- stderr : diagnosticFor ( 'capture-failure' , detail ) . toString ( 'utf8' ) } ) ) ;
85+ exitCode : null , signal : null , stopReason : cancelReason ?? 'capture-failure' , stdout : '' ,
86+ stderr : diagnosticFor ( cancelReason ?? 'capture-failure' , detail ) . toString ( 'utf8' ) } ) ) ;
8687 } catch {
8788 cleaning = false ;
8889 schedule ( ) ;
@@ -91,7 +92,12 @@ const retainCleanupOwnership = (profile: ContainerProfile, detail: string, regis
9192 cleaning = false ;
9293 } ;
9394 handle = Object . freeze ( { attemptId : invocation . attemptId , settled,
94- cancel : ( ) => { if ( timer ) clearTimeout ( timer ) ; timer = undefined ; retry ( ) ; } } ) ;
95+ cancel : ( reason : StopReason ) => {
96+ cancelReason ??= reason ;
97+ if ( timer ) clearTimeout ( timer ) ;
98+ timer = undefined ;
99+ retry ( ) ;
100+ } } ) ;
95101 activeProfiles . add ( profile ) ;
96102 if ( register ) active . set ( invocation . attemptId , handle ) ;
97103 else cleanupRecoveries . add ( handle ) ;
@@ -111,6 +117,7 @@ export function retainSetupCleanup(invocation: InvocationInput, retryCleanup: ()
111117 startupError : unknown , cleanupError : unknown , kind = 'setup cleanup' ) : InvocationHandle {
112118 const register = ! ownsAttempt ( invocation . attemptId ) ;
113119 let resolveSettled ! : ( result : InvocationResult ) => void , cleaning = false , complete = false ;
120+ let cancelReason : StopReason | undefined ;
114121 let handle ! : InvocationHandle ;
115122 let timer : ReturnType < typeof setTimeout > | undefined ;
116123 const settled = new Promise < InvocationResult > ( resolve => { resolveSettled = resolve ; } ) ;
@@ -126,8 +133,8 @@ export function retainSetupCleanup(invocation: InvocationInput, retryCleanup: ()
126133 if ( active . get ( invocation . attemptId ) === handle ) active . delete ( invocation . attemptId ) ;
127134 cleanupRecoveries . delete ( handle ) ;
128135 resolveSettled ( Object . freeze ( { attemptId : invocation . attemptId , context : invocation . context ,
129- exitCode : null , signal : null , stopReason : 'capture-failure' , stdout : '' ,
130- stderr : diagnosticFor ( 'capture-failure' , detail ) . toString ( 'utf8' ) } ) ) ;
136+ exitCode : null , signal : null , stopReason : cancelReason ?? 'capture-failure' , stdout : '' ,
137+ stderr : diagnosticFor ( cancelReason ?? 'capture-failure' , detail ) . toString ( 'utf8' ) } ) ) ;
131138 } catch {
132139 cleaning = false ;
133140 if ( ! timer ) {
@@ -138,7 +145,12 @@ export function retainSetupCleanup(invocation: InvocationInput, retryCleanup: ()
138145 cleaning = false ;
139146 } ;
140147 handle = Object . freeze ( { attemptId : invocation . attemptId , settled,
141- cancel : ( ) => { if ( timer ) clearTimeout ( timer ) ; timer = undefined ; retry ( ) ; } } ) ;
148+ cancel : ( reason : StopReason ) => {
149+ cancelReason ??= reason ;
150+ if ( timer ) clearTimeout ( timer ) ;
151+ timer = undefined ;
152+ retry ( ) ;
153+ } } ) ;
142154 if ( register ) active . set ( invocation . attemptId , handle ) ;
143155 else cleanupRecoveries . add ( handle ) ;
144156 timer = setTimeout ( ( ) => { timer = undefined ; retry ( ) ; } , 1_000 ) ;
0 commit comments