@@ -58,13 +58,13 @@ const SESSION_REQUEST_OVERHEAD_MS = 3_000;
5858const FORWARD_TIMEOUT_MS = 5_000 ;
5959// A helper that cannot start spends its whole start budget failing, and the one-shot transport that
6060// answers afterwards still has to run. Retrying that on the very next command is what made commands
61- // on the slow hosts of #2553 take roughly twice as long, so a failed start keeps the persistent path
62- // away for at least this long.
63- const SESSION_START_RETRY_FLOOR_MS = 10_000 ;
64- // …and for no longer than this, however long the start took. The floor keeps a burst of commands
61+ // on the slow hosts of #2553 take roughly twice as long, so a failed start or session capture keeps
62+ // the persistent path away for at least this long.
63+ const SESSION_RETRY_FLOOR_MS = 10_000 ;
64+ // …and for no longer than this, however long the failure took. The floor keeps a burst of commands
6565// from re-paying an instant failure; the ceiling keeps a host whose helper is simply broken from
6666// being written off for longer than a working session would have lasted.
67- const SESSION_START_RETRY_CEILING_MS = 60_000 ;
67+ const SESSION_RETRY_CEILING_MS = 60_000 ;
6868
6969export type AndroidSnapshotHelperSessionHelperIdentity = {
7070 packageName : string ;
@@ -93,8 +93,11 @@ export type AndroidSnapshotHelperSessionAcquisition = {
9393} ;
9494
9595const sessions = new Map < string , AndroidSnapshotHelperSession > ( ) ;
96- /** Capture identity → when this process may spawn that helper build again after a failed start. */
97- const failedStarts = new Map < string , number > ( ) ;
96+ /**
97+ * Capture identity → when this process may spawn that helper build again after a failed start or a
98+ * failed session capture.
99+ */
100+ const failedSessions = new Map < string , number > ( ) ;
98101
99102/**
100103 * Starts (or reuses) the session without capturing, so a helper-backed read that is not a snapshot
@@ -150,7 +153,7 @@ async function resolveAndroidSnapshotHelperSession(params: {
150153 resolved : AndroidSnapshotHelperResolvedCaptureOptions ;
151154 startBudgetMs : number ;
152155} ) : Promise < AndroidSnapshotHelperSession | undefined > {
153- if ( isAndroidSnapshotHelperStartBackedOff ( params . identity ) ) return undefined ;
156+ if ( isAndroidSnapshotHelperSessionBackedOff ( params . identity ) ) return undefined ;
154157 await retireUnusableAndroidSnapshotHelperSession ( params . deviceKey , params . identity ) ;
155158 return sessions . get ( params . deviceKey ) ?? ( await tryStartAndroidSnapshotHelperSession ( params ) ) ;
156159}
@@ -186,9 +189,9 @@ async function tryStartAndroidSnapshotHelperSession(params: {
186189 return await startAndroidSnapshotHelperSession ( params ) ;
187190 } catch ( error ) {
188191 params . options . signal ?. throwIfAborted ( ) ;
189- failedStarts . set (
192+ failedSessions . set (
190193 params . identity ,
191- Date . now ( ) + androidSnapshotHelperStartRetryAfterMs ( Date . now ( ) - startedAtMs ) ,
194+ Date . now ( ) + androidSnapshotHelperSessionRetryAfterMs ( Date . now ( ) - startedAtMs ) ,
192195 ) ;
193196 emitDiagnostic ( {
194197 level : 'warn' ,
@@ -203,24 +206,52 @@ async function tryStartAndroidSnapshotHelperSession(params: {
203206 }
204207}
205208
206- /** A helper build whose last start failed is left alone until that start's backoff has run out. */
207- function isAndroidSnapshotHelperStartBackedOff ( identity : string ) : boolean {
208- const retryAtMs = failedStarts . get ( identity ) ;
209+ /** A helper build whose last start or capture failed is left alone until its backoff has run out. */
210+ function isAndroidSnapshotHelperSessionBackedOff ( identity : string ) : boolean {
211+ const retryAtMs = failedSessions . get ( identity ) ;
209212 if ( retryAtMs === undefined ) return false ;
210213 if ( retryAtMs > Date . now ( ) ) return true ;
211- failedStarts . delete ( identity ) ;
214+ failedSessions . delete ( identity ) ;
212215 return false ;
213216}
214217
215218/**
216- * How long a failed start earns: as long as it spent failing, because a start that burned half a
217- * minute on a wedged device would burn another half minute on the next command, bounded so a burst
219+ * How long a failed start or capture earns: as long as it spent failing, because a start that burned
220+ * half a minute on a wedged device would burn another half minute on the next command, bounded so a burst
218221 * of commands neither re-pays an instant failure nor writes a device off for the rest of the run.
219222 */
220- function androidSnapshotHelperStartRetryAfterMs ( startDurationMs : number ) : number {
221- return Math . min (
222- Math . max ( startDurationMs , SESSION_START_RETRY_FLOOR_MS ) ,
223- SESSION_START_RETRY_CEILING_MS ,
223+ function androidSnapshotHelperSessionRetryAfterMs ( failureDurationMs : number ) : number {
224+ return Math . min ( Math . max ( failureDurationMs , SESSION_RETRY_FLOOR_MS ) , SESSION_RETRY_CEILING_MS ) ;
225+ }
226+
227+ /**
228+ * Tears down a session whose capture failed and keeps its helper build off the persistent path for
229+ * as long as a failed start would, counted from when the one-shot capture that answers instead has
230+ * had its whole budget. A session that timed out once times out again on the same screen, so
231+ * restarting it on the next attempt or command only spends its budget again before the one-shot
232+ * transport answers. A cancellation is the caller's deadline, not evidence about the helper, so it
233+ * earns no backoff.
234+ */
235+ export async function retireFailedAndroidSnapshotHelperSession ( params : {
236+ deviceKey : string ;
237+ failedAfterMs : number ;
238+ /** Budget of the one-shot capture that answers in place of the failed session. */
239+ fallbackBudgetMs : number ;
240+ signal ?: AbortSignal ;
241+ cause : unknown ;
242+ } ) : Promise < void > {
243+ const identity = sessions . get ( params . deviceKey ) ?. identity ;
244+ await stopAndroidSnapshotHelperSession ( params . deviceKey , {
245+ force : true ,
246+ signal : params . signal ,
247+ cause : params . cause ,
248+ } ) ;
249+ if ( identity === undefined || params . signal ?. aborted ) return ;
250+ failedSessions . set (
251+ identity ,
252+ Date . now ( ) +
253+ params . fallbackBudgetMs +
254+ androidSnapshotHelperSessionRetryAfterMs ( params . failedAfterMs ) ,
224255 ) ;
225256}
226257
@@ -285,7 +316,7 @@ async function startAndroidSnapshotHelperSession(params: {
285316 params . options . signal ,
286317 ) ;
287318 sessions . set ( params . deviceKey , session ) ;
288- failedStarts . delete ( params . identity ) ;
319+ failedSessions . delete ( params . identity ) ;
289320 // `am instrument` force-stops whatever is already instrumenting this package, so a helper that
290321 // reported itself ready is the only helper process the device has left, and the release the
291322 // previous teardown could not prove went away with the process that owed it. Leaving the entry
@@ -530,7 +561,7 @@ export async function resetAndroidSnapshotHelperSessions(): Promise<void> {
530561 // One teardown that throws must not leave the next caller believing a session, a pending
531562 // retirement, or a failed start is still standing.
532563 sessions . clear ( ) ;
533- failedStarts . clear ( ) ;
564+ failedSessions . clear ( ) ;
534565 resetAndroidSnapshotHelperRetirements ( ) ;
535566 resetAndroidAdbShellProtocolProbes ( ) ;
536567 }
0 commit comments