Skip to content

Commit acd4c10

Browse files
committed
fix(limrun): retain capture close failures beside write errors
1 parent 9043233 commit acd4c10

5 files changed

Lines changed: 142 additions & 40 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ test('a capture that answers late and then refuses is not loosed on the process'
105105
}
106106
});
107107

108+
test('a refused capture record carries the close failure beside the write error', async () => {
109+
const rejections = watchUnhandledRejections();
110+
const writeError = new Error('HAR inspection refused');
111+
const close = vi.fn(() => {
112+
throw new Error('capture tunnel refused to close');
113+
});
114+
const lifecycle = new LimrunCaptureLifecycle(captureHost());
115+
const owner = ownerWithCapture();
116+
owner.capture = {
117+
phase: 'capturing',
118+
harPath: '/tmp/sm4-refused-capture.har',
119+
capture: {
120+
entries: () => [],
121+
failures: async () => {
122+
throw writeError;
123+
},
124+
close,
125+
},
126+
};
127+
try {
128+
await expect(lifecycle.end(owner)).rejects.toMatchObject({
129+
message: 'HAR inspection refused; Capture close failed: capture tunnel refused to close',
130+
cause: writeError,
131+
});
132+
expect(close).toHaveBeenCalledOnce();
133+
await rejections.assertIsSilent();
134+
} finally {
135+
rejections.stop();
136+
}
137+
});
138+
108139
/**
109140
* The record limit counts a lease whose owner still holds a capture, so the very same test spares that
110141
* record and no other. A lease the runtime still holds for an unconfirmed delete, whose capture is already

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,19 +203,25 @@ async function endCapture(
203203
record: (leaseId: string, capture: SettledNetworkCapture) => SettledNetworkCapture,
204204
): Promise<SettledNetworkCapture> {
205205
const { writeCaptureRecord } = await import('./network-har.ts');
206-
let settled: SettledNetworkCapture;
206+
const written = await writeCaptureRecord(capture).then(
207+
(value) => ({ ok: true, value }) as const,
208+
(error: unknown) => ({ ok: false, error }) as const,
209+
);
207210
let closeFailure: string | undefined;
208-
try {
209-
settled = await writeCaptureRecord(capture);
210-
} finally {
211-
if (capture.phase === 'capturing') {
212-
try {
213-
capture.capture.close();
214-
} catch (error) {
215-
closeFailure = errorMessage(error);
216-
}
211+
if (capture.phase === 'capturing') {
212+
try {
213+
capture.capture.close();
214+
} catch (error) {
215+
closeFailure = errorMessage(error);
217216
}
218217
}
218+
if (!written.ok) {
219+
if (closeFailure === undefined) throw written.error;
220+
throw new Error(`${errorMessage(written.error)}; Capture close failed: ${closeFailure}`, {
221+
cause: written.error,
222+
});
223+
}
224+
const settled = written.value;
219225
return record(leaseId, closeFailure === undefined ? settled : { ...settled, closeFailure });
220226
}
221227

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ export type LimrunOwnerCapture =
9595
*/
9696
export type LimrunDisposalOutcome = Readonly<{
9797
cleanup: 'confirmed' | 'attached' | 'uncertain' | 'none';
98-
/** Creation outlived this disposal's wait; the runtime re-takes the owner after creation settles. */
98+
/**
99+
* Creation outlived this disposal's wait. An uncertain outcome re-takes the owner after creation settles;
100+
* a confirmed delete already ended the rented instance, and its local teardown awaits the late session.
101+
*/
99102
creationPending?: true;
100103
/** The instance the lease held, known as soon as the create named it. */
101104
instanceId: string | undefined;

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

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const state = vi.hoisted(() => ({
1818
/** Instance deletes this runtime asked Limrun for; each waits for its case to answer it. */
1919
deletes: [] as Array<{ instanceId: string; report: () => void }>,
2020
creates: 0,
21+
clientStarts: 0,
22+
clientGate: undefined as Promise<void> | undefined,
2123
createGate: undefined as Promise<void> | undefined,
2224
autoDelete: false,
2325
disconnects: 0,
@@ -62,31 +64,35 @@ vi.mock('@limrun/api', () => ({
6264
}));
6365

6466
vi.mock('@limrun/api/instance-client', () => ({
65-
createInstanceClient: vi.fn(async () => ({
66-
disconnect: () => {
67-
state.disconnects += 1;
68-
if (state.disconnectThrows) throw new Error('instance client refused to disconnect');
69-
},
70-
keepAlive: () => {},
71-
openUrl: vi.fn(),
72-
sendAsset: vi.fn(),
73-
pressKey: vi.fn(),
74-
setText: vi.fn(),
75-
tap: vi.fn(),
76-
startAdbTunnel: vi.fn(async () => ({
77-
address: { address: '127.0.0.1', port: 62001 },
78-
close: () => {},
79-
})),
80-
startTunnel: vi.fn(async () => ({
81-
tunnelId: 't1',
82-
selectors: [],
83-
close: () => {
84-
state.captureCloses += 1;
85-
if (state.captureCloseThrows) throw new Error('capture tunnel refused to close');
67+
createInstanceClient: vi.fn(async () => {
68+
state.clientStarts += 1;
69+
await state.clientGate;
70+
return {
71+
disconnect: () => {
72+
state.disconnects += 1;
73+
if (state.disconnectThrows) throw new Error('instance client refused to disconnect');
8674
},
87-
})),
88-
getTunnelStatus: vi.fn(async () => ({})),
89-
})),
75+
keepAlive: () => {},
76+
openUrl: vi.fn(),
77+
sendAsset: vi.fn(),
78+
pressKey: vi.fn(),
79+
setText: vi.fn(),
80+
tap: vi.fn(),
81+
startAdbTunnel: vi.fn(async () => ({
82+
address: { address: '127.0.0.1', port: 62001 },
83+
close: () => {},
84+
})),
85+
startTunnel: vi.fn(async () => ({
86+
tunnelId: 't1',
87+
selectors: [],
88+
close: () => {
89+
state.captureCloses += 1;
90+
if (state.captureCloseThrows) throw new Error('capture tunnel refused to close');
91+
},
92+
})),
93+
getTunnelStatus: vi.fn(async () => ({})),
94+
};
95+
}),
9096
}));
9197

9298
test('a local teardown that refuses beside a pending delete is reported, not loosed on the process', async () => {
@@ -219,6 +225,7 @@ test('a capture close that refuses still leaves the delete it ran beside answere
219225

220226
test('a late create with failed local cleanup is freed by the normal take with confirmed deletion', async () => {
221227
await import('./lease-release.ts');
228+
const rejections = watchUnhandledRejections();
222229
const createsBefore = state.creates;
223230
state.deletes.length = 0;
224231
state.disconnectThrows = true;
@@ -275,6 +282,8 @@ test('a late create with failed local cleanup is freed by the normal take with c
275282
state.createGate = undefined;
276283
await runtime.leaseLifecycle.allocate?.(lease);
277284
expect(state.creates - createsBefore).toBe(2);
285+
vi.useRealTimers();
286+
await rejections.assertIsSilent();
278287
} finally {
279288
reportCreate();
280289
state.createGate = undefined;
@@ -285,6 +294,59 @@ test('a late create with failed local cleanup is freed by the normal take with c
285294
await runtime.shutdown();
286295
state.autoDelete = false;
287296
state.deletes.length = 0;
297+
rejections.stop();
298+
}
299+
});
300+
301+
test('a confirmed delete during creation still disconnects the late session without another rent', async () => {
302+
await import('./lease-release.ts');
303+
const rejections = watchUnhandledRejections();
304+
const createsBefore = state.creates;
305+
const clientsBefore = state.clientStarts;
306+
const disconnectsBefore = state.disconnects;
307+
state.deletes.length = 0;
308+
let reportClient!: () => void;
309+
state.clientGate = new Promise<void>((resolve) => {
310+
reportClient = resolve;
311+
});
312+
const runtime = createLimrunRuntime({ apiKey: 'lim_test_key' }, testDependencies());
313+
const lease = androidLease('lease-confirmed-before-session');
314+
const allocating = Promise.resolve(runtime.leaseLifecycle.allocate?.(lease)).catch(
315+
(error: unknown) => error,
316+
);
317+
try {
318+
// Reaching the instance client means the one POST has already named the rented instance.
319+
await vi.waitFor(() => expect(state.clientStarts - clientsBefore).toBe(1));
320+
const released = runtime.leaseLifecycle.release?.(lease);
321+
vi.useFakeTimers();
322+
await vi.advanceTimersByTimeAsync(125_000);
323+
expect(state.deletes).toHaveLength(1);
324+
state.deletes[0]?.report();
325+
await vi.advanceTimersByTimeAsync(50_000);
326+
expect(await released).toMatchObject({
327+
limrunInstanceId: 'android-instance-1',
328+
warnings: [{ code: 'LIMRUN_TEARDOWN_INCOMPLETE' }],
329+
});
330+
expect(state.disconnects - disconnectsBefore).toBe(0);
331+
// The owner has been closed, but its observed, memoized local release still owns this session.
332+
reportClient();
333+
await vi.advanceTimersByTimeAsync(0);
334+
expect(await allocating).toMatchObject({
335+
details: { reason: 'limrun_lease_released_mid_allocation' },
336+
});
337+
expect(state.creates - createsBefore).toBe(1);
338+
expect(state.deletes).toHaveLength(1);
339+
expect(state.disconnects - disconnectsBefore).toBe(1);
340+
vi.useRealTimers();
341+
await rejections.assertIsSilent();
342+
} finally {
343+
reportClient();
344+
state.clientGate = undefined;
345+
state.deletes.forEach(({ report }) => report());
346+
vi.useRealTimers();
347+
await runtime.shutdown();
348+
state.deletes.length = 0;
349+
rejections.stop();
288350
}
289351
});
290352

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ function captureRecordOutcome(
134134
}
135135

136136
/**
137-
* Ends what a disposal can name after creation outlives its wait. The lease stays held until the runtime
138-
* re-takes it after creation settles. A known instance is deleted now; the re-take joins that delete and the
139-
* memoized local teardown before the runtime decides whether the lease is free.
137+
* Ends what a disposal can name after creation outlives its wait. An uncertain outcome keeps the lease for
138+
* a re-take after creation settles. A confirmed delete frees it: that create already named its only rented
139+
* instance, and the memoized local teardown still awaits any session the create goes on to build.
140140
*/
141141
async function disposeUnansweredCreate(
142142
host: LimrunDisposalHost,
@@ -162,8 +162,8 @@ async function disposeUnansweredCreate(
162162
capture: await capture,
163163
};
164164
}
165-
// An instance is already named, so this disposal ends the billing it can see and leaves the create's silence
166-
// as what keeps the lease held.
165+
// The create has named its only rented instance, so confirming this delete ends its billing even while
166+
// session construction is still pending.
167167
const instance = await endInstance(host, owner, instanceId);
168168
// Reported, not thrown: a local step that refuses is one this disposal can no longer fix, and the delete
169169
// it ran beside has already been answered for.

0 commit comments

Comments
 (0)