Skip to content

Commit b52aea9

Browse files
committed
fix(limrun): abort the heartbeat probe GET when its budget expires
The budget stopped waiting but left the GET running until the SDK's own timeout. The probe now aborts its request on expiry. Move the provider-instance-ended reason and predicate next to isInactiveLeaseError in lease-scope.ts, so the device facade stays a pure re-export.
1 parent b31757c commit b52aea9

4 files changed

Lines changed: 30 additions & 15 deletions

File tree

‎packages/contracts/src/facades/device.ts‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,4 @@
1-
export const PROVIDER_INSTANCE_ENDED = 'provider-instance-ended';
2-
3-
export function isProviderInstanceEndedError(error: unknown): boolean {
4-
return (
5-
typeof error === 'object' &&
6-
error !== null &&
7-
(error as { code?: unknown }).code === 'COMMAND_FAILED' &&
8-
(error as { details?: { reason?: unknown } }).details?.reason === PROVIDER_INSTANCE_ENDED
9-
);
10-
}
11-
1+
export { PROVIDER_INSTANCE_ENDED, isProviderInstanceEndedError } from '../lease-scope.ts';
122
export type { TriggerAppEventCommandResult } from '../app-events.ts';
133
export { assertResolvedAppsFilter, resolveAppsFilter } from '../app-inventory.ts';
144
export type { AppsFilter } from '../app-inventory.ts';

‎packages/contracts/src/lease-scope.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,20 @@ export function isInactiveLeaseError(error: unknown): boolean {
512512
);
513513
}
514514

515+
/** The reason a provider gives when the device instance behind a lease ended outside the daemon. */
516+
export const PROVIDER_INSTANCE_ENDED = 'provider-instance-ended';
517+
518+
/** Whether `error` says the provider's device instance ended, which ends its lease. */
519+
export function isProviderInstanceEndedError(error: unknown): boolean {
520+
return (
521+
typeof error === 'object' &&
522+
error !== null &&
523+
(error as Readonly<{ code?: unknown }>).code === 'COMMAND_FAILED' &&
524+
(error as Readonly<{ details?: Readonly<{ reason?: unknown }> }>).details?.reason ===
525+
PROVIDER_INSTANCE_ENDED
526+
);
527+
}
528+
515529
/** The leases a daemon reply says its request ended; a malformed entry is no ended lease. */
516530
export function readEndedLeases(response: DaemonResponse): EndedLease[] {
517531
const body = response.ok ? response.data : response.error.details;

‎packages/provider-limrun/src/runtime-dependencies.test.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const state = vi.hoisted(() => ({
3232
os: 'android',
3333
},
3434
]),
35-
instanceGet: vi.fn(async () => ({
35+
instanceGet: vi.fn(async (_id?: string, _options?: { signal?: AbortSignal }) => ({
3636
status: { state: 'terminated', terminationReason: 'InactivityTimeout' },
3737
})),
3838
tunnelClose: vi.fn(),
@@ -99,10 +99,15 @@ test('heartbeat reports a terminated instance with its typed cause', async () =>
9999
state.instanceGet.mockRejectedValueOnce(new Error('API unavailable'));
100100
assert.equal(await runtime.leaseLifecycle.heartbeat!(lease), undefined);
101101
vi.useFakeTimers();
102-
state.instanceGet.mockReturnValueOnce(new Promise(() => {}));
102+
let probeSignal: AbortSignal | undefined;
103+
state.instanceGet.mockImplementationOnce((_id?: string, options?: { signal?: AbortSignal }) => {
104+
probeSignal = options?.signal;
105+
return new Promise(() => {});
106+
});
103107
const timedOut = runtime.leaseLifecycle.heartbeat!(lease);
104108
await vi.advanceTimersByTimeAsync(2_000);
105109
assert.equal(await timedOut, undefined);
110+
assert.equal(probeSignal?.aborted, true);
106111
vi.useRealTimers();
107112
await assert.rejects(
108113
runtime.leaseLifecycle.heartbeat!(lease),

‎packages/provider-limrun/src/runtime.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,17 @@ class LimrunRuntimeImplementation implements ProviderDeviceRuntime {
756756
let answer;
757757
try {
758758
const { awaitLimrunWithinBudget } = await import('./lifecycle-budget.ts');
759+
// The budget only stops waiting, so the probe aborts its own GET when the budget expires.
760+
const expiry = new AbortController();
761+
const signal = context?.signal
762+
? AbortSignal.any([context.signal, expiry.signal])
763+
: expiry.signal;
759764
const request =
760765
session.platform === 'ios'
761-
? this.limrun.iosInstances.get(session.instanceId, { signal: context?.signal })
762-
: this.limrun.androidInstances.get(session.instanceId, { signal: context?.signal });
766+
? this.limrun.iosInstances.get(session.instanceId, { signal })
767+
: this.limrun.androidInstances.get(session.instanceId, { signal });
763768
answer = await awaitLimrunWithinBudget(request, HEARTBEAT_PROBE_BUDGET_MS);
769+
if (!answer.ok && 'expired' in answer) expiry.abort();
764770
} catch {
765771
return undefined;
766772
}

0 commit comments

Comments
 (0)