Skip to content

Commit 7593233

Browse files
committed
fix(providers): stop superseded snapshot subscriptions
1 parent 055a39d commit 7593233

2 files changed

Lines changed: 64 additions & 18 deletions

File tree

‎apps/server/src/provider/ProviderRegistry.test.ts‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,8 @@ it.layer(
18231823
models: [],
18241824
} satisfies ServerProvider;
18251825
const changes = yield* PubSub.unbounded<ServerProvider>();
1826+
let activeSubscriptions = 0;
1827+
const instanceChanges = yield* PubSub.unbounded<void>();
18261828
const instance = {
18271829
instanceId: cursorInstanceId,
18281830
driverKind: cursorDriver,
@@ -1842,23 +1844,33 @@ it.layer(
18421844
),
18431845
getSnapshot: Effect.succeed(initialProvider),
18441846
refresh: Effect.succeed(refreshedProvider),
1845-
streamChanges: Stream.fromPubSub(changes),
1847+
streamChanges: Stream.unwrap(
1848+
Effect.sync(() => {
1849+
activeSubscriptions++;
1850+
return Stream.fromPubSub(changes);
1851+
}),
1852+
).pipe(
1853+
Stream.ensuring(
1854+
Effect.sync(() => {
1855+
activeSubscriptions--;
1856+
}),
1857+
),
1858+
),
18461859
applyUsageLimits: () => Effect.void,
18471860
},
18481861
orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
18491862
textGeneration: {} as ProviderInstance["textGeneration"],
18501863
} satisfies ProviderInstance;
1864+
let instances: ReadonlyArray<ProviderInstance> = [instance];
18511865
const layerInstanceRegistry = Layer.succeed(
18521866
ProviderInstanceRegistry.ProviderInstanceRegistry,
18531867
{
18541868
getInstance: (instanceId) =>
1855-
Effect.succeed(instanceId === cursorInstanceId ? instance : undefined),
1856-
listInstances: Effect.succeed([instance]),
1869+
Effect.sync(() => instances.find((instance) => instance.instanceId === instanceId)),
1870+
listInstances: Effect.sync(() => instances),
18571871
listUnavailable: Effect.succeed([]),
18581872
streamChanges: Stream.empty,
1859-
subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), (pubsub) =>
1860-
PubSub.subscribe(pubsub),
1861-
),
1873+
subscribeChanges: PubSub.subscribe(instanceChanges),
18621874
},
18631875
);
18641876
const scope = yield* Scope.make();
@@ -1900,6 +1912,32 @@ it.layer(
19001912
models: [...initialProvider.models],
19011913
}),
19021914
);
1915+
for (const iconUrl of ["https://example.com/one.png", "https://example.com/two.png"]) {
1916+
const next = { ...initialProvider, iconUrl };
1917+
instances = [
1918+
{
1919+
...instance,
1920+
snapshot: { ...instance.snapshot, getSnapshot: Effect.succeed(next) },
1921+
},
1922+
];
1923+
const updated = yield* registry.streamChanges.pipe(
1924+
Stream.filter((providers) => providers[0]?.iconUrl === iconUrl),
1925+
Stream.runHead,
1926+
Effect.forkChild({ startImmediately: true }),
1927+
);
1928+
yield* PubSub.publish(instanceChanges, undefined);
1929+
yield* Fiber.join(updated);
1930+
assert.strictEqual(activeSubscriptions, 1);
1931+
}
1932+
instances = [];
1933+
const removed = yield* registry.streamChanges.pipe(
1934+
Stream.filter((providers) => providers.length === 0),
1935+
Stream.runHead,
1936+
Effect.forkChild({ startImmediately: true }),
1937+
);
1938+
yield* PubSub.publish(instanceChanges, undefined);
1939+
yield* Fiber.join(removed);
1940+
assert.strictEqual(activeSubscriptions, 0);
19031941
}).pipe(Effect.provide(runtimeServices));
19041942
}),
19051943
);

‎apps/server/src/provider/ProviderRegistry.ts‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,22 @@ export const layer = Layer.effect(
428428
ReadonlyMap<ProviderInstanceId, { readonly update?: ServerProviderUpdateState | undefined }>
429429
>(new Map());
430430

431-
const liveSubsRef = yield* Ref.make<ReadonlyMap<ProviderInstanceId, ProviderInstance>>(
431+
type LiveSubscription = {
432+
readonly instance: ProviderInstance;
433+
readonly fiber: Fiber.Fiber<void>;
434+
};
435+
const liveSubsRef = yield* Ref.make<ReadonlyMap<ProviderInstanceId, LiveSubscription>>(
432436
new Map(),
433437
);
434438
const syncSemaphore = yield* Semaphore.make(1);
435439

436440
const getLiveSources: Effect.Effect<ReadonlyArray<ProviderSnapshotSource>> = Ref.get(
437441
liveSubsRef,
438-
).pipe(Effect.map((map) => Array.from(map.values(), buildSnapshotSource)));
442+
).pipe(
443+
Effect.map((map) =>
444+
Array.from(map.values(), ({ instance }) => buildSnapshotSource(instance)),
445+
),
446+
);
439447

440448
const persistProvider = Effect.fn("ProviderRegistry.persistProvider")(function* (
441449
provider: ServerProvider,
@@ -773,11 +781,13 @@ export const layer = Layer.effect(
773781
}
774782
const previousSubs = yield* Ref.get(liveSubsRef);
775783

776-
const carriedOver = new Map<ProviderInstanceId, ProviderInstance>();
777-
for (const [instanceId, previousInstance] of previousSubs) {
784+
const carriedOver = new Map<ProviderInstanceId, LiveSubscription>();
785+
for (const [instanceId, previous] of previousSubs) {
778786
const nextInstance = nextByInstance.get(instanceId);
779-
if (nextInstance !== undefined && nextInstance === previousInstance) {
780-
carriedOver.set(instanceId, previousInstance);
787+
if (nextInstance !== undefined && nextInstance === previous.instance) {
788+
carriedOver.set(instanceId, previous);
789+
} else {
790+
yield* Fiber.interrupt(previous.fiber);
781791
}
782792
}
783793

@@ -812,11 +822,13 @@ export const layer = Layer.effect(
812822
}
813823

814824
// Subscribe before reading so a probe completing mid-sync is observed by at least one path.
815-
for (const [, instance] of newlyAdded) {
825+
const nextSubs = new Map(carriedOver);
826+
for (const [instanceId, instance] of newlyAdded) {
816827
const source = buildSnapshotSource(instance);
817-
yield* Stream.runForEach(source.streamChanges, (provider) =>
828+
const fiber = yield* Stream.runForEach(source.streamChanges, (provider) =>
818829
correlateSnapshotWithSource(source, provider).pipe(Effect.flatMap(syncProvider)),
819830
).pipe(Effect.forkScoped);
831+
nextSubs.set(instanceId, { instance, fiber });
820832
}
821833
yield* Effect.yieldNow;
822834

@@ -837,10 +849,6 @@ export const layer = Layer.effect(
837849
replace: true,
838850
});
839851

840-
const nextSubs = new Map(carriedOver);
841-
for (const [instanceId, instance] of newlyAdded) {
842-
nextSubs.set(instanceId, instance);
843-
}
844852
yield* Ref.set(liveSubsRef, nextSubs);
845853

846854
const [previousProviders, providers] = yield* Ref.modify(

0 commit comments

Comments
 (0)