Skip to content

Commit 621001d

Browse files
committed
fix(server): protect shared refresh startup from interruption
1 parent 09d38cf commit 621001d

2 files changed

Lines changed: 63 additions & 16 deletions

File tree

‎apps/server/src/vcs/VcsStatusBroadcaster.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,43 @@ describe("VcsStatusBroadcaster", () => {
603603
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
604604
});
605605

606+
it.effect(
607+
"interrupting the caller that starts a refresh leaves it running for queued callers",
608+
() => {
609+
const firstFetchStarted = Deferred.makeUnsafe<void>();
610+
const releaseFirstFetch = Deferred.makeUnsafe<void>();
611+
const state: VcsStatusTestState = {
612+
currentLocalStatus: baseLocalStatus,
613+
currentRemoteStatus: baseRemoteStatus,
614+
localStatusCalls: 0,
615+
remoteStatusCalls: 0,
616+
localInvalidationCalls: 0,
617+
remoteInvalidationCalls: 0,
618+
};
619+
state.beforeRemoteStatus = Effect.suspend(() =>
620+
state.remoteStatusCalls === 0
621+
? Deferred.succeed(firstFetchStarted, undefined).pipe(
622+
Effect.andThen(Deferred.await(releaseFirstFetch)),
623+
)
624+
: Effect.void,
625+
);
626+
627+
return Effect.gen(function* () {
628+
const broadcaster = yield* VcsStatusBroadcaster.VcsStatusBroadcaster;
629+
const starter = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped);
630+
yield* Deferred.await(firstFetchStarted);
631+
const waiting = yield* broadcaster.refreshStatus("/repo").pipe(Effect.forkScoped);
632+
yield* TestClock.adjust(Duration.zero);
633+
634+
yield* Fiber.interrupt(starter);
635+
yield* Deferred.succeed(releaseFirstFetch, undefined);
636+
637+
assert.deepStrictEqual(yield* Fiber.join(waiting), baseStatus);
638+
assert.equal(state.remoteStatusCalls, 2);
639+
}).pipe(Effect.provide(layerTestFor(state)), Effect.scoped);
640+
},
641+
);
642+
606643
it.effect("a failed shared refresh fails every request in it and the next one runs", () => {
607644
const firstFetchStarted = Deferred.makeUnsafe<void>();
608645
const releaseFirstFetch = Deferred.makeUnsafe<void>();

‎apps/server/src/vcs/VcsStatusBroadcaster.ts‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -531,25 +531,35 @@ export const make = Effect.gen(function* () {
531531
result: yield* Deferred.make<VcsStatusResult, GitManagerServiceError>(),
532532
policyCwds: new Set([rawCwd]),
533533
};
534-
const [joined, start] = yield* Ref.modify(
535-
refreshesRef,
536-
(refreshes): readonly [readonly [QueuedRefresh, boolean], ActiveRefreshes] => {
537-
const active = refreshes.get(cwd);
538-
if (active === undefined) {
539-
return [[request, true], new Map(refreshes).set(cwd, { queued: null })];
540-
}
541-
if (active.queued !== null) {
542-
const queued = {
543-
...active.queued,
544-
policyCwds: new Set(active.queued.policyCwds).add(rawCwd),
545-
};
546-
return [[queued, false], new Map(refreshes).set(cwd, { queued })];
534+
const joined = yield* Effect.uninterruptibleMask(() =>
535+
Effect.gen(function* () {
536+
const [joined, start] = yield* Ref.modify(
537+
refreshesRef,
538+
(refreshes): readonly [readonly [QueuedRefresh, boolean], ActiveRefreshes] => {
539+
const active = refreshes.get(cwd);
540+
if (active === undefined) {
541+
return [[request, true], new Map(refreshes).set(cwd, { queued: null })];
542+
}
543+
if (active.queued !== null) {
544+
const queued = {
545+
...active.queued,
546+
policyCwds: new Set(active.queued.policyCwds).add(rawCwd),
547+
};
548+
return [[queued, false], new Map(refreshes).set(cwd, { queued })];
549+
}
550+
return [[request, false], new Map(refreshes).set(cwd, { queued: request })];
551+
},
552+
);
553+
// Do not leave an active entry behind without its broadcaster-owned worker.
554+
if (start) {
555+
yield* runQueuedRefreshes(cwd, request).pipe(
556+
Effect.forkIn(broadcasterScope, { uninterruptible: false }),
557+
);
547558
}
548-
return [[request, false], new Map(refreshes).set(cwd, { queued: request })];
549-
},
559+
return joined;
560+
}),
550561
);
551562
// The refresh outlives a caller that stops waiting, so the others still get its result.
552-
if (start) yield* runQueuedRefreshes(cwd, request).pipe(Effect.forkIn(broadcasterScope));
553563
return yield* Deferred.await(joined.result);
554564
});
555565

0 commit comments

Comments
 (0)