@@ -236,6 +236,7 @@ type ChannelManagerOptions = {
236236 getNativeApprovalRuntime ?: ( ) => GatewayNativeApprovalRuntime | undefined ;
237237 ambientAutostartSuppressedChannelIds ?: ReadonlySet < string > ;
238238 tryRecoverAutostartSuppression ?: ( ) => boolean ;
239+ isClosing ?: ( ) => boolean ;
239240} ;
240241
241242type StopChannelOptions = {
@@ -823,7 +824,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
823824 } else if ( startupTrace ) {
824825 await waitForChannelStartupHandoff ( ) ;
825826 }
826- if ( abort . signal . aborted || manuallyStopped . has ( rKey ) ) {
827+ if ( abort . signal . aborted || manuallyStopped . has ( rKey ) || opts . isClosing ?. ( ) ) {
827828 return ;
828829 }
829830 const gatewayApprovalRuntime = opts . getNativeApprovalRuntime ?.( ) ;
@@ -855,7 +856,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
855856 }
856857 let startAccountTask : ReturnType < typeof startAccount > | undefined ;
857858 await measureStartup ( `channels.${ channelId } .start-account-handoff` , ( ) => {
858- if ( abort . signal . aborted || manuallyStopped . has ( rKey ) ) {
859+ if ( abort . signal . aborted || manuallyStopped . has ( rKey ) || opts . isClosing ?. ( ) ) {
859860 return ;
860861 }
861862 const runStartAccount = ( ) => {
@@ -917,7 +918,12 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
917918 // Only the task that still owns the store slot may write lifecycle state.
918919 const trackedPromise = task
919920 . then ( ( ) => {
920- if ( abort . signal . aborted || manuallyStopped . has ( rKey ) || ! isCurrentTask ( ) ) {
921+ if (
922+ abort . signal . aborted ||
923+ manuallyStopped . has ( rKey ) ||
924+ opts . isClosing ?.( ) ||
925+ ! isCurrentTask ( )
926+ ) {
921927 return ;
922928 }
923929 if ( getRuntime ( channelId , id ) . terminalDisconnect ) {
@@ -930,7 +936,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
930936 log . error ?.( `[${ id } ] ${ message } ` ) ;
931937 } )
932938 . catch ( ( err : unknown ) => {
933- if ( ! isCurrentTask ( ) || store . stops . has ( id ) ) {
939+ if ( ! isCurrentTask ( ) || store . stops . has ( id ) || opts . isClosing ?. ( ) ) {
934940 return ;
935941 }
936942 const message = formatErrorMessage ( err ) ;
@@ -948,15 +954,15 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
948954 await cleanupTaskScopedApprovalRuntime ( "channel cleanup failed" ) ;
949955 // stopChannel owns the failed-teardown snapshot until a later
950956 // successful stop proves replacement is safe.
951- if ( ! isCurrentTask ( ) || store . stops . has ( id ) ) {
957+ if ( ! isCurrentTask ( ) || store . stops . has ( id ) || opts . isClosing ?. ( ) ) {
952958 return ;
953959 }
954960 setStoppedRuntime ( channelId , id , {
955961 lastStopAt : Date . now ( ) ,
956962 } ) ;
957963 } )
958964 . then ( async ( ) => {
959- if ( ! isCurrentTask ( ) || store . stops . has ( id ) ) {
965+ if ( ! isCurrentTask ( ) || store . stops . has ( id ) || opts . isClosing ?. ( ) ) {
960966 return ;
961967 }
962968 if ( manuallyStopped . has ( rKey ) ) {
@@ -1052,7 +1058,7 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
10521058 pendingAutoRestarts . add ( rKey ) ;
10531059 try {
10541060 await sleepWithAbort ( retry . delayMs , retry . signal ) ;
1055- if ( manuallyStopped . has ( rKey ) ) {
1061+ if ( manuallyStopped . has ( rKey ) || opts . isClosing ?. ( ) ) {
10561062 return ;
10571063 }
10581064 if ( store . tasks . get ( id ) === trackedPromise ) {
@@ -1350,7 +1356,12 @@ export function createChannelManager(opts: ChannelManagerOptions): ChannelManage
13501356 const startChannels = async ( ) => await startChannelsWithOptions ( ) ;
13511357
13521358 const recoverAutostartSuppression = async ( ) : Promise < boolean > => {
1353- if ( ! autostartSuppression || ! opts . tryRecoverAutostartSuppression ?.( ) ) {
1359+ if (
1360+ ! autostartSuppression ||
1361+ opts . isClosing ?.( ) ||
1362+ ! opts . tryRecoverAutostartSuppression ?.( ) ||
1363+ opts . isClosing ?.( )
1364+ ) {
13541365 return false ;
13551366 }
13561367 autostartSuppression = null ;
0 commit comments