Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(cache): decide in the cache that a read without a signal kee…
…ps its Store call

`allAborted` now takes only signals, and answers for exactly the signals
that it gets. Before, an undefined element meant "this caller can never
cancel", and made the result have no signal. That rule belongs to the
code that shares the work, so `Cache.#readStore` now applies it: a Store
call with a read that has no signal asks for no signal at all.

The behaviour of the cache does not change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
  • Loading branch information
ethanresnick and claude committed Nov 24, 2026
commit 17fccb6fe9c082b53379bdaef309b5ae562cdcdb
11 changes: 8 additions & 3 deletions src/cache/Cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,15 +1025,20 @@ export default class Cache<
const autoBatched =
this.#dataStore.getMany !== undefined &&
reads.some((read) => !read.expedited);
const cancellation = allAborted(reads.map(({ input }) => input.signal));
const signals = reads.map(({ input }) => input.signal);
// A read without a signal can never be cancelled, so neither can a call
// that it is in.
const cancellation = signals.every((signal) => signal !== undefined)
? allAborted(signals)
: undefined;

try {
return await readStoreObserved(
this.#dataStore,
reads.map(({ input }) => input.request),
// Only the reads of a Store without `getMany` have a budget, and each
// of them is its own call.
{ signal: cancellation.signal, budget: reads[0]?.input.budget },
{ signal: cancellation?.signal, budget: reads[0]?.input.budget },
cacheReadStoreChannel.hasSubscribers
? (event) => {
cacheReadStoreChannel.publish({
Expand All @@ -1051,7 +1056,7 @@ export default class Cache<
: undefined,
);
} finally {
cancellation.stop();
cancellation?.stop();
}
}

Expand Down
22 changes: 2 additions & 20 deletions src/utils/allAborted.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,11 @@ const listenerCounts = (controllers: readonly AbortController[]) =>
controllers.map(({ signal }) => getEventListeners(signal, "abort").length);

describe("allAborted", () => {
it("gives no signal when there is no caller", () => {
it("gives no signal for an empty list", () => {
assert.equal(allAborted([]).signal, undefined);
});

it("gives no signal when no caller has a signal", () => {
assert.equal(allAborted([undefined, undefined]).signal, undefined);
});

it("gives no signal, and adds no listener, when only some callers have a signal", () => {
const controllers = [new AbortController(), new AbortController()];
const [a, b] = controllers.map(({ signal }) => signal);

for (const signals of [
[a, undefined, b],
[undefined, a, b],
[a, b, undefined],
]) {
assert.equal(allAborted(signals).signal, undefined);
}
assert.deepEqual(listenerCounts(controllers), [0, 0]);
});

it("gives the callers' own signal, and adds no listener, when every caller has the same signal", () => {
it("gives the same signal back, and adds no listener, when every element is that signal", () => {
const controller = new AbortController();
const { signal } = controller;

Expand Down
47 changes: 21 additions & 26 deletions src/utils/allAborted.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/** The signal for work that several callers share. See {@link allAborted}. */
export type AllAborted = {
/**
* Aborts once every caller's signal has aborted. Undefined when the work
* can never be cancelled: a caller has no signal, or there is no caller.
* Aborts once every one of the signals has aborted. Undefined when there
* is no signal at all: then nothing can cancel the work.
*/
readonly signal: AbortSignal | undefined;
/**
Expand All @@ -14,7 +14,7 @@ export type AllAborted = {

const stopNothing = (): void => {};

/** The answer for work that can never be cancelled. */
/** The answer for an empty list of signals. */
const NEVER: AllAborted = { signal: undefined, stop: stopNothing };

/**
Expand Down Expand Up @@ -59,39 +59,35 @@ class LastAbort {
}

/**
* Makes the signal for one piece of work that several callers share, from
* the signal of each caller. The work must stop only when no caller wants
* its result any more, so the returned signal aborts when every caller's
* signal has aborted. Its reason is the reason of the signal that aborted
* last. `AbortSignal.any` is the opposite: it aborts on the first abort.
* Makes a signal that aborts when every one of `signals` has aborted. Its
* reason is the reason of the signal that aborted last. `AbortSignal.any` is
* the opposite: it aborts on the first abort.
*
* The usual cases cost nothing: they add no listener, and make no signal.
*
* - No caller has a signal, or only some callers have one: the result has
* no signal, because the work can never be cancelled.
* - Every caller has the same signal: the result is that signal.
* It is for one piece of work that several callers share, where each signal
* is one caller's. Such work must stop only when no caller wants its result
* any more. A caller without a signal can never cancel, so the code that
* shares the work must not ask for a signal at all when it has such a
* caller: this function knows only the signals that it gets.
*
* - Every caller's signal has aborted already: the result is a signal that
* has aborted.
* The usual cases cost nothing: they add no listener, and make no signal.
*
* Other callers with different signals get a new signal that listens to
* theirs. A signal that occurs more than once counts once. A signal that has
* aborted already counts as aborted.
* - The list is empty: the result has no signal.
* - Every element is the same signal: the result is that signal.
* - Every signal has aborted already: the result is a signal that has
* aborted.
*
* @param signals The signal of each caller, or undefined for a caller that
* has none.
* Other lists get a new signal that listens to theirs. A signal that occurs
* more than once counts once. A signal that has aborted already counts as
* aborted.
*/
export function allAborted(
signals: readonly (AbortSignal | undefined)[],
): AllAborted {
export function allAborted(signals: readonly AbortSignal[]): AllAborted {
const [first] = signals;
if (first === undefined) return NEVER;

// One pass finds every case that needs no listener.
let allSame = true;
let allHaveAborted = true;
for (const signal of signals) {
if (signal === undefined) return NEVER;
if (signal !== first) allSame = false;
if (!signal.aborted) allHaveAborted = false;
}
Expand All @@ -105,6 +101,5 @@ export function allAborted(
};
}

// SAFETY: the loop above returned for any undefined element.
return new LastAbort(new Set(signals as readonly AbortSignal[]));
return new LastAbort(new Set(signals));
}
Loading