@@ -183,18 +183,32 @@ export function createReplyOperation(params: {
183183 terminalSettleTimer . scheduleOnce ( REPLY_RUN_TERMINAL_SETTLE_TIMEOUT_MS ) ;
184184 } ;
185185
186- const abortWithReason = (
186+ const abortOperation = (
187187 reason : ReplyBackendCancelReason ,
188188 abortReason : unknown ,
189- opts ?: { abortedCode ? : ReplyOperationAbortCode } ,
189+ abortedCode : ReplyOperationAbortCode ,
190190 ) => {
191- if ( opts ?. abortedCode && ! result ) {
192- setResult ( { kind : "aborted" , code : opts . abortedCode } ) ;
191+ const phaseBeforeAbort = phase ;
192+ if ( ! result ) {
193+ setResult ( { kind : "aborted" , code : abortedCode } ) ;
193194 detachUpstreamAbort ( ) ;
194195 }
195196 phase = "aborted" ;
196197 abortInternally ( abortReason ) ;
197- getAttachedBackend ( operation ) ?. cancel ( reason ) ;
198+ // Cancellation may throw, but lifecycle cleanup still must run. Pre-backend
199+ // non-retained owners release now; retained/running owners await terminal settle.
200+ try {
201+ getAttachedBackend ( operation ) ?. cancel ( reason ) ;
202+ } finally {
203+ if (
204+ isReplyOperationPreBackendPhase ( phaseBeforeAbort ) &&
205+ ! retainStateUntilCompleteOperations . has ( operation )
206+ ) {
207+ clearState ( ) ;
208+ } else {
209+ scheduleTerminalSettle ( ) ;
210+ }
211+ }
198212 } ;
199213
200214 const operation : ReplyOperation = {
@@ -463,36 +477,14 @@ export function createReplyOperation(params: {
463477 if ( ! isReplyOperationAbortable ( operation ) ) {
464478 return false ;
465479 }
466- const phaseBeforeAbort = phase ;
467- abortWithReason ( "user_abort" , createUserAbortError ( ) , {
468- abortedCode : "aborted_by_user" ,
469- } ) ;
470- if (
471- isReplyOperationPreBackendPhase ( phaseBeforeAbort ) &&
472- ! retainStateUntilCompleteOperations . has ( operation )
473- ) {
474- clearState ( ) ;
475- } else {
476- scheduleTerminalSettle ( ) ;
477- }
480+ abortOperation ( "user_abort" , createUserAbortError ( ) , "aborted_by_user" ) ;
478481 return true ;
479482 } ,
480483 abortForRestart ( ) {
481484 if ( ! isReplyOperationAbortable ( operation ) ) {
482485 return false ;
483486 }
484- const phaseBeforeAbort = phase ;
485- abortWithReason ( "restart" , createAgentRunRestartAbortError ( ) , {
486- abortedCode : "aborted_for_restart" ,
487- } ) ;
488- if (
489- isReplyOperationPreBackendPhase ( phaseBeforeAbort ) &&
490- ! retainStateUntilCompleteOperations . has ( operation )
491- ) {
492- clearState ( ) ;
493- } else {
494- scheduleTerminalSettle ( ) ;
495- }
487+ abortOperation ( "restart" , createAgentRunRestartAbortError ( ) , "aborted_for_restart" ) ;
496488 return true ;
497489 } ,
498490 } ;
@@ -638,18 +630,11 @@ export function createReplyOperation(params: {
638630 return ;
639631 }
640632 const restart = isAgentRunRestartAbortReason ( upstreamAbortSignal . reason ) ;
641- const phaseBeforeAbort = phase ;
642- abortWithReason ( restart ? "restart" : "user_abort" , upstreamAbortSignal . reason , {
643- abortedCode : restart ? "aborted_for_restart" : "aborted_by_user" ,
644- } ) ;
645- if (
646- isReplyOperationPreBackendPhase ( phaseBeforeAbort ) &&
647- ! retainStateUntilCompleteOperations . has ( operation )
648- ) {
649- clearState ( ) ;
650- } else {
651- scheduleTerminalSettle ( ) ;
652- }
633+ abortOperation (
634+ restart ? "restart" : "user_abort" ,
635+ upstreamAbortSignal . reason ,
636+ restart ? "aborted_for_restart" : "aborted_by_user" ,
637+ ) ;
653638 } ;
654639 if ( upstreamAbortSignal . aborted ) {
655640 abortFromUpstream ( ) ;
0 commit comments