Skip to content

Commit 3631d87

Browse files
fix(server): refreshes drop provider merge request reads before readers can see them
A finished turn now forgets every provider change request read rather than only the project's stored repository, and every refresh path forgets provider reads before it moves an epoch, so a read in between cannot keep the old one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ac270ce commit 3631d87

2 files changed

Lines changed: 84 additions & 37 deletions

File tree

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2208,12 +2208,70 @@ it.effect("a refresh or a finished turn drops the provider's own change request
22082208

22092209
assert.deepStrictEqual(forgotten, [
22102210
{ repository: "acme/web", number: 1 },
2211-
{ repository: "acme/web" },
2211+
undefined,
22122212
undefined,
22132213
]);
22142214
}),
22152215
);
22162216

2217+
it.effect("a read during a refresh cannot keep the provider's old change request", () =>
2218+
Effect.gen(function* () {
2219+
let hostTitle = "old";
2220+
let held: string | undefined;
2221+
let gate:
2222+
| { readonly entered: Deferred.Deferred<void>; readonly release: Deferred.Deferred<void> }
2223+
| undefined;
2224+
const service = yield* makeService({
2225+
projects: [
2226+
project({
2227+
id: "p1",
2228+
title: "web",
2229+
workspaceRoot: "/a",
2230+
repository: "acme/web",
2231+
provider: "gitlab",
2232+
}),
2233+
],
2234+
providers: [
2235+
fakeProvider(SourceControlProviderKind.make("gitlab"), {
2236+
getChangeRequest: () =>
2237+
Effect.sync(() => {
2238+
held ??= hostTitle;
2239+
return { ...hostedChangeRequest("body"), title: held };
2240+
}),
2241+
forgetChangeRequestReads: () =>
2242+
Effect.gen(function* () {
2243+
if (gate !== undefined) {
2244+
yield* Deferred.succeed(gate.entered, undefined);
2245+
yield* Deferred.await(gate.release);
2246+
}
2247+
held = undefined;
2248+
}),
2249+
}),
2250+
],
2251+
});
2252+
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 1 };
2253+
const refreshWithReadInBetween = (refresh: Effect.Effect<void>) =>
2254+
Effect.gen(function* () {
2255+
gate = { entered: yield* Deferred.make<void>(), release: yield* Deferred.make<void>() };
2256+
const running = yield* refresh.pipe(Effect.forkChild());
2257+
yield* Deferred.await(gate.entered);
2258+
yield* service.detail(reference);
2259+
yield* Deferred.succeed(gate.release, undefined);
2260+
yield* Fiber.join(running);
2261+
gate = undefined;
2262+
});
2263+
2264+
assert.strictEqual((yield* service.detail(reference)).title, "old");
2265+
hostTitle = "after turn";
2266+
yield* refreshWithReadInBetween(service.refreshAfterTurn(reference.projectId));
2267+
assert.strictEqual((yield* service.detail(reference)).title, "after turn");
2268+
2269+
hostTitle = "after refresh";
2270+
yield* refreshWithReadInBetween(service.invalidate({ reference }));
2271+
assert.strictEqual((yield* service.detail(reference)).title, "after refresh");
2272+
}),
2273+
);
2274+
22172275
it.effect("close and reopen notify subscribed readers after invalidating their cached state", () =>
22182276
Effect.scoped(
22192277
Effect.gen(function* () {

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3374,57 +3374,46 @@ export const make = Effect.gen(function* () {
33743374
if (reference !== undefined) {
33753375
yield* canonicalRef(reference).pipe(
33763376
Effect.flatMap((ref) =>
3377-
readCache
3378-
.invalidate(refScope(ref))
3379-
.pipe(
3380-
Effect.andThen(Effect.sync(() => bumpRefEpoch(ref))),
3381-
Effect.andThen(
3382-
forgetProviderReads({ repository: ref.repository, number: ref.number }),
3383-
),
3384-
),
3377+
forgetProviderReads({ repository: ref.repository, number: ref.number }).pipe(
3378+
Effect.andThen(readCache.invalidate(refScope(ref))),
3379+
Effect.andThen(Effect.sync(() => bumpRefEpoch(ref))),
3380+
),
33853381
),
33863382
Effect.ignore,
33873383
);
33883384
} else {
3385+
yield* forgetProviderReads();
33893386
listingsEpoch = ++epochCounter;
33903387
everyFileRevisionEpoch = ++epochCounter;
33913388
viewersByHost.clear();
33923389
yield* Cache.invalidateAll(viewerFlights);
3393-
yield* forgetProviderReads();
33943390
}
33953391
if (options?.notifyReaders) {
33963392
yield* SubscriptionRef.set(pullRequestRefreshes, ++epochCounter);
33973393
}
33983394
});
33993395

3400-
const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = (projectId) =>
3401-
Effect.suspend(() => {
3402-
listingsEpoch = ++epochCounter;
3403-
projectEpochs.delete(projectId);
3404-
if (projectEpochs.size >= REF_EPOCH_CAPACITY) {
3405-
const oldest = projectEpochs.keys().next().value;
3406-
if (oldest !== undefined) {
3407-
projectEpochFloor = projectEpochs.get(oldest)!;
3408-
projectEpochs.delete(oldest);
3409-
}
3396+
const refreshAfterTurn: PullRequestService["Service"]["refreshAfterTurn"] = Effect.fn(
3397+
"PullRequestService.refreshAfterTurn",
3398+
)(function* (projectId) {
3399+
// Everything the provider holds, not just the project's repository: a turn can touch a
3400+
// change request reached through another repository, and no epoch may move until those
3401+
// reads are gone, or a reader in between keeps the old one under the new epoch.
3402+
yield* forgetProviderReads();
3403+
listingsEpoch = ++epochCounter;
3404+
projectEpochs.delete(projectId);
3405+
if (projectEpochs.size >= REF_EPOCH_CAPACITY) {
3406+
const oldest = projectEpochs.keys().next().value;
3407+
if (oldest !== undefined) {
3408+
projectEpochFloor = projectEpochs.get(oldest)!;
3409+
projectEpochs.delete(oldest);
34103410
}
3411-
projectEpochs.set(projectId, listingsEpoch);
3412-
const epoch = listingsEpoch;
3413-
return readCache.invalidate(`project:${projectId}`).pipe(
3414-
Effect.andThen(
3415-
projects.getShell(projectId).pipe(
3416-
Effect.flatMap((shell) => {
3417-
const repository = Option.isSome(shell)
3418-
? sourceControlRepositorySelector(shell.value.repositoryIdentity)
3419-
: null;
3420-
return repository === null ? Effect.void : forgetProviderReads({ repository });
3421-
}),
3422-
Effect.ignore,
3423-
),
3424-
),
3425-
Effect.andThen(SubscriptionRef.set(pullRequestRefreshes, epoch)),
3426-
);
3427-
});
3411+
}
3412+
projectEpochs.set(projectId, listingsEpoch);
3413+
const epoch = listingsEpoch;
3414+
yield* readCache.invalidate(`project:${projectId}`);
3415+
yield* SubscriptionRef.set(pullRequestRefreshes, epoch);
3416+
});
34283417

34293418
const invalidatedByMutation =
34303419
<I extends PullRequestRef>(

0 commit comments

Comments
 (0)