Skip to content

Commit 427ea97

Browse files
committed
fix(cloud-mirror): bound finish while draining polls
Session-Id: 01a0d409-e854-7540-a76e-a2f9cd136946
1 parent 0e3b36d commit 427ea97

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

‎packages/sdk/src/cloud-mirror.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export function createRunMirror(options: RunMirrorOptions): RunMirror {
416416
try {
417417
const deadline = now() + budgetMs;
418418
await scan(deadline);
419-
await publishSnapshot(deadline);
419+
if (!stopped) await publishSnapshot(deadline);
420420
} catch (error) {
421421
diagnostic(`poll failed: ${error instanceof Error ? error.message : String(error)}`);
422422
} finally {
@@ -497,17 +497,27 @@ export function createRunMirror(options: RunMirrorOptions): RunMirror {
497497
},
498498
async finish(outcome) {
499499
if (timer !== undefined) { clearInterval(timer); timer = undefined; }
500+
const deadline = now() + MIRROR_FINISH_BUDGET_MS;
501+
const signal = AbortSignal.timeout(MIRROR_FINISH_BUDGET_MS);
500502
// Stop new polls and drain one already in flight before the terminal
501503
// scan; otherwise a stale poll can publish after the terminal callback.
502504
stopped = true;
503-
if (activePoll !== undefined) await activePoll;
504-
const deadline = now() + MIRROR_FINISH_BUDGET_MS;
505-
const signal = AbortSignal.timeout(MIRROR_FINISH_BUDGET_MS);
505+
let pollDrained = activePoll === undefined;
506+
if (activePoll !== undefined) {
507+
const waitForPoll = activePoll.then(() => { pollDrained = true; });
508+
const remaining = Math.max(0, deadline - now());
509+
await Promise.race([
510+
waitForPoll,
511+
new Promise<void>(resolve => { setTimeout(resolve, remaining); }),
512+
]);
513+
}
506514
try {
507515
// One last reading, so the page shows the run's actual last moments
508516
// rather than whatever the previous poll happened to catch.
509-
await scan(deadline);
510-
await publishSnapshot(deadline);
517+
if (pollDrained) {
518+
await scan(deadline);
519+
await publishSnapshot(deadline);
520+
}
511521
if (outcome.log !== undefined && outcome.log.length > 0) {
512522
// The CLI's own output, redacted on the way out. It is not a
513523
// transcript the worker already scrubbed: it is whatever this

0 commit comments

Comments
 (0)