Skip to content

Commit f051772

Browse files
committed
fix(desktop): don't cancel the fetch the timeline is waiting on
Converging review found this at P1, two lenses independently, both with probes against the installed query-core source. Reproduced here. The prefetch marker records that a channel had a prefetch requested, not which fetch is in flight. On a cold channel `prefetchQuery` does not start a fetch at all — TanStack dedupes onto the request already running (query.js honours `cancelRefetch` only when `state.data !== undefined`). So when the hover dwell fires after the click, which is the ordinary case for a decisive click inside the 100ms dwell, the marker labels the mounted timeline's own fetch a "prefetch", and the post-subscribe refresh cancelled it. Probe, real modules and a real QueryObserver: no hover: { calls: 1, mountFetchAborted: false } hover then click: { calls: 2, mountFetchAborted: true } That is two sequential relay round trips with the skeleton held across both, making a hovered channel slower to open than an unhovered one — on the exact path this stack exists to speed up. The refresh now waits for the in-flight fetch instead of cancelling it. The gap it was closing still closes: the invalidate below re-reads the window after the subscription exists, it just no longer discards the round trip the user is watching. #6455's one-fetch contract for a cold channel with no prefetch is unchanged. The guard's own comment claimed a cold mount fetch "is still parked on the hydration gate" and so could not be affected. That is false in steady state — `channelHeadHydration` is an already-resolved promise after boot. Adds the regression test; it fails on the cancelling version. Signed-off-by: Max Lampert <[email protected]>
1 parent a88994e commit f051772

2 files changed

Lines changed: 67 additions & 5 deletions

File tree

‎desktop/src/features/messages/lib/projectChannelWindow.test.mjs‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,62 @@ test("gap refresh refetches after an in-flight prefetch settles (no dedupe)", as
467467
client.clear();
468468
resetChannelWindowPrefetches();
469469
});
470+
471+
test("the post-subscribe refresh never aborts the fetch the timeline is watching", async () => {
472+
// Hover dwell fires AFTER the click: prefetchQuery does not start a fetch,
473+
// it dedupes onto the mount fetch the user is already watching a skeleton
474+
// for. The prefetch marker cannot tell those apart, so the refresh must
475+
// wait for the in-flight fetch rather than cancel it — cancelling made a
476+
// hovered channel slower to open than an unhovered one.
477+
resetChannelWindowPrefetches();
478+
const client = new QueryClient({
479+
defaultOptions: { queries: { retry: false } },
480+
});
481+
const channelId = "chan-hover-then-click";
482+
const queryKey = channelMessagesKey(channelId);
483+
let calls = 0;
484+
let mountSignal = null;
485+
let release;
486+
const gate = new Promise((resolve) => {
487+
release = resolve;
488+
});
489+
const options = {
490+
queryKey,
491+
queryFn: async ({ signal }) => {
492+
calls += 1;
493+
if (calls === 1) {
494+
mountSignal = signal;
495+
await gate;
496+
}
497+
return [event(`fetch-${calls}`, 100 + calls)];
498+
},
499+
staleTime: 300_000,
500+
};
501+
502+
// The click mounts the screen and starts the fetch it will paint.
503+
const observer = new QueryObserver(client, options);
504+
const unsubscribe = observer.subscribe(() => {});
505+
await new Promise((resolve) => setTimeout(resolve, 10));
506+
507+
// The dwell fires afterwards and merely joins that fetch.
508+
markChannelPrefetchStarted(channelId);
509+
void client
510+
.prefetchQuery(options)
511+
.finally(() => markChannelPrefetchSettled(channelId));
512+
await new Promise((resolve) => setTimeout(resolve, 10));
513+
514+
const refresh = refreshChannelWindowMessages(client, channelId);
515+
await new Promise((resolve) => setTimeout(resolve, 10));
516+
release();
517+
await Promise.allSettled([refresh]);
518+
await new Promise((resolve) => setTimeout(resolve, 50));
519+
520+
assert.equal(
521+
mountSignal?.aborted,
522+
false,
523+
"the mounted timeline's own fetch must not be aborted",
524+
);
525+
unsubscribe();
526+
client.clear();
527+
resetChannelWindowPrefetches();
528+
});

‎desktop/src/features/messages/lib/projectChannelWindow.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ export async function refreshChannelWindowMessages(
4747
await query.promise?.catch(() => {});
4848
} else if (hasInFlightChannelPrefetch(channelId)) {
4949
// A hover prefetch reached the relay before this subscription existed, so
50-
// its snapshot can miss events that landed in between. Cancel it so the
51-
// invalidate below issues a fetch that starts after this refresh; a cold
52-
// mount fetch is still parked on the hydration gate above, carries no such
53-
// gap, and keeps deduping.
54-
await queryClient.cancelQueries({ queryKey, exact: true });
50+
// its snapshot can miss events that landed in between — the invalidate
51+
// below re-reads the window after the subscription. Wait for the in-flight
52+
// fetch rather than cancelling it: by the time this runs the mounted
53+
// timeline has very likely joined that same fetch (TanStack dedupes onto a
54+
// cold query's in-flight request rather than starting a second one), so
55+
// cancelling would abort the fetch the user is watching a skeleton for and
56+
// make a hovered channel slower to open than an unhovered one.
57+
await query?.promise?.catch(() => {});
5558
}
5659
await queryClient.invalidateQueries(
5760
{ queryKey, exact: true, refetchType: "active" },

0 commit comments

Comments
 (0)