@@ -25,13 +25,15 @@ import type { LeaseRegistry } from '../lease-registry.ts';
2525import { releaseSessionLease } from '../lease-lifecycle.ts' ;
2626import type { LeaseLifecycleProvider } from './lease.ts' ;
2727import {
28+ reportSessionCleanupFailures ,
2829 restoreSessionAndroidIme ,
2930 stopAppleRunnerForClose ,
3031 stopSessionAndroidNativePerfCapture ,
3132 stopSessionAndroidSnapshotHelper ,
3233 stopSessionAppLog ,
3334 stopSessionApplePerfCapture ,
3435 stopSessionAudioProbe ,
36+ type SessionCleanupFailure ,
3537} from '../session-teardown.ts' ;
3638
3739async function maybeShutdownSessionTarget ( params : {
@@ -72,29 +74,49 @@ export async function handleCloseCommand(params: {
7274 return await closeWithoutSession ( req , logPath ) ;
7375 }
7476 let providerData : Record < string , unknown > | undefined ;
77+ // Resource teardown is failure-isolated: a rejected step is collected instead of
78+ // short-circuiting the rest, so every subsequent resource (and the runner stop)
79+ // is still attempted. Lease release and session deletion below run regardless,
80+ // and any collected failures are surfaced as an aggregate after cleanup.
81+ const cleanupFailures : SessionCleanupFailure [ ] = [ ] ;
82+ const attemptCleanup = async ( step : string , run : ( ) => Promise < void > ) : Promise < void > => {
83+ try {
84+ await run ( ) ;
85+ } catch ( error ) {
86+ cleanupFailures . push ( { step, error } ) ;
87+ }
88+ } ;
7589 try {
76- await stopSessionAppLog ( session ) ;
77- await stopSessionAudioProbe ( session , 'session-close' ) ;
78- await stopSessionApplePerfCapture ( session ) ;
79- await stopSessionAndroidNativePerfCapture ( session ) ;
80- await stopSessionAndroidSnapshotHelper ( session ) ;
81- await restoreSessionAndroidIme ( session , sessionStore . resolveDaemonStateDir ( ) ) ;
90+ await attemptCleanup ( 'app_log' , ( ) => stopSessionAppLog ( session ) ) ;
91+ await attemptCleanup ( 'audio_probe' , async ( ) => {
92+ await stopSessionAudioProbe ( session , 'session-close' ) ;
93+ } ) ;
94+ await attemptCleanup ( 'apple_perf' , ( ) => stopSessionApplePerfCapture ( session ) ) ;
95+ await attemptCleanup ( 'android_native_perf' , ( ) => stopSessionAndroidNativePerfCapture ( session ) ) ;
96+ await attemptCleanup ( 'android_snapshot_helper' , ( ) =>
97+ stopSessionAndroidSnapshotHelper ( session ) ,
98+ ) ;
99+ await attemptCleanup ( 'android_ime' , ( ) =>
100+ restoreSessionAndroidIme ( session , sessionStore . resolveDaemonStateDir ( ) ) ,
101+ ) ;
82102 if ( shouldDispatchPlatformClose ( req , session ) ) {
83103 if ( shouldStopAppleRunnerBeforeTargetedClose ( session ) ) {
84- await stopAppleRunnerForClose ( session ) ;
104+ await attemptCleanup ( 'apple_runner_pre_close' , ( ) => stopAppleRunnerForClose ( session ) ) ;
85105 }
86- await dispatchCommand ( session . device , 'close' , req . positionals ?? [ ] , req . flags ?. out , {
87- ...contextFromFlags ( logPath , req . flags , session . appBundleId , session . trace ?. outPath ) ,
106+ await attemptCleanup ( 'platform_close' , async ( ) => {
107+ await dispatchCommand ( session . device , 'close' , req . positionals ?? [ ] , req . flags ?. out , {
108+ ...contextFromFlags ( logPath , req . flags , session . appBundleId , session . trace ?. outPath ) ,
109+ } ) ;
110+ await settleIosSimulator ( session . device , IOS_SIMULATOR_POST_CLOSE_SETTLE_MS ) ;
88111 } ) ;
89- await settleIosSimulator ( session . device , IOS_SIMULATOR_POST_CLOSE_SETTLE_MS ) ;
90112 }
91113 if (
92114 isApplePlatform ( session . device . platform ) &&
93115 ! shouldRetainAppleRunnerAfterClose ( req , session )
94116 ) {
95117 // The targeted close path stops before dispatch to avoid runner/app races.
96118 // Stop again here for idempotent cleanup, and keep cleanup-sensitive closes explicit.
97- await stopAppleRunnerForClose ( session ) ;
119+ await attemptCleanup ( 'apple_runner' , ( ) => stopAppleRunnerForClose ( session ) ) ;
98120 } else if ( isApplePlatform ( session . device . platform ) ) {
99121 emitDiagnostic ( {
100122 level : 'debug' ,
@@ -123,7 +145,9 @@ export async function handleCloseCommand(params: {
123145 session . recordSession = true ;
124146 }
125147 sessionStore . writeSessionLog ( session ) ;
126- await cleanupRetainedMaterializedPathsForSession ( sessionName ) . catch ( ( ) => { } ) ;
148+ await attemptCleanup ( 'materialized_paths' , ( ) =>
149+ cleanupRetainedMaterializedPathsForSession ( sessionName ) ,
150+ ) ;
127151 } finally {
128152 // Always drop the local session, even if provider-side release fails:
129153 // a failed close must not strand device ownership until inactivity expiry.
@@ -133,6 +157,12 @@ export async function handleCloseCommand(params: {
133157 sessionStore . delete ( sessionName ) ;
134158 }
135159 }
160+ const cleanupAggregate = reportSessionCleanupFailures ( {
161+ sessionName,
162+ phase : 'session_close_cleanup_failed' ,
163+ failures : cleanupFailures ,
164+ } ) ;
165+ if ( cleanupAggregate ) throw cleanupAggregate ;
136166 const shutdownResult = await maybeShutdownSessionTarget ( {
137167 device : session . device ,
138168 shutdownRequested : req . flags ?. shutdown ,
0 commit comments