Skip to content

Commit 5961b92

Browse files
committed
fix(android): stop a structurally-failed helper session before the one-shot viewport retry
A structured ok=false viewport response leaves the session process alive, and Android permits only one instrumentation owner of UiAutomation - running the one-shot fallback against a still-live helper contends with it and masks the original structured failure. Stop the session first; regression pins that the one-shot retry only executes once the session is gone.
1 parent 0b033f1 commit 5961b92

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎src/platforms/android/__tests__/touch-helper.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,48 @@ test('viewport falls back to one-shot instrumentation after a session error', as
672672
assert.ok(oneShotArgs?.includes('viewport'));
673673
assert.equal(await session.isSessionAlive(), false);
674674
});
675+
676+
test('a structured ok=false viewport response stops the session before the one-shot retry', async () => {
677+
const device = makeIsolatedDevice();
678+
const session = await startFakeTouchHelperSession(device, (command, requestId) => {
679+
if (command.startsWith('viewport')) {
680+
return sessionHeaderResponse({
681+
agentDeviceProtocol: 'android-snapshot-helper-v1',
682+
requestId,
683+
ok: 'false',
684+
errorType: 'java.lang.IllegalStateException',
685+
message: 'Active application interaction viewport is unavailable',
686+
});
687+
}
688+
return sessionHeaderResponse({
689+
agentDeviceProtocol: 'android-snapshot-helper-v1',
690+
requestId,
691+
ok: 'true',
692+
});
693+
});
694+
695+
let oneShotArgs: string[] | undefined;
696+
const viewportResult = await withAndroidAdbProvider(
697+
{
698+
exec: currentVersionAdb(async (args) => {
699+
// One instrumentation may own UiAutomation: the one-shot retry must only run once the
700+
// structurally-failed session has been stopped.
701+
assert.equal(await session.isSessionAlive(), false);
702+
oneShotArgs = args;
703+
return {
704+
exitCode: 0,
705+
stdout: [
706+
resultRecord({ ok: 'true', x: '5', y: '6', width: '300', height: '400' }),
707+
'INSTRUMENTATION_CODE: 0',
708+
].join('\n'),
709+
stderr: '',
710+
};
711+
}),
712+
},
713+
{ serial: device.id },
714+
async () => await readAndroidTouchHelperViewport(device),
715+
);
716+
717+
assert.deepEqual(viewportResult, { x: 5, y: 6, width: 300, height: 400 });
718+
assert.ok(oneShotArgs?.includes('viewport'));
719+
});

‎src/platforms/android/touch-helper.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ensureAndroidSnapshotHelper } from './snapshot-helper-install.ts';
1717
import {
1818
getAndroidSnapshotHelperSessionDeviceKey,
1919
runAndroidSnapshotHelperSessionTouchCommand,
20+
stopAndroidSnapshotHelperSession,
2021
} from './snapshot-helper-session.ts';
2122
import {
2223
ANDROID_SNAPSHOT_HELPER_PROTOCOL,
@@ -112,7 +113,10 @@ export async function readAndroidTouchHelperViewport(device: DeviceInfo): Promis
112113
});
113114
if (sessionHeaders) return readViewportResult(sessionHeaders);
114115
} catch (error) {
115-
// Viewport reads are idempotent; after the failed session is stopped, retry one-shot below.
116+
// Viewport reads are idempotent, so a fresh one-shot run may still answer. The session must
117+
// be stopped first: Android permits one instrumentation owner of UiAutomation, and a helper
118+
// left alive after a structured failure would contend with the one-shot instrumentation.
119+
await stopAndroidSnapshotHelperSession(prepared.deviceKey);
116120
emitDiagnostic({
117121
level: 'warn',
118122
phase: 'android_touch_helper_viewport_session_fallback',

0 commit comments

Comments
 (0)