Skip to content

Commit deb8204

Browse files
committed
fix(signals): a derive's peek at a reader of its own keys settles (#4342)
A derived store's derive that peeks (untracked, NotReadyError caught) at a memo built from its own leaves re-ran without bound after a parent key change. The peek of a pending memo links it to the derive, and a leaf's reader is linked to the derive by the flight it observed (`pullFamily`). A held frame keeps both links after the passes that dropped them (A30). The derive's commit woke every reader linked to it on every pass, so a reader that builds a fresh object (a query key) re-ran the peeked memo, the memo re-ran the derive, and the derive re-ran the reader, all in one flush, until `queuePendingNode` threw a RangeError. #4307 made the wake park the leaves' readers on the derive itself, so a row's key is still pending when the derive's pass for the new parent key peeks at it; before, the parent key's landing had released it. The same loop predates #4307 when the peeked memo is pending on another source. A sync pass now wakes the derive's readers only if the derive was pending or errored before it; a landing wakes them as before (#4070). A settled derive's links are stale ones from readers that re-ran since. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 56a982c commit deb8204

5 files changed

Lines changed: 366 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
A derived store whose derive peeks (untracked, catching `NotReadyError`) at a memo built from its own keys settles instead of re-running without bound (#4342). A derive's synchronous pass no longer re-runs the readers left linked to it from a flight that has already landed. A held frame keeps those links, and re-running them on every pass made a loop: the reader, then the peeked memo, then the derive, all in one flush, until `queuePendingNode` threw a `RangeError`. This is the query-hook shape: a keyed list of rows, each row with its own hook, and a parent key switched while the list's answer is in flight. Landings still re-run the readers that observed the flight (#4070).

‎packages/signals/docs/RULES-INDEX.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
7474
| A27 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:301` | — | loading-value.test.ts×2 visibility-oracle.states.ts×18 visibility-oracle.test.ts×1 | [ruled 2026-08-10] The commit-#0 loading window is loading-class and verdict-quiet — (**ruled 2026-08-10**) **The commit-#0 loading window is loading-class and verdict-quiet.** A node born committed v… |
7575
| A28 | ruled, mechanism landed | `docs/SPEC-ASYNC-SEMANTICS.md:95` | constants.ts×1 core.ts×12 lanes.ts×2 scheduler.ts×3 types.ts×1 verdict.ts×2 store.ts×5 | continuation-sibling-reader-4146.test.ts×1 createOptimistic.test.ts×5 fuzz-findings-l2.test.ts×6 held-derivation-not-a-proposal-3612.test.ts×1 issue-3800-repro.test.ts×1 latest-held-till-flush.test.ts×1 posture-store-parity.test.ts×5 question-scoped-pending.test.ts×3 snapshot-derived-store-rows.test.ts×1 createOptimisticStore.test.ts×10 optimistic-draft-visibility-3665.test.ts×5 optimistic-list-mutation-matrix.harness.ts×1 optimistic-list-mutation-matrix.test.ts×2 shallow.test.ts×1 woken-transaction-adopts-staged-bump.test.ts×1 treeshake.test.ts×2 verdict-contract.test.ts×1 visibility-oracle-store.states.ts×8 visibility-oracle.states.ts×8 | [ruled, mechanism landed 2026-09-15] A write becomes visible at flush — to every channel — (**ruled 2026-09-08**; supersedes the #2922 mid-tick pull) **A write becomes visible at flush — to every chan… |
7676
| A29 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:119` | boundaries.ts×1 action.ts×1 constants.ts×1 core.ts×14 effect.ts×1 lanes.ts×1 scheduler.ts×4 signals.ts×1 store.ts×1 | adoption-unchanged-key-read-3706.test.ts×9 body-end-supersession-visibility.test.ts×1 born-held.test.ts×3 boundary-not-born-held-3540.test.ts×4 createProjection.draft-lifetime-3585.test.ts×1 direct-commit-readers-posture.test.ts×1 fuzz-findings-l2.test.ts×6 held-branch-verdict-reader-4010.test.ts×1 held-conditional-memo.test.ts×1 held-frame-dependencies.test.ts×2 held-truth-lane-only.test.ts×3 l2-contract.test.ts×1 latest-held-till-flush.test.ts×2 loading-fallback-in-flush-3540.test.ts×6 loading-on-frame-following-3540.test.ts×1 mount-over-foreign-hold-3761.test.ts×1 optimistic-mount-nested-memo-3835.test.ts×1 optimistic-read-lane-not-transaction-3698.test.ts×3 posture-born-held-and-observation.test.ts×1 posture-store-parity.test.ts×6 derived-presence-async-3726.test.ts×3 optimistic-untracked-reads-f3-f5.test.ts×1 store-unchanged-read-independent-write-3688.test.ts×1 tick-scoped-pass-transaction.test.ts×2 treeshake.test.ts×3 verdict-mount-first-pass-3851.test.ts×2 verdict-mount-loading-flush-loop.test.ts×2 visibility-oracle-store.states.ts×5 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×9 visibility-oracle.test.ts×1 write-proposals-3494.test.ts×1 | [ruled, amended in place 2026-09-13 (#3408)] A tracked read served a live transaction's staged value enters that transaction — A tracked computation served a node's staged `_pendingValue` — a value a … |
77-
| A30 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:257` | async.ts×3 attribution.ts×1 constants.ts×1 core.ts×1 effect.ts×1 lanes.ts×1 scheduler.ts×5 | async-landing-deps-3461.test.ts×3 fuzz-findings-l2.test.ts×3 held-conditional-effect.test.ts×1 held-conditional-memo.test.ts×1 held-frame-dependencies.test.ts×2 ispending-in-boundary-on-3528.test.ts×1 lane-frame-deferred-run-3662.test.ts×1 lane-frame-held-lane-3662.test.ts×1 posture-born-held-and-observation.test.ts×1 treeshake.test.ts×2 write-proposals-3494.test.ts×2 zombie-rerun-after-commit-3546.test.ts×2 | [ruled 2026-09-13 (#3410)] A memo's dependencies are the committed frame's until the frame is replaced — A pass that _staged_ its value has not replaced the committed frame, so the committed value sti… |
77+
| A30 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:257` | async.ts×3 attribution.ts×1 constants.ts×1 core.ts×1 effect.ts×1 lanes.ts×1 scheduler.ts×5 projection.ts×1 | async-landing-deps-3461.test.ts×3 fuzz-findings-l2.test.ts×3 held-conditional-effect.test.ts×1 held-conditional-memo.test.ts×1 held-frame-dependencies.test.ts×2 ispending-in-boundary-on-3528.test.ts×1 lane-frame-deferred-run-3662.test.ts×1 lane-frame-held-lane-3662.test.ts×1 posture-born-held-and-observation.test.ts×1 projection-peek-loop-4342.test.ts×1 treeshake.test.ts×2 write-proposals-3494.test.ts×2 zombie-rerun-after-commit-3546.test.ts×2 | [ruled 2026-09-13 (#3410)] A memo's dependencies are the committed frame's until the frame is replaced — A pass that _staged_ its value has not replaced the committed frame, so the committed value sti… |
7878
| A31 | live | `docs/SPEC-ASYNC-SEMANTICS.md:131` | boundaries.ts×1 core.ts×1 lanes.ts×1 verdict.ts×2 | fuzz-findings-l2.test.ts×6 ispending-combined-atomic-3442.test.ts×1 | [live 2026-09-14 (#3442)] A memo computes under its own lane posture, never its puller's — A memo's value is one shared slot every reader sees, so its pass runs under the lane posture the memo itself … |
7979
| A32 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:139` | core.ts×4 lanes.ts×1 store.ts×1 | visibility-oracle-store.states.ts×8 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×9 visibility-oracle.test.ts×1 | [ruled 2026-09-14] Children-forbidden readers see the frame, not the graph — `createTrackedEffect` and `onSettled` callbacks are effect-phase code that runs after the frame is decided. They read the f… |
8080
| A33 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:273` | boundaries.ts×1 scheduler.ts×2 | async-chain-supersession.test.ts×2 boundary-not-born-held-3540.test.ts×2 create-deferred-3710.test.ts×1 fuzz-findings-l2.test.ts×4 ispending-in-boundary-on-3528.test.ts×2 loading-reset-collects-forwarded-3459.test.ts×3 | [ruled 2026-09-12 (#3375)] A fallback-caught flight holds no transaction; a Loading reset moves the hold onto the boundary — A `<Loading>` boundary showing its fallback is the display of everything un… |

‎packages/signals/src/store/projection.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export function runProjectionComputed<T extends object>(
258258
draft,
259259
s => {
260260
result = fn((shadow ?? s) as T);
261-
const commit = (v: void | T) => {
261+
const commit = (v: void | T, wake: number = 1) => {
262262
// Shadow run: commit a detached snapshot, never the shadow itself
263263
// (adoption takes the value by identity — handing it the live
264264
// shadow would fuse the draft to the observable store).
@@ -282,8 +282,11 @@ export function runProjectionComputed<T extends object>(
282282
// A commit is the derive's change for the readers that observed its
283283
// flight (linked by `pullFamily`; a settled pull does not link), as
284284
// a memo's landing is for its readers: one that caught the NotReady
285-
// (a fallback value, not pending) re-runs too (#4070).
286-
insertSubs(owner);
285+
// (a fallback value, not pending) re-runs too (#4070). A pass of a
286+
// settled derive has none (`wake` 0): a link left is a stale one a
287+
// held frame keeps (A30), from a reader that re-ran since. Woken by
288+
// every pass, a reader feeding a key the pass peeks at looped (#4342).
289+
if (wake) insertSubs(owner);
287290
};
288291
const sync = handleAsync(owner, result, commit, (error, pending) => {
289292
// The flight rejected: the leaves' readers learn it from here —
@@ -296,7 +299,9 @@ export function runProjectionComputed<T extends object>(
296299
if (pending) wakeFamily(fam);
297300
else errorFamily(fam);
298301
});
299-
if (!owner._loading) commit(sync as void | T);
302+
// (The status is the one before this pass: `recompute` sets it after.)
303+
if (!owner._loading)
304+
commit(sync as void | T, owner._statusFlags & (STATUS_PENDING | STATUS_ERROR));
300305
},
301306
false
302307
);
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
/**
2+
* #4342: a derived store's pass that peeks (untracked, `NotReadyError`
3+
* caught) at a memo built from one of its own leaves does not re-run without
4+
* bound. The peek of a pending memo links it, so the memo's change re-runs
5+
* the derive; the reader of the leaf is linked to the derive by the flight it
6+
* observed. A held frame keeps both links after the passes that dropped them
7+
* (A30). The derive's sync pass woke every reader linked to it, so a reader
8+
* that builds a fresh object (a query key) re-ran the memo, the memo re-ran
9+
* the derive, and the derive re-ran the reader, all in one flush. A pass of
10+
* a settled derive now wakes no reader; a landing still does (#4070).
11+
*
12+
* "issue": the report's shape (a query hook whose projection peeks at every
13+
* mounted hook's key on a cache miss, a keyed list of rows each with its own
14+
* hook, a parent key from an async memo). The regression of #4307: the wake
15+
* parks the rows' readers on the derive, so a row's key is still pending
16+
* when the derive's pass for the new parent key peeks at it.
17+
* "rows" / "direct": the same loop without boundaries or a row hook, through
18+
* a keyed `mapArray` and through a reader of the leaf with no list.
19+
* "other-source": the peeked key is pending on a different async memo, not on
20+
* the derive (this loop predates #4307).
21+
*/
22+
import { describe, expect, it } from "vitest";
23+
import {
24+
createErrorBoundary,
25+
createLoadingBoundary,
26+
createMemo,
27+
createProjection,
28+
createRenderEffect,
29+
createRoot,
30+
createSignal,
31+
flush,
32+
mapArray,
33+
NotReadyError,
34+
onCleanup,
35+
untrack
36+
} from "../src/index.js";
37+
38+
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
39+
const tick = async () => {
40+
for (let i = 0; i < 3; i++) {
41+
await wait(0);
42+
flush();
43+
}
44+
};
45+
const peekAll = (peeks: Iterable<() => unknown>) => {
46+
for (const peek of peeks)
47+
try {
48+
untrack(peek);
49+
} catch (err) {
50+
if (!(err instanceof NotReadyError)) throw err;
51+
}
52+
};
53+
const resolveAll = (v: any): string =>
54+
typeof v === "function"
55+
? resolveAll(v())
56+
: Array.isArray(v)
57+
? v.map(resolveAll).join("")
58+
: String(v ?? "");
59+
60+
describe("#4342: a derive's peek at a reader of its own leaf", () => {
61+
it("issue: switching the parent key settles", async () => {
62+
const keys = new Set<() => { hash: string }>();
63+
let runs = 0;
64+
function useQuery<T>(keyFn: () => string, fetcher: () => Promise<T>) {
65+
const key = createMemo(() => ({ hash: keyFn() }));
66+
keys.add(key);
67+
onCleanup(() => keys.delete(key));
68+
const cache = new Map<string, { p: Promise<{ value: T }>; d?: T }>();
69+
const data = createProjection<{ value?: T }>(() => {
70+
const k = key().hash;
71+
let e = cache.get(k);
72+
if (!e) {
73+
const entry: { p: Promise<{ value: T }>; d?: T } = {
74+
p: fetcher().then(d => ((entry.d = d), { value: d }))
75+
};
76+
cache.set(k, (e = entry));
77+
peekAll(keys);
78+
}
79+
return e.d !== undefined ? { value: e.d } : e.p;
80+
}, {});
81+
return () => data.value as T;
82+
}
83+
const [id, setId] = createSignal("A");
84+
let release!: (v: { key: string }[]) => void;
85+
const later = new Promise<{ key: string }[]>(r => (release = r));
86+
const Item = (props: { link: string; item: { key: string } }) => {
87+
const opts = createMemo(() => {
88+
if (++runs > 200) throw new Error("runaway");
89+
return { link: props.link, item: props.item.key };
90+
});
91+
useQuery(
92+
() => `${opts().link}:${opts().item}`,
93+
async () => 0
94+
);
95+
return () => props.item.key;
96+
};
97+
const List = (props: { link: string }) => {
98+
const list = useQuery(
99+
() => props.link,
100+
() => (props.link === "A" ? Promise.resolve([{ key: "a" }]) : later)
101+
);
102+
return createLoadingBoundary(
103+
() =>
104+
createErrorBoundary(
105+
() =>
106+
mapArray(
107+
() => list(),
108+
x =>
109+
untrack(() =>
110+
Item({
111+
get link() {
112+
return props.link;
113+
},
114+
get item() {
115+
return x();
116+
}
117+
})
118+
),
119+
{ keyed: (x: { key: string }) => x.key }
120+
),
121+
e => String(e())
122+
),
123+
() => "..."
124+
);
125+
};
126+
let screen = "";
127+
const dispose = createRoot(d => {
128+
const link = createMemo(async () => {
129+
const v = id();
130+
await Promise.resolve();
131+
return v;
132+
});
133+
const out = createLoadingBoundary(
134+
() =>
135+
untrack(() =>
136+
List({
137+
get link() {
138+
return link();
139+
}
140+
})
141+
),
142+
() => "..."
143+
);
144+
createRenderEffect(
145+
() => resolveAll(out),
146+
v => {
147+
screen = v;
148+
}
149+
);
150+
return d;
151+
});
152+
flush();
153+
await wait(20);
154+
expect(screen).toBe("a");
155+
const before = runs;
156+
setId("B");
157+
flush();
158+
await wait(20);
159+
release([{ key: "b" }]);
160+
await wait(20);
161+
expect(screen).toBe("b");
162+
expect(runs - before).toBeLessThan(10);
163+
dispose();
164+
});
165+
166+
for (const mode of ["rows", "direct", "other-source"] as const) {
167+
it(`${mode}: the landing settles`, async () => {
168+
const [id, setId] = createSignal("A");
169+
const res: Record<string, (v: { key: string }[]) => void> = {};
170+
const slowRes: Record<string, (v: string) => void> = {};
171+
let runs = 0;
172+
let screen = "";
173+
const dispose = createRoot(d => {
174+
// "other-source": the readers wait on `slow`; the derive's input is sync.
175+
const link =
176+
mode === "other-source"
177+
? id
178+
: createMemo(async () => {
179+
const v = id();
180+
await Promise.resolve();
181+
return v;
182+
});
183+
// The rows' other input: the parent key itself, else a slower memo.
184+
const slow =
185+
mode === "other-source"
186+
? createMemo(() => {
187+
const v = id();
188+
return v === "A" ? v : new Promise<string>(r => (slowRes[v] = r));
189+
})
190+
: link;
191+
const peeks: (() => unknown)[] = [];
192+
const cache: Record<string, { p: Promise<{ rows: { key: string }[] }>; d?: any }> = {};
193+
const p = createProjection<{ rows?: { key: string }[] }>(() => {
194+
const k = link();
195+
let e = cache[k];
196+
if (!e) {
197+
const entry: (typeof cache)[string] = {
198+
p: new Promise<{ key: string }[]>(r => (res[k] = r)).then(
199+
v => ((entry.d = v), { rows: v })
200+
)
201+
};
202+
e = cache[k] = entry;
203+
peekAll(peeks);
204+
}
205+
return e.d ? { rows: e.d } : e.p;
206+
}, {});
207+
const row = (item: () => string | undefined) => {
208+
const opts = createMemo(() => {
209+
if (++runs > 200) throw new Error("runaway");
210+
return { s: slow(), item: item() };
211+
});
212+
const key = createMemo(() => ({ hash: `${opts().s}:${opts().item}` }));
213+
peeks.push(key);
214+
return () => key().hash;
215+
};
216+
const view =
217+
mode === "direct"
218+
? (f => () => [f])(row(() => p.rows?.[0]?.key))
219+
: mapArray(
220+
() => p.rows,
221+
r => row(() => r().key),
222+
{ keyed: (r: { key: string }) => r.key }
223+
);
224+
createRenderEffect(
225+
() =>
226+
view()
227+
.map(f => f())
228+
.join(","),
229+
v => {
230+
screen = v;
231+
}
232+
);
233+
return d;
234+
});
235+
flush();
236+
await tick();
237+
res.A([{ key: "a" }]);
238+
await tick();
239+
expect(screen).toBe("A:a");
240+
const before = runs;
241+
setId("B");
242+
flush();
243+
await tick();
244+
if (mode === "other-source") {
245+
slowRes.B("B");
246+
await tick();
247+
}
248+
res.B([{ key: "b" }]);
249+
await tick();
250+
expect(screen).toBe("B:b");
251+
expect(runs - before).toBeLessThan(10);
252+
dispose();
253+
});
254+
}
255+
});

0 commit comments

Comments
 (0)