Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(cache): don't report cancelled query reads as cache errors (#7952)

teo1c6bba69322f
diff --git a/apps/web/src/lib/graphql-cache/exchange/document-query-reader.test.ts b/apps/web/src/lib/graphql-cache/exchange/document-query-reader.test.ts
index e995546e7e..341b7357c7 100644
--- a/apps/web/src/lib/graphql-cache/exchange/document-query-reader.test.ts
+++ b/apps/web/src/lib/graphql-cache/exchange/document-query-reader.test.ts
@@ -2,7 +2,10 @@ import fc from 'fast-check';
import { createComputed, createRoot } from 'solid-js';
import { describe, expect, it, vi } from 'vitest';
import { parseCacheRevision, type QueryUpdate } from '../protocol';
-import { createDocumentQueryReader } from './document-query-reader';
+import {
+ createDocumentQueryReader,
+ QueryReadInvalidatedError,
+} from './document-query-reader';
import { LiveQuery } from './live-query';
import { applyQueryPatches } from './query-patches';
@@ -200,7 +203,7 @@ describe('document query projection', () => {
reader.clear();
await reader.read(args);
finish(hit({ rows: ['old-account'] }, '100'));
- await expect(pending).rejects.toThrow('invalidated');
+ await expect(pending).rejects.toBeInstanceOf(QueryReadInvalidatedError);
await reader.read(args);
expect(watchQuery.mock.calls[2][0].since).toBe('0');
reader.forget(1);
diff --git a/apps/web/src/lib/graphql-cache/exchange/document-query-reader.ts b/apps/web/src/lib/graphql-cache/exchange/document-query-reader.ts
index f8fa10fe1b..2209f045e5 100644
--- a/apps/web/src/lib/graphql-cache/exchange/document-query-reader.ts
+++ b/apps/web/src/lib/graphql-cache/exchange/document-query-reader.ts
@@ -7,6 +7,14 @@ import {
import { isQueryObject } from './live-query';
import { applyQueryPatches } from './query-patches';
+/** Expected cancellation when teardown or a generation change drops the operation's cursor mid-read. */
+export class QueryReadInvalidatedError extends Error {
+ constructor() {
+ super('query read was invalidated');
+ this.name = 'QueryReadInvalidatedError';
+ }
+}
+
/** One cursor per active operation. Generation/disposal invalidates in-flight reads too. */
export function createDocumentQueryReader(
host: Pick<CacheHost, 'readQuery' | 'watchQuery'>
@@ -48,8 +56,7 @@ export function createDocumentQueryReader(
});
const full = !update || update.kind === 'unsupported';
const fallback = full ? await host.readQuery(args) : undefined;
- if (states.get(key) !== state)
- throw new Error('query read was invalidated');
+ if (states.get(key) !== state) throw new QueryReadInvalidatedError();
const revision = !full
? parseCacheRevision(update.revision)
: undefined;
diff --git a/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.test.ts b/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.test.ts
index a814f48dd3..59543ab739 100644
--- a/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.test.ts
+++ b/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.test.ts
@@ -48,6 +48,8 @@ import {
type MutationClaim,
type MutationSettlement,
OWNER_LOCK_UNAVAILABLE_ERROR_CODE,
+ parseCacheRevision,
+ type QueryUpdate,
type ReadResult,
type WriteResult,
} from '../protocol';
@@ -1570,6 +1572,63 @@ describe('normalizedCacheExchange', () => {
}
);
+ it.each(['teardown', 'generation change'] as const)(
+ 'does not report a cache read cancelled by %s',
+ async (cancel) => {
+ const read = deferred<ReadResult>();
+ host.readQuery = vi.fn(() => read.promise);
+ const onCacheError = vi.fn();
+ const { ops, results } = harness(host, undefined, { onCacheError });
+ const op = makeOp(1);
+ ops.next(op);
+ await tick();
+
+ if (cancel === 'teardown') ops.next(teardownOf(op));
+ else host.pushGeneration({ storage: 'preserved' });
+ read.resolve({ kind: 'hit', data: { from: 'cache' } });
+ await tick();
+
+ expect(onCacheError).not.toHaveBeenCalled();
+ expect(results.filter((r) => r.operation.kind === 'query')).toEqual([]);
+ }
+ );
+
+ it('does not report a watched reread cancelled by teardown', async () => {
+ const reread = deferred<QueryUpdate>();
+ host.watchQuery = vi
+ .fn()
+ .mockResolvedValueOnce({
+ kind: 'hit',
+ revision: parseCacheRevision('1'),
+ data: { from: 'cache' },
+ })
+ .mockReturnValueOnce(reread.promise);
+ const onCacheError = vi.fn();
+ const { ops, results } = harness(host, undefined, { onCacheError });
+ const op = makeOp(1);
+ ops.next(op);
+ await tick();
+ expect(results.map((result) => result.data)).toEqual([{ from: 'cache' }]);
+
+ host.pushAffected([1]);
+ await tick();
+ expect(host.watchQuery).toHaveBeenCalledTimes(2);
+ ops.next(teardownOf(op));
+ reread.resolve({
+ kind: 'hit',
+ revision: parseCacheRevision('2'),
+ data: { from: 'newer cache' },
+ });
+ await tick();
+
+ expect(onCacheError).not.toHaveBeenCalled();
+ expect(
+ results
+ .filter((r) => r.operation.kind === 'query')
+ .map((result) => result.data)
+ ).toEqual([{ from: 'cache' }]);
+ });
+
it.each(['miss', 'error'] as const)(
'does not forward twice when a concurrent cache read returns %s first',
async (outcome) => {
diff --git a/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.ts b/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.ts
index 68d96a0036..d3763746ec 100644
--- a/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.ts
+++ b/apps/web/src/lib/graphql-cache/exchange/normalized-cache-exchange.ts
@@ -78,7 +78,10 @@ import {
type QueryRevalidationWire,
} from '../protocol';
import { createDeferredQueryRereads } from './deferred-query-rereads';
-import { createDocumentQueryReader } from './document-query-reader';
+import {
+ createDocumentQueryReader,
+ QueryReadInvalidatedError,
+} from './document-query-reader';
import {
compileEntityResolvers,
type EntityResolverConfig,
@@ -417,6 +420,12 @@ export function normalizedCacheExchange(
/** Operations registered with the host, for push-driven re-execution. */
const activeOps = new Map<number, Operation>();
const documentReader = createDocumentQueryReader(host);
+ // Teardown and generation changes cancel document reads in flight. The
+ // read is already stale then, so the cancellation is not a cache failure.
+ const reportReadError = (error: unknown, op: Operation): void => {
+ if (!(error instanceof QueryReadInvalidatedError))
+ options.onCacheError?.(error, op);
+ };
let cacheGeneration = 0;
let mutationVersion = 0;
host.onMutationSettled((settlement) => {
@@ -662,7 +671,7 @@ export function normalizedCacheExchange(
)
);
})
- .catch((error) => options.onCacheError?.(error, operation));
+ .catch((error) => reportReadError(error, operation));
};
// Deliver cache changes directly. Reexecuting through urql first replays
@@ -689,7 +698,7 @@ export function normalizedCacheExchange(
return;
}
} catch (error) {
- options.onCacheError?.(error, operation);
+ reportReadError(error, operation);
if (!isCurrent()) return;
}
reexecuteAffected(key);
@@ -988,7 +997,7 @@ export function normalizedCacheExchange(
return cacheResult(op, undefined, false);
}
} catch (error) {
- options.onCacheError?.(error, op);
+ reportReadError(error, op);
if (
isOwnerEpochLostError(error) &&
queryStates.get(op.key) === readState &&
@@ -1238,7 +1247,10 @@ export function normalizedCacheExchange(
live.view.replace(read.data);
}
})
- .catch(reportError);
+ .catch((error) => {
+ if (!(error instanceof QueryReadInvalidatedError))
+ reportError(error);
+ });
}
if (state.retainedReplacementFallback === retained) {
state.retainedReplacementFallback = undefined;