Skip to content

Commit eddafcc

Browse files
loganjLarry
andcommitted
fix(links): look channel links up again after cache and access resets
Clearing the cache or losing access drops every reference answer, but mounted links kept showing the old one and an in-flight reply was dropped without a retry. Notify links and look demanded channels up again on reset. A lookup batch now carries the epoch it was queued in, so an old read's cancellation can't put the new epoch into backoff. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent b3ad009 commit eddafcc

3 files changed

Lines changed: 65 additions & 5 deletions

File tree

‎docs/relay-queries.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ nothing is `withheld`: the relay hides a private channel's metadata from
346346
non-members, so private, deleted and invalid channels look the same. A
347347
`withheld` answer is kept for five minutes and stays shown while it is
348348
rechecked. A failed read is never `withheld`; it waits at least 30 seconds (or
349-
the relay's `retryAfterMs`) before another try. Clearing the cache forgets
350-
every answer. Composer decorations never ask.
349+
the relay's `retryAfterMs`) before another try. Clearing the cache or
350+
losing access forgets every answer and drops replies still in flight; mounted
351+
links are notified and looked up again. Composer decorations never ask.
351352

352353
## Ownership and reconciliation
353354

‎src/features/relay/channel-references.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,45 @@ it("arms the recheck for a link remounted during its backoff", async () => {
277277
await h.lookup([SECRET], []);
278278
expect(link.result.current).toEqual({ state: "withheld" });
279279
});
280+
281+
it("tells a mounted withheld link about a cache reset and looks it up again", async () => {
282+
const h = await setup();
283+
const link = renderHook(() => useChannelReference(h.channels, SECRET));
284+
await h.lookup([SECRET], []);
285+
expect(link.result.current).toEqual({ state: "withheld" });
286+
await act(() => h.owner.clearCache());
287+
expect(link.result.current).toEqual({ state: "unknown" });
288+
// The channel has since become public.
289+
await h.lookup([SECRET], [open(SECRET, "launch")]);
290+
expect(link.result.current).toMatchObject({ state: "found", name: "launch" });
291+
});
292+
293+
it("looks a mounted link up again when a reset drops its in-flight reply", async () => {
294+
const h = await setup();
295+
const link = renderHook(() => useChannelReference(h.channels, SECRET));
296+
await flush();
297+
const stale = await h.next(39000);
298+
await act(() => h.owner.clearCache());
299+
stale.respond([]);
300+
await flush();
301+
await h.lookup([SECRET], [open(SECRET, "launch")]);
302+
expect(link.result.current).toMatchObject({ state: "found", name: "launch" });
303+
});
304+
305+
it("keeps a cancelled lookup from the old cache out of the new one's backoff", async () => {
306+
const h = await setup();
307+
const release = h.channels.refer?.(SECRET);
308+
await flush();
309+
const stale = await h.next(39000);
310+
release?.();
311+
const cleared = h.owner.clearCache();
312+
// The new cache's answers are read before the old read's cancellation
313+
// settles.
314+
expect(h.channels.describe?.(SECRET)).toEqual({ state: "unknown" });
315+
await cleared;
316+
stale.fail(new Error("aborted"));
317+
await flush();
318+
h.channels.refer?.(SECRET);
319+
await h.lookup([SECRET], []);
320+
expect(h.channels.describe?.(SECRET)).toEqual({ state: "withheld" });
321+
});

‎src/features/relay/store.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,6 +1654,7 @@ export function createChannelStore(
16541654
heads.clear();
16551655
tails.clear();
16561656
if (discovery) setList({ ...list, channels: discovery.channels() });
1657+
resetReferences();
16571658
await persistence?.clear().catch(() => {});
16581659
}
16591660
/** One background head read at a time; warm never competes with demand reads
@@ -1724,9 +1725,22 @@ export function createChannelStore(
17241725
references.retryAt.clear();
17251726
references.queue.clear();
17261727
references.pending.clear();
1728+
// A queued microtask from the old epoch drops its batch.
1729+
references.scheduled = false;
17271730
}
17281731
return references;
17291732
}
1733+
/** Cache and access resets drop every answer. Mounted links still show the
1734+
* old one until notified, and an in-flight lookup's reply is dropped, so
1735+
* notify them and look their channels up again. */
1736+
function resetReferences() {
1737+
currentReferences();
1738+
if (referenceWake) clearTimeout(referenceWake.timer);
1739+
referenceWake = undefined;
1740+
setList(list, true);
1741+
for (const id of demand.keys()) lookUpReference(id);
1742+
wakeReferences();
1743+
}
17301744
function describeReference(id: string): ChannelReference {
17311745
const channel = discovery?.get(id);
17321746
if (channel) {
@@ -1810,9 +1824,11 @@ export function createChannelStore(
18101824
state.queue.add(id);
18111825
if (state.scheduled) return;
18121826
state.scheduled = true;
1827+
// `state` is shared across epochs; this batch belongs to this one.
1828+
const generation = epoch;
18131829
queueMicrotask(() => {
1830+
if (generation !== epoch) return;
18141831
state.scheduled = false;
1815-
if (state.generation !== epoch) return;
18161832
const ids = [...state.queue];
18171833
state.queue.clear();
18181834
// `resolve` bounds one exact read to 128 channels.
@@ -1821,7 +1837,7 @@ export function createChannelStore(
18211837
for (const id of batch) state.pending.add(id);
18221838
resolve(batch, { priority: "background" }).then(
18231839
() => {
1824-
if (state.generation !== epoch) return;
1840+
if (generation !== epoch) return;
18251841
for (const id of batch) {
18261842
state.pending.delete(id);
18271843
state.retryAt.delete(id);
@@ -1833,7 +1849,7 @@ export function createChannelStore(
18331849
wakeReferences();
18341850
},
18351851
(error: unknown) => {
1836-
if (state.generation !== epoch) return;
1852+
if (generation !== epoch) return;
18371853
const wait = Math.max(
18381854
REFERENCE_RETRY,
18391855
error instanceof ReadError ? (error.retryAfterMs ?? 0) : 0,
@@ -2315,6 +2331,7 @@ export function createChannelStore(
23152331
// this disposable cache conservatively; pending writes use separate storage.
23162332
if (hadHydration) void persistence?.retain([]).catch(() => {});
23172333
setList(list);
2334+
resetReferences();
23182335
}
23192336
function dispose() {
23202337
disposed = true;

0 commit comments

Comments
 (0)