Skip to content

Commit a880362

Browse files
committed
fix: typed cancellation everywhere + own the AWS remote-access ARN through startup
Second-order follow-ups the #1774 refactor made cheap: - markRequestCanceled aborts the request signal WITH the kernel's typed canceled error as its reason. Every signal.throwIfAborted(), aborted fetch, and 'throw signal.reason' in the daemon (20+ sites) now surfaces a canceled request as such instead of a bare DOMException that normalized to UNKNOWN — and no site has to know the factory exists. - AWS Device Farm prepareSession owns the remote-access ARN from the moment create-remote-access-session answers: a startup timeout, the allocation deadline, or a canceled request now stops it before the failure surfaces (previously a timed-out startup left a RUNNING billed session behind — the same leak class as the WebDriver session, one phase earlier). The startup wait is capped by LeaseLifecycleContext.deadline and wakes on cancellation. - BrowserStack's pre-session local app upload honors the request signal (an upload is not billed, so plain abort is right there). - lease_heartbeat/lease_release share lease_allocate's preserve-daemon policy: the rationale — the daemon owns billed sessions; a reset orphans them all — applies verbatim. Each AWS ownership test proven red without the guard (3/3).
1 parent 0d1f1eb commit a880362

7 files changed

Lines changed: 276 additions & 42 deletions

File tree

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import type { DeviceLease } from '@agent-device/contracts/device';
4+
import {
5+
AppError,
6+
createRequestCanceledError,
7+
isRequestCanceledError,
8+
} from '@agent-device/kernel/errors';
9+
import {
10+
createAwsDeviceFarmPrepareSession,
11+
type AwsDeviceFarmClient,
12+
type AwsDeviceFarmRemoteAccessSession,
13+
} from './aws-device-farm.ts';
14+
import { buildCloudWebDriverBaseCapabilities } from './runtime.ts';
15+
16+
const ARN = 'arn:aws:devicefarm:us-west-2:1:session/pending';
17+
18+
// Once `create-remote-access-session` answers, the ARN is a billed session that
19+
// nothing else will stop. Every way the startup wait can end short of RUNNING
20+
// must stop it before the failure surfaces (#1774 ownership rule, one phase
21+
// earlier than the WebDriver session).
22+
test('a startup timeout stops the remote-access session it created', async () => {
23+
const client = fakeClient({ status: 'PENDING' });
24+
const prepare = createAwsDeviceFarmPrepareSession({
25+
...baseOptions(client),
26+
startupTimeoutMs: 30,
27+
pollIntervalMs: 5,
28+
});
29+
30+
await assert.rejects(
31+
() => prepare({ lease: makeLease(), base: baseSession() }),
32+
(error: unknown) => {
33+
assert.ok(error instanceof AppError);
34+
assert.match(error.message, /Timed out waiting for AWS Device Farm/);
35+
return true;
36+
},
37+
);
38+
assert.deepEqual(client.stopped, [ARN]);
39+
});
40+
41+
test('a canceled request stops the remote-access session mid-startup', async () => {
42+
const controller = new AbortController();
43+
const client = fakeClient({ status: 'PENDING' }, () =>
44+
controller.abort(createRequestCanceledError()),
45+
);
46+
const prepare = createAwsDeviceFarmPrepareSession({
47+
...baseOptions(client),
48+
startupTimeoutMs: 5_000,
49+
pollIntervalMs: 5,
50+
});
51+
52+
await assert.rejects(
53+
() => prepare({ lease: makeLease(), req: { signal: controller.signal }, base: baseSession() }),
54+
(error: unknown) => isRequestCanceledError(error),
55+
);
56+
assert.deepEqual(client.stopped, [ARN]);
57+
});
58+
59+
// The daemon's allocation deadline caps the startup wait so a client that
60+
// stops waiting at that deadline never abandons a still-polling daemon.
61+
test('the allocation deadline caps the startup wait below its own default', async () => {
62+
const client = fakeClient({ status: 'PENDING' });
63+
const prepare = createAwsDeviceFarmPrepareSession({
64+
...baseOptions(client),
65+
startupTimeoutMs: 60_000,
66+
pollIntervalMs: 5,
67+
});
68+
const startedAt = Date.now();
69+
70+
await assert.rejects(
71+
() => prepare({ lease: makeLease(), req: { deadline: Date.now() + 40 }, base: baseSession() }),
72+
/Timed out waiting for AWS Device Farm/,
73+
);
74+
assert.ok(Date.now() - startedAt < 5_000, 'the wait must end at the deadline, not the default');
75+
assert.deepEqual(client.stopped, [ARN]);
76+
});
77+
78+
test('a session that reaches RUNNING is handed on and not stopped', async () => {
79+
const client = fakeClient({
80+
status: 'RUNNING',
81+
endpoints: { appium: 'https://appium.example/wd/hub' },
82+
});
83+
const prepare = createAwsDeviceFarmPrepareSession(baseOptions(client));
84+
85+
const prepared = await prepare({ lease: makeLease(), base: baseSession() });
86+
assert.equal(prepared.providerSessionId, ARN);
87+
assert.equal(prepared.endpoint, 'https://appium.example/wd/hub');
88+
assert.deepEqual(client.stopped, []);
89+
});
90+
91+
function fakeClient(
92+
session: Partial<AwsDeviceFarmRemoteAccessSession>,
93+
onPoll?: () => void,
94+
): AwsDeviceFarmClient & { stopped: string[] } {
95+
const stopped: string[] = [];
96+
return {
97+
stopped,
98+
createRemoteAccessSession: async () => ({ arn: ARN, status: 'PENDING' }),
99+
getRemoteAccessSession: async (arn) => {
100+
onPoll?.();
101+
return { arn, ...session };
102+
},
103+
stopRemoteAccessSession: async (arn) => {
104+
stopped.push(arn);
105+
return { arn, status: 'STOPPING' };
106+
},
107+
listArtifacts: async () => [],
108+
};
109+
}
110+
111+
function baseOptions(client: AwsDeviceFarmClient) {
112+
return {
113+
client,
114+
platform: 'android' as const,
115+
deviceName: 'Pixel',
116+
projectArn: 'arn:aws:devicefarm:us-west-2:1:project/p',
117+
deviceArn: 'arn:aws:devicefarm:us-west-2::device/d',
118+
};
119+
}
120+
121+
function baseSession() {
122+
return {
123+
provider: 'aws-device-farm',
124+
endpoint: 'http://127.0.0.1/',
125+
platform: 'android' as const,
126+
deviceName: 'Pixel',
127+
webdriverCapabilities: buildCloudWebDriverBaseCapabilities('android', 'Pixel'),
128+
};
129+
}
130+
131+
function makeLease(): DeviceLease {
132+
return {
133+
leaseId: 'lease-aws',
134+
tenantId: 'team-a',
135+
runId: 'run-a',
136+
leaseProvider: 'aws-device-farm',
137+
backend: 'android-instance',
138+
createdAt: 1,
139+
expiresAt: 2,
140+
heartbeatAt: 1,
141+
};
142+
}

‎packages/provider-webdriver/src/aws-device-farm.ts‎

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ import type {
1515
CloudWebDriverRuntimeOptions,
1616
CloudWebDriverPrepareSession,
1717
} from './runtime.ts';
18-
import type { DeviceLease, ProviderDeviceRuntime } from '@agent-device/contracts/device';
18+
import type {
19+
DeviceLease,
20+
LeaseLifecycleContext,
21+
ProviderDeviceRuntime,
22+
} from '@agent-device/contracts/device';
1923
import { setTimeout as sleep } from 'node:timers/promises';
2024
import { AppError } from '@agent-device/kernel/errors';
2125
import type { RunHostCommand } from './dependencies.ts';
@@ -203,7 +207,7 @@ export function createAwsDeviceFarmPrepareSession(
203207
'client' | 'platform' | 'deviceName' | 'clientVersion'
204208
>,
205209
): CloudWebDriverPrepareSession {
206-
return async ({ lease, base }) => {
210+
return async ({ lease, req, base }) => {
207211
const remoteAccess = await options.client.createRemoteAccessSession({
208212
projectArn: options.projectArn,
209213
deviceArn: options.deviceArn,
@@ -212,13 +216,27 @@ export function createAwsDeviceFarmPrepareSession(
212216
interactionMode: options.interactionMode,
213217
configuration: options.configuration,
214218
});
215-
const running = await waitForRunningRemoteAccessSession(remoteAccess.arn, options);
216-
const endpoint = selectAwsDeviceFarmWebDriverEndpoint(running);
217-
if (!endpoint) {
218-
throw new AppError('COMMAND_FAILED', 'AWS Device Farm did not expose a WebDriver endpoint.', {
219-
sessionArn: running.arn,
220-
status: running.status,
221-
});
219+
// From here the ARN is OURS: a billed remote-access session that nothing
220+
// else will ever stop. Whatever ends the startup wait short of RUNNING —
221+
// startup timeout, allocation deadline, or the requester leaving — must
222+
// stop it before the failure surfaces, or it keeps billing until AWS reaps
223+
// it (the same ownership rule as the WebDriver session in
224+
// WebDriverSessionManager, one phase earlier).
225+
let running: AwsDeviceFarmRemoteAccessSession;
226+
let endpoint: string | undefined;
227+
try {
228+
running = await waitForRunningRemoteAccessSession(remoteAccess.arn, options, req);
229+
endpoint = selectAwsDeviceFarmWebDriverEndpoint(running);
230+
if (!endpoint) {
231+
throw new AppError(
232+
'COMMAND_FAILED',
233+
'AWS Device Farm did not expose a WebDriver endpoint.',
234+
{ sessionArn: running.arn, status: running.status },
235+
);
236+
}
237+
} catch (error) {
238+
await stopRemoteAccessSessionAfterFailure(options.client, remoteAccess.arn, error);
239+
throw error;
222240
}
223241
const deviceName = running.device?.name ?? options.deviceName;
224242
const configured =
@@ -276,31 +294,60 @@ async function waitForRunningRemoteAccessSession(
276294
pollIntervalMs?: number;
277295
startupTimeoutMs?: number;
278296
},
297+
req: LeaseLifecycleContext | undefined,
279298
): Promise<AwsDeviceFarmRemoteAccessSession> {
280299
const timeoutMs = options.startupTimeoutMs ?? 120_000;
281300
const pollIntervalMs = options.pollIntervalMs ?? 5_000;
282301
const startedAt = Date.now();
302+
// The startup wait fits inside the lease-allocation deadline, so a client
303+
// that stops waiting at that deadline never abandons a still-polling daemon.
304+
const deadline = Math.min(startedAt + timeoutMs, req?.deadline ?? Infinity);
305+
const signal = req?.signal;
283306
let last = await options.client.getRemoteAccessSession(arn);
284-
while (Date.now() - startedAt < timeoutMs) {
307+
while (Date.now() < deadline) {
308+
signal?.throwIfAborted();
285309
if (last.status === 'RUNNING') return last;
286-
if (last.status === 'ERRORED' || last.status === 'STOPPED' || last.status === 'COMPLETED') {
287-
throw new AppError('COMMAND_FAILED', 'AWS Device Farm remote access session did not start.', {
288-
sessionArn: arn,
289-
status: last.status,
290-
result: last.result,
291-
});
292-
}
293-
await sleep(pollIntervalMs);
310+
throwIfRemoteAccessSessionEnded(last);
311+
// Wake early on cancellation; the typed reason is rethrown at the loop top.
312+
await sleep(pollIntervalMs, undefined, { signal }).catch(() => signal?.throwIfAborted());
294313
last = await options.client.getRemoteAccessSession(arn);
295314
}
296315
throw new AppError('COMMAND_FAILED', 'Timed out waiting for AWS Device Farm remote access.', {
297316
sessionArn: arn,
298317
status: last.status,
299318
result: last.result,
300-
timeoutMs,
319+
timeoutMs: deadline - startedAt,
320+
});
321+
}
322+
323+
const ENDED_REMOTE_ACCESS_STATUSES = new Set(['ERRORED', 'STOPPED', 'COMPLETED']);
324+
325+
function throwIfRemoteAccessSessionEnded(session: AwsDeviceFarmRemoteAccessSession): void {
326+
if (!session.status || !ENDED_REMOTE_ACCESS_STATUSES.has(session.status)) return;
327+
throw new AppError('COMMAND_FAILED', 'AWS Device Farm remote access session did not start.', {
328+
sessionArn: session.arn,
329+
status: session.status,
330+
result: session.result,
301331
});
302332
}
303333

334+
async function stopRemoteAccessSessionAfterFailure(
335+
client: AwsDeviceFarmClient,
336+
arn: string,
337+
primaryError: unknown,
338+
): Promise<void> {
339+
try {
340+
await client.stopRemoteAccessSession(arn);
341+
} catch (cleanupError) {
342+
if (primaryError instanceof AppError) {
343+
primaryError.details = {
344+
...primaryError.details,
345+
cleanupError: cleanupError instanceof Error ? cleanupError.message : String(cleanupError),
346+
};
347+
}
348+
}
349+
}
350+
304351
async function runAwsJson(
305352
runHostCommand: RunHostCommand,
306353
command: string,

‎packages/provider-webdriver/src/provider-definitions.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ export function createCloudWebDriverProviderDefinitions(
119119
username,
120120
accessKey,
121121
uploadEndpoint: env.BROWSERSTACK_APP_UPLOAD_ENDPOINT,
122+
// A local IPA/APK upload can run long (130 MB is routine); an
123+
// upload is not a billed resource, so the request's cancellation
124+
// may simply abort it — unlike the session creation that follows.
125+
signal: request.signal,
122126
});
123127
return {
124128
...base,
@@ -252,6 +256,7 @@ async function resolveBrowserStackAppReference(options: {
252256
username: string;
253257
accessKey: string;
254258
uploadEndpoint?: string;
259+
signal?: AbortSignal;
255260
}): Promise<string> {
256261
if (isProviderAppReference(options.app)) return options.app;
257262
const appPath = path.resolve(options.cwd ?? process.cwd(), options.app);
@@ -262,12 +267,16 @@ async function resolveBrowserStackAppReference(options: {
262267
{ providerApp: options.app },
263268
);
264269
}
265-
return await uploadBrowserStackApp(appPath, {
266-
clientVersion: options.clientVersion,
267-
username: options.username,
268-
accessKey: options.accessKey,
269-
endpoint: options.uploadEndpoint,
270-
});
270+
return await uploadBrowserStackApp(
271+
appPath,
272+
{
273+
clientVersion: options.clientVersion,
274+
username: options.username,
275+
accessKey: options.accessKey,
276+
endpoint: options.uploadEndpoint,
277+
},
278+
options.signal,
279+
);
271280
}
272281

273282
function isProviderAppReference(value: string): boolean {

‎src/core/command-descriptor/__tests__/timeout-policy.test.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ test('daemon-preserving timeout commands are a bounded, reviewed set', () => {
7070
// destroyed healthy app sessions.
7171
// scroll/back joined in #1638: `--settle` gives them the same post-action
7272
// capture loop, so a wedged bridge is now their dominant hang mode too.
73-
// lease_allocate joined in #1774: allocation creates a BILLED provider
74-
// session the daemon owns, so a client-side timeout must not SIGKILL the
75-
// daemon mid-create and orphan it (and every other provider session held).
73+
// The lease route joined in #1774: those commands act on BILLED provider
74+
// sessions the daemon owns, so a client-side timeout must not SIGKILL the
75+
// daemon mid-create/mid-release and orphan them (and every other provider
76+
// session held).
7677
const preserving = commandDescriptors
7778
.filter((descriptor) => descriptor.timeoutPolicy.onTimeout === 'preserve-daemon')
7879
.map((descriptor) => descriptor.name);
@@ -84,6 +85,8 @@ test('daemon-preserving timeout commands are a bounded, reviewed set', () => {
8485
'get',
8586
'is',
8687
'lease_allocate',
88+
'lease_heartbeat',
89+
'lease_release',
8790
'longpress',
8891
'press',
8992
'scroll',

‎src/core/command-descriptor/registry.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,23 @@ const INSTALL_TIMEOUT_POLICY: CommandTimeoutPolicy = {
285285
envelopeMs: INSTALL_REQUEST_TIMEOUT_MS,
286286
};
287287

288-
// Lease allocation against a cloud provider is remote work the daemon owns on
289-
// the caller's behalf: the provider session it creates is billed until the
290-
// daemon releases it. A client that stops waiting must leave the daemon alive —
291-
// the allocation either finishes and is released for the gone requester (see
292-
// `LeaseLifecycleContext.signal`) or fails under the daemon's own budget with
293-
// typed evidence. Resetting the daemon here would SIGKILL it mid-`POST /session`
294-
// and orphan the billed session it was about to own, along with every other
295-
// provider session that daemon held (#1774).
296-
const LEASE_ALLOCATE_TIMEOUT_POLICY: CommandTimeoutPolicy = {
288+
// Lease-route commands act on cloud provider sessions the daemon owns on the
289+
// caller's behalf — billed until the daemon releases them. A client that stops
290+
// waiting on one must leave the daemon alive: resetting it would SIGKILL a
291+
// process mid-`POST /session` (allocate) or mid-`DELETE` (release) and orphan
292+
// the session in flight, along with every other provider session that daemon
293+
// held (#1774). Allocation additionally carries its own envelope: the request
294+
// either finishes and is released for the gone requester (see
295+
// `LeaseLifecycleContext.signal`) or fails under the daemon's budget with
296+
// typed evidence.
297+
const LEASE_TIMEOUT_POLICY: CommandTimeoutPolicy = {
297298
...DEFAULT_TIMEOUT_POLICY,
298-
envelopeMs: LEASE_ALLOCATE_REQUEST_TIMEOUT_MS,
299299
onTimeout: 'preserve-daemon',
300300
};
301+
const LEASE_ALLOCATE_TIMEOUT_POLICY: CommandTimeoutPolicy = {
302+
...LEASE_TIMEOUT_POLICY,
303+
envelopeMs: LEASE_ALLOCATE_REQUEST_TIMEOUT_MS,
304+
};
301305

302306
const DEFAULT_SETTLE_TIMEOUT_MS = 10_000;
303307

@@ -387,7 +391,7 @@ export const RAW_COMMAND_DESCRIPTORS = [
387391
catalog: { group: 'internal', key: 'leaseHeartbeat' },
388392
recordsSessionAction: false,
389393
daemon: { route: 'lease', refFrameEffect: 'preserve', ...ADMISSION_AND_LOCK_EXEMPT },
390-
timeoutPolicy: DEFAULT_TIMEOUT_POLICY,
394+
timeoutPolicy: LEASE_TIMEOUT_POLICY,
391395
batchable: false,
392396
platformExecution: NO_PLATFORM_EXECUTION,
393397
},
@@ -397,7 +401,7 @@ export const RAW_COMMAND_DESCRIPTORS = [
397401
catalog: { group: 'internal', key: 'leaseRelease' },
398402
recordsSessionAction: false,
399403
daemon: { route: 'lease', refFrameEffect: 'preserve', ...ADMISSION_AND_LOCK_EXEMPT },
400-
timeoutPolicy: DEFAULT_TIMEOUT_POLICY,
404+
timeoutPolicy: LEASE_TIMEOUT_POLICY,
401405
batchable: false,
402406
platformExecution: NO_PLATFORM_EXECUTION,
403407
},

0 commit comments

Comments
 (0)