Skip to content

Commit d476097

Browse files
committed
fix(capture-kit): a poll never starts after the signal aborted
observeUntil checked the caller's signal only at the top of each iteration, before the sleep. A cancel that arrived during the sleep still paid one more capture before the loop noticed. The loop now checks the signal after the sleep and ends with the typed request-canceled error before starting a poll. The host-kit sleep takes no signal, so the check after the sleep is the boundary.
1 parent fa7f36a commit d476097

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎packages/capture-kit/src/observe-until.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,34 @@ describe('observeUntil', () => {
210210
});
211211
});
212212

213+
describe('observeUntil cancellation', () => {
214+
test('a signal aborted during the sleep ends the loop canceled before another capture', async () => {
215+
const clock = fakeClock();
216+
const controller = new AbortController();
217+
let captures = 0;
218+
await assert.rejects(
219+
observeUntil({
220+
capture: async () => {
221+
captures += 1;
222+
return captures;
223+
},
224+
verdict: () => ({ kind: 'continue' }),
225+
schedule: UNBOUNDED_SCHEDULE,
226+
signal: controller.signal,
227+
clock: {
228+
now: clock.now,
229+
sleep: async (ms) => {
230+
await clock.sleep(ms);
231+
controller.abort();
232+
},
233+
},
234+
}),
235+
(error) => error instanceof AppError && error.details?.reason === 'request_canceled',
236+
);
237+
assert.equal(captures, 1);
238+
});
239+
});
240+
213241
describe('observeUntil budgetFrom first-capture', () => {
214242
test('never bounds the first capture and spends the budget only on retries', async () => {
215243
const clock = fakeClock();

‎packages/capture-kit/src/observe-until.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export async function observeUntil<T, R>(
149149
if (params.signal?.aborted) throw createRequestCanceledError();
150150
const expired = await waitBeforePoll(loop);
151151
if (expired) return expired;
152+
if (params.signal?.aborted) throw createRequestCanceledError();
152153
const ended = await pollOnce(loop);
153154
if (ended) return ended;
154155
}

0 commit comments

Comments
 (0)