fix(ui): drop stale async responses with a generation guard (#1734)

TasksPage load/loadMore, use-task-run keyedLoad, TaskLogs loadOlder and
EventsPage loadCounts now each capture a generation at request start and
drop their writes unless they are still the latest request, the same
guard logs/state.ts already uses. dropCaches also stales every in-flight
keyed read, so a 30s timeout's null can no longer overwrite a newer
successful response, and the task-log pager no longer writes a page
fetched under the old attempt or filter.
This commit is contained in:
sam committed 2026-11-06 21:52:55 +11:00
1 parent d90f7d48d0
commit 7a8d948fcb
4 files changed
+45 -5

No files matched your search

+8 -1
View File
@@ -73,13 +73,20 @@ watch(active, (tab) => show(tab), { immediate: true });
// and the counting live in counts.ts, so the strip, the chips and the test all
// read one source rather than three.
const counts = ref<EventsCounts>({});
// A counts read bumps the generation; one that settles after a newer read
// started is dropped, so the previous workspace's counts never overwrite the
// current one.
let countsGeneration = 0;
async function loadCounts() {
const gen = ++countsGeneration;
const workspace = events.workspace;
counts.value = await loadEventCounts({
const loaded = await loadEventCounts({
captures: api.captures,
mappings: () => api.mappings(workspace),
bindings: () => api.bindings(workspace),
});
if (gen !== countsGeneration) return;
counts.value = loaded;
}
useLiveResource(null, () => void loadCounts(), 1000);
+11 -4
View File
@@ -77,7 +77,12 @@ const pageKey = computed(
() =>
`${props.taskId}|${resolvedAttempt.value}|${props.filters.level}|${props.filters.stage}`,
);
// A page fetch bumps the generation; one that settles after a newer fetch or
// an attempt/filter change is dropped, so an older page never lands under the
// current attempt or filter.
let pageGeneration = 0;
watch(pageKey, () => {
pageGeneration++;
page.value = null;
newerPages.value = [];
pageError.value = false;
@@ -95,10 +100,10 @@ const canOlder = computed(
const canNewer = computed(() => canPage.value && newerPages.value.length > 0);
/** Read the page ending just before cursor; zero reads the tail. */
async function fetchPage(before: number): Promise<LogState | null> {
async function fetchPage(before: number, gen: number): Promise<LogState | null> {
if (props.taskId == null) return null;
paging.value = true;
pageError.value = false;
if (gen === pageGeneration) pageError.value = false;
try {
return await api.taskLogs<LogState>(String(props.taskId), {
attempt: resolvedAttempt.value || undefined,
@@ -108,7 +113,7 @@ async function fetchPage(before: number): Promise<LogState | null> {
before: before || undefined,
});
} catch {
pageError.value = true;
if (gen === pageGeneration) pageError.value = true;
return null;
} finally {
paging.value = false;
@@ -116,10 +121,12 @@ async function fetchPage(before: number): Promise<LogState | null> {
}
async function loadOlder(): Promise<void> {
const gen = ++pageGeneration;
const shown = current.value;
const before = shown?.cursor ?? 0;
if (!shown?.more_available || before <= 0) return;
const older = await fetchPage(before);
const older = await fetchPage(before, gen);
if (gen !== pageGeneration) return;
if (!older) return;
await router.replace({ hash: "" });
newerPages.value.push({ before: page.value?.before ?? 0, state: shown });
+11
View File
@@ -100,16 +100,24 @@ const state = computed<"loading" | "error" | "empty" | "no-match">(() => {
const PAGE = 100;
// Every read bumps the generation; a response that settles after a newer read
// started is dropped, so the previous filter's rows or next cursor never land
// under the current filter.
let loadGeneration = 0;
// load re-reads the board from the top, as many rows as are showing, so a live
// update or a row action does not collapse pages the operator loaded.
async function load() {
const current = ++loadGeneration;
error.value = null;
const limit = Math.min(Math.max(tasks.value?.length ?? 0, PAGE), 500);
try {
const page = await api.taskPage<Task>({ status: status.value || undefined, limit });
if (current !== loadGeneration) return;
tasks.value = page.tasks;
next.value = page.next;
} catch (err) {
if (current !== loadGeneration) return;
error.value = String((err as Error).message || err);
tasks.value = null;
}
@@ -117,6 +125,7 @@ async function load() {
async function loadMore() {
if (!next.value || loadingMore.value) return;
const current = ++loadGeneration;
loadingMore.value = true;
try {
const page = await api.taskPage<Task>({
@@ -124,9 +133,11 @@ async function loadMore() {
limit: PAGE,
cursor: next.value,
});
if (current !== loadGeneration) return;
tasks.value = [...(tasks.value ?? []), ...page.tasks];
next.value = page.next;
} catch (err) {
if (current !== loadGeneration) return;
error.value = String((err as Error).message || err);
} finally {
loadingMore.value = false;
+15
View File
@@ -164,6 +164,13 @@ export function provideTaskRun(id: ComputedRef<number | null>): TaskRun {
debugKey.value == null ? undefined : debug.value.get(debugKey.value),
);
// Each keyed read gets a generation number; a response or timeout that
// settles after a newer read for the same key started is dropped, so a late
// null never overwrites a newer successful response. dropCaches bumps the
// global counter, which stales every in-flight read at once.
let cacheGeneration = 0;
const loadGenerations = new Map<string, number>();
function keyedLoad<T>(
cacheKey: string,
fetcher: () => Promise<T>,
@@ -171,6 +178,8 @@ export function provideTaskRun(id: ComputedRef<number | null>): TaskRun {
{ force = false, keep = false }: { force?: boolean; keep?: boolean } = {},
): void {
if (cache.value.has(cacheKey) && cache.value.get(cacheKey) !== null && !force) return;
const current = ++cacheGeneration;
loadGenerations.set(cacheKey, current);
// keep leaves a loaded payload on screen while it is re-read, so a live
// refresh replaces it in place instead of flashing the loading state.
if (!keep || cache.value.get(cacheKey) == null) cache.value = new Map(cache.value).set(cacheKey, undefined);
@@ -184,9 +193,11 @@ export function provideTaskRun(id: ComputedRef<number | null>): TaskRun {
});
Promise.race([fetcher(), timeout])
.then((res) => {
if (loadGenerations.get(cacheKey) !== current) return;
cache.value = new Map(cache.value).set(cacheKey, res);
})
.catch(() => {
if (loadGenerations.get(cacheKey) !== current) return;
cache.value = new Map(cache.value).set(cacheKey, null);
});
}
@@ -272,6 +283,10 @@ export function provideTaskRun(id: ComputedRef<number | null>): TaskRun {
/** Drop every cache so a manual refresh cannot show a stale capture or log
* beside fresh attempt history. */
function dropCaches(): void {
// Invalidate in-flight reads too: a response that lands after the drop
// would otherwise write its stale payload into the fresh caches.
cacheGeneration++;
loadGenerations.clear();
logs.value = new Map();
changes.value = new Map();
debug.value = new Map();