Skip to content

Commit dd57844

Browse files
fix(clients): hold icon downloads while their environment cache clears
A resolve that started after the revision bump but before the clear finished could persist its result once the deletes had run. Downloads now wait for an in-flight clear before capturing the revision they compare against. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 3069ab5 commit dd57844

2 files changed

Lines changed: 46 additions & 5 deletions

File tree

‎packages/client-runtime/src/projectFaviconCache.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,35 @@ describe("persistent project favicon cache", () => {
156156
},
157157
);
158158

159+
it("discards a download that starts while the environment is being cleared", async () => {
160+
const records = new Map<string, ProjectFaviconEntry>();
161+
const removal = deferred<void>();
162+
const load = vi.fn(async () => image);
163+
const storage: ProjectFaviconStorage = {
164+
list: async () => [...records.values()],
165+
put: async (key, entry) => {
166+
records.set(key, entry);
167+
},
168+
remove: async (key) => {
169+
await removal.promise;
170+
records.delete(key);
171+
},
172+
};
173+
const cache = createProjectFaviconCache({ storage, load });
174+
await cache.resolve(target, url, signal());
175+
await cache.flush();
176+
const clearing = cache.clearEnvironment(target.environmentId);
177+
await Promise.resolve();
178+
const late = cache.resolve(target, url.replace("vabc", "vdef"), signal());
179+
removal.resolve();
180+
await clearing;
181+
expect(records.size).toBe(0);
182+
expect(cache.peek(target)).toBeNull();
183+
expect(load).toHaveBeenCalledTimes(1);
184+
expect(await late).toBe(image);
185+
expect(load).toHaveBeenCalledTimes(2);
186+
});
187+
159188
it("bounds individual images, total bytes, and entry count in storage", async () => {
160189
const { cache, load, records } = fixture();
161190
load.mockResolvedValueOnce(

‎packages/client-runtime/src/projectFaviconCache.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ export function createProjectFaviconCache(input: {
127127
const environmentRevisions = new Map<EnvironmentId, number>();
128128
let generation = 0;
129129
let hydration: Promise<void> | undefined;
130+
let clearing: Promise<void> | undefined;
130131
const pending = new Set<Promise<void>>();
131132

132133
const persist = (operation: () => Promise<void>) => {
@@ -179,6 +180,7 @@ export function createProjectFaviconCache(input: {
179180
url: string | null,
180181
signal: AbortSignal,
181182
): Promise<string | null> => {
183+
await clearing;
182184
const startGeneration = generation;
183185
const startRevision = environmentRevisions.get(target.environmentId) ?? 0;
184186
await hydrate();
@@ -227,15 +229,25 @@ export function createProjectFaviconCache(input: {
227229
await Promise.all(pending);
228230
};
229231

232+
// A download that started before the clear sees the revision change and is discarded;
233+
// one that starts during the clear waits for it, so it cannot repopulate storage.
230234
const clear = async (environmentId?: EnvironmentId) => {
231235
if (environmentId === undefined) generation += 1;
232236
else
233237
environmentRevisions.set(environmentId, (environmentRevisions.get(environmentId) ?? 0) + 1);
234-
await hydrate();
235-
for (const [key, entry] of entries) {
236-
if (environmentId === undefined || entry.environmentId === environmentId) remove(key);
237-
}
238-
await flush();
238+
const previous = clearing;
239+
const task = (async () => {
240+
await previous;
241+
await hydrate();
242+
for (const [key, entry] of entries) {
243+
if (environmentId === undefined || entry.environmentId === environmentId) remove(key);
244+
}
245+
await flush();
246+
})().finally(() => {
247+
if (clearing === task) clearing = undefined;
248+
});
249+
clearing = task;
250+
await task;
239251
};
240252

241253
return {

0 commit comments

Comments
 (0)