From 4e1dd899931866c775a9ee333ab279e3a1543111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sat, 21 Nov 2026 10:28:09 +0100 Subject: [PATCH 1/2] fix(android): keep a timed-out target probe from failing discovery of other devices Fixes #4765 Co-Authored-By: Claude Sonnet 5.5 --- .../platform-android/src/inventory.test.ts | 61 +++++++++++++++++++ packages/platform-android/src/inventory.ts | 24 +++++++- 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/packages/platform-android/src/inventory.test.ts b/packages/platform-android/src/inventory.test.ts index 67282f9b3e..39a870db51 100644 --- a/packages/platform-android/src/inventory.test.ts +++ b/packages/platform-android/src/inventory.test.ts @@ -199,6 +199,67 @@ test('Android inventory classifies a device by has-feature amazon.hardware.fire_ assert.deepEqual(featureListProbeCalls, []); }); +test('Android inventory keeps a device whose feature probe times out from failing discovery of another', async () => { + const diagnostics: unknown[] = []; + const host = createHost(async (request) => { + const key = request.args.join('\0'); + if (key === 'devices\0-l') { + return result( + 'List of devices attached\nemulator-5554 device model:Busy\nemulator-5556 device model:Idle\n', + ); + } + if (key.includes('emulator-5554') && key.includes('pm\0list\0features')) { + throw new AppError('COMMAND_FAILED', 'adb timed out after 10000ms', { timeoutMs: 10_000 }); + } + const probe = RUNNING_EMULATOR_PROBES.find(([fragment]) => key.includes(fragment)); + if (probe) return result(probe[1]); + throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`); + }); + + const devices = await createAndroidInventory(host).discover( + { androidAvdSelection: 'running-only' }, + { ...scope, diagnostics: { emit: (event) => diagnostics.push(event) } }, + ); + + assert.deepEqual( + devices.map((device) => [device.id, device.target]), + [ + ['emulator-5554', undefined], + ['emulator-5556', 'mobile'], + ], + ); + assert.deepEqual(diagnostics, [ + { + level: 'warn', + phase: 'android_inventory_target_probe_failed', + data: { serial: 'emulator-5554', adbFailure: 'timeout' }, + }, + ]); +}); + +test('Android inventory still aborts when the request is cancelled during the feature probe', async () => { + const controller = new AbortController(); + const host = createHost(async (request) => { + const key = request.args.join('\0'); + if (key === 'devices\0-l') return result('List of devices attached\nemulator-5554 device\n'); + if (key.includes('pm\0list\0features')) { + controller.abort(); + throw new AppError('COMMAND_FAILED', 'aborted', { timeoutMs: 10_000 }); + } + const probe = RUNNING_EMULATOR_PROBES.find(([fragment]) => key.includes(fragment)); + if (probe) return result(probe[1]); + throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`); + }); + + await assert.rejects( + createAndroidInventory(host).discover( + { androidAvdSelection: 'running-only' }, + { ...scope, signal: controller.signal }, + ), + (error: unknown) => error instanceof AppError, + ); +}); + test('Android inventory fails closed when adb is unavailable', async () => { const host = createHost(async () => result(''), { adb: undefined }); await assert.rejects( diff --git a/packages/platform-android/src/inventory.ts b/packages/platform-android/src/inventory.ts index c43285fa25..56c2f6ceee 100644 --- a/packages/platform-android/src/inventory.ts +++ b/packages/platform-android/src/inventory.ts @@ -136,7 +136,7 @@ async function probeRunningDevice( const [name, booted, target, description] = await Promise.all([ resolveDeviceName(context, entry), isBooted(context, entry.serial), - resolveTarget(context, entry.serial), + resolveTargetOrUnknown(context, entry.serial), readDeviceDescription(context, entry.serial), ]); return { @@ -219,6 +219,28 @@ async function isBooted(context: AndroidInventoryContext, serial: string): Promi } } +/** + * A busy device that times out a target probe stays listed with an unknown target, so it cannot + * fail a request that selects another device. Every other failure still fails discovery. + */ +async function resolveTargetOrUnknown( + context: AndroidInventoryContext, + serial: string, +): Promise<'mobile' | 'tv' | undefined> { + try { + return await resolveTarget(context, serial); + } catch (error) { + const appError = asAppError(error); + if (context.scope.signal.aborted || appError.details?.adbFailure !== 'timeout') throw error; + context.scope.diagnostics.emit({ + level: 'warn', + phase: 'android_inventory_target_probe_failed', + data: { serial, adbFailure: 'timeout' }, + }); + return undefined; + } +} + async function resolveTarget( context: AndroidInventoryContext, serial: string, From 7a4ff0289da3d90a1abc900b1f1dbfb15b04921e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Sat, 21 Nov 2026 11:06:52 +0100 Subject: [PATCH 2/2] fix(android): fail discovery when the requested serial's target probe times out Co-Authored-By: Claude Sonnet 5.5 --- .../platform-android/src/inventory.test.ts | 23 ++++++++++++++++++- packages/platform-android/src/inventory.ts | 14 +++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/platform-android/src/inventory.test.ts b/packages/platform-android/src/inventory.test.ts index 39a870db51..46f5e2e528 100644 --- a/packages/platform-android/src/inventory.test.ts +++ b/packages/platform-android/src/inventory.test.ts @@ -256,7 +256,28 @@ test('Android inventory still aborts when the request is cancelled during the fe { androidAvdSelection: 'running-only' }, { ...scope, signal: controller.signal }, ), - (error: unknown) => error instanceof AppError, + (error: unknown) => error instanceof AppError && error.code === 'COMMAND_FAILED', + ); +}); + +test('Android inventory fails when the feature probe of the requested serial times out', async () => { + const host = createHost(async (request) => { + const key = request.args.join('\0'); + if (key === 'devices\0-l') return result('List of devices attached\nemulator-5554 device\n'); + if (key.includes('pm\0list\0features')) { + throw new AppError('COMMAND_FAILED', 'adb timed out after 10000ms', { timeoutMs: 10_000 }); + } + const probe = RUNNING_EMULATOR_PROBES.find(([fragment]) => key.includes(fragment)); + if (probe) return result(probe[1]); + throw new Error(`Unexpected command: ${request.executable} ${request.args.join(' ')}`); + }); + + await assert.rejects( + createAndroidInventory(host).discover( + { serial: 'emulator-5554', androidAvdSelection: 'running-only' }, + scope, + ), + (error: unknown) => error instanceof AppError && error.details?.adbFailure === 'timeout', ); }); diff --git a/packages/platform-android/src/inventory.ts b/packages/platform-android/src/inventory.ts index 56c2f6ceee..ddde88e63c 100644 --- a/packages/platform-android/src/inventory.ts +++ b/packages/platform-android/src/inventory.ts @@ -43,6 +43,8 @@ type AndroidInventoryContext = Readonly<{ host: DeviceInventoryHostFor<'android'>; adb: string; emulator?: string; + /** The serial the request names; its probe failures fail discovery. */ + selectedSerial?: string; scope: PlatformRequestScope; }>; @@ -75,6 +77,7 @@ async function discoverAndroidDevices( host, adb, emulator, + selectedSerial: request.serial?.trim() || undefined, scope, }; const allowlist = resolveSerialAllowlist(request); @@ -221,7 +224,8 @@ async function isBooted(context: AndroidInventoryContext, serial: string): Promi /** * A busy device that times out a target probe stays listed with an unknown target, so it cannot - * fail a request that selects another device. Every other failure still fails discovery. + * fail a request that selects another device. The selected serial and every other failure still + * fail discovery, so a selected device never runs with a demoted target. */ async function resolveTargetOrUnknown( context: AndroidInventoryContext, @@ -231,7 +235,13 @@ async function resolveTargetOrUnknown( return await resolveTarget(context, serial); } catch (error) { const appError = asAppError(error); - if (context.scope.signal.aborted || appError.details?.adbFailure !== 'timeout') throw error; + if ( + context.scope.signal.aborted || + serial === context.selectedSerial || + appError.details?.adbFailure !== 'timeout' + ) { + throw error; + } context.scope.diagnostics.emit({ level: 'warn', phase: 'android_inventory_target_probe_failed',