Skip to content

Commit f051db5

Browse files
committed
fix(limrun): give client disconnect an independent deadline
1 parent 079711d commit f051db5

5 files changed

Lines changed: 41 additions & 61 deletions

File tree

‎packages/provider-limrun/src/capture-lifecycle.test.ts‎

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { DeviceLease } from '@agent-device/contracts/device';
44
import { LimrunCaptureLifecycle, type LimrunCaptureHost } from './capture-lifecycle.ts';
55
import type { LimrunLeaseOwner } from './lease-ownership.ts';
66
import type { NetworkCapture } from './network-har.ts';
7+
import { watchUnhandledRejections } from './lifecycle.fixtures.ts';
78

89
/** Longer than the lifecycle's budget for a destination tunnel that is still being opened. */
910
const CAPTURE_OPEN_WAIT_MS = 25_000;
@@ -142,32 +143,6 @@ function captureHost(held: readonly LimrunLeaseOwner[] = []): LimrunCaptureHost
142143
};
143144
}
144145

145-
/**
146-
* Collects the rejections the process would die on: the daemon's failure mode for one is to take every other
147-
* session down with it. The check yields to a macrotask first, because Node reports a rejection only after the
148-
* current microtask drain — asserting before that yield, or removing the listener without one, could pass a
149-
* leaked rejection unseen.
150-
*/
151-
function watchUnhandledRejections(): {
152-
assertIsSilent: () => Promise<void>;
153-
stop: () => void;
154-
} {
155-
const seen: unknown[] = [];
156-
const watch = (reason: unknown): void => {
157-
seen.push(reason);
158-
};
159-
process.on('unhandledRejection', watch);
160-
return {
161-
assertIsSilent: async () => {
162-
await new Promise<void>((resolve) => {
163-
setTimeout(resolve, 0);
164-
});
165-
if (seen.length > 0) assert.fail(`expected no unhandled rejection, got ${String(seen[0])}`);
166-
},
167-
stop: () => process.off('unhandledRejection', watch),
168-
};
169-
}
170-
171146
/**
172147
* An owner whose lease has a destination tunnel that has not answered, and never will. A case that drives a
173148
* different lease overrides the id it needs.

‎packages/provider-limrun/src/lease-release-observation.test.ts‎

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import assert from 'node:assert/strict';
21
import { test, vi, expect } from 'vitest';
32
import type { DeviceLease } from '@agent-device/contracts/device';
43
import { createLimrunRuntime } from './runtime.ts';
54
import { stubLimrunRuntimeDependencies } from './runtime-dependencies.fixtures.ts';
65
import { mkdtempForTestSync } from './tmp-dir.fixtures.ts';
76
import type { LimrunRuntimeDependencies } from './runtime-dependencies.ts';
7+
import { watchUnhandledRejections } from './lifecycle.fixtures.ts';
88

99
/*
1010
* A disposal starts its capture end and its local teardown beside the instance delete and joins them only
@@ -200,34 +200,6 @@ function warningsOf(record: Record<string, unknown>): Array<Record<string, unkno
200200
return Array.isArray(record.warnings) ? (record.warnings as Record<string, unknown>[]) : [];
201201
}
202202

203-
/**
204-
* Collects the rejections the process would die on: the daemon's failure mode for one is to take every
205-
* other session down with it, so a case whose disposal looses one fails here rather than in production.
206-
* The check yields to a macrotask first: Node reports a rejection only after the current microtask drain,
207-
* so asserting before that yield, or removing the listener without one, could pass a leaked rejection
208-
* unseen.
209-
*/
210-
function watchUnhandledRejections(): {
211-
assertIsSilent: () => Promise<void>;
212-
stop: () => void;
213-
} {
214-
const seen: unknown[] = [];
215-
const watch = (reason: unknown): void => {
216-
seen.push(reason);
217-
};
218-
process.on('unhandledRejection', watch);
219-
return {
220-
assertIsSilent: async () => {
221-
await new Promise<void>((resolve) => {
222-
setTimeout(resolve, 0);
223-
});
224-
if (seen.length === 0) return;
225-
assert.fail(`expected no unhandled rejection, got ${String(seen[0])}`);
226-
},
227-
stop: () => process.off('unhandledRejection', watch),
228-
};
229-
}
230-
231203
function androidLease(leaseId: string): DeviceLease {
232204
return {
233205
leaseId,

‎packages/provider-limrun/src/lease-release.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,17 @@ async function releaseLocalResources(owner: LimrunLeaseOwner): Promise<void> {
338338
// rather than passed over: the session it eventually builds has nowhere else to be disconnected.
339339
const session = owner.session ?? (await sessionTheCreateBuilt(owner));
340340
if (session === undefined) return;
341-
try {
342-
// Disconnect closes the SDK's ADB endpoint, so reverse cleanup must finish while that endpoint is live.
343-
if (session.platform === 'android') await cleanupLimrunAndroidAdbTunnel(session);
344-
} finally {
341+
const { awaitLimrunWithinBudget } = await import('./lifecycle-budget.ts');
342+
const tunnel =
343+
session.platform === 'android' ? cleanupLimrunAndroidAdbTunnel(session) : Promise.resolve();
344+
// Disconnect closes the ADB endpoint: reverse cleanup gets its window, but cannot postpone disconnect
345+
// past that window. Both promises are observed before either can settle.
346+
const disconnect = awaitLimrunWithinBudget(tunnel, LOCAL_TEARDOWN_BUDGET_MS).then(() => {
345347
session.client.disconnect();
346-
}
348+
});
349+
const results = await Promise.allSettled([tunnel, disconnect]);
350+
const refused = results.find((result) => result.status === 'rejected');
351+
if (refused?.status === 'rejected') throw refused.reason;
347352
}
348353

349354
/** The session a create answered for, once it answers, for a release that got there first. */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import assert from 'node:assert/strict';
2+
3+
/**
4+
* Collects the rejections the process would die on: the daemon's failure mode for one is to take every other
5+
* session down with it. The check yields to a macrotask first, because Node reports a rejection only after the
6+
* current microtask drain — asserting before that yield, or removing the listener without one, could pass a
7+
* leaked rejection unseen.
8+
*/
9+
export function watchUnhandledRejections(): {
10+
assertIsSilent: () => Promise<void>;
11+
stop: () => void;
12+
} {
13+
const seen: unknown[] = [];
14+
const watch = (reason: unknown): void => {
15+
seen.push(reason);
16+
};
17+
process.on('unhandledRejection', watch);
18+
return {
19+
assertIsSilent: async () => {
20+
await new Promise<void>((resolve) => {
21+
setTimeout(resolve, 0);
22+
});
23+
if (seen.length > 0) assert.fail(`expected no unhandled rejection, got ${String(seen[0])}`);
24+
},
25+
stop: () => process.off('unhandledRejection', watch),
26+
};
27+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ test('a release that arrives while a tunnel is opening ends the tunnel that answ
430430
});
431431

432432
test('a release stops waiting on a stuck ADB tunnel, deletes the instance and says what it left', async () => {
433+
await import('./lease-release.ts');
433434
const { deletes, disconnects, adbTunnels, tunnelCloses } = limrunActivity();
434435
let reportTunnel!: () => void;
435436
state.tunnelGate = new Promise<void>((resolve) => {
@@ -458,6 +459,7 @@ test('a release stops waiting on a stuck ADB tunnel, deletes the instance and sa
458459
// The release's own budget for the local teardown elapses; the delete it ran beside is already answered.
459460
await vi.advanceTimersByTimeAsync(LOCAL_TEARDOWN_WAIT_MS);
460461
const [warning] = warningsOf(await released);
462+
assert.equal(state.disconnect.mock.calls.length - disconnects, 1);
461463
assert.equal(warning?.code, 'LIMRUN_TEARDOWN_INCOMPLETE');
462464
// The report names the work this route stopped waiting for, and the instance it left a host endpoint on.
463465
assert.match(String(warning?.message), /Local teardown/);
@@ -468,7 +470,6 @@ test('a release stops waiting on a stuck ADB tunnel, deletes the instance and sa
468470
reportTunnel();
469471
await vi.advanceTimersByTimeAsync(0);
470472
const refused = await refusedCommand;
471-
assert.equal(state.disconnect.mock.calls.length - disconnects, 1);
472473
assert.ok(
473474
refused instanceof AppError && refused.details?.reason === 'limrun_adb_tunnel_cleaned_up',
474475
`expected the retained command to be refused, got ${String(refused)}`,

0 commit comments

Comments
 (0)