Skip to content

Commit a78d91a

Browse files
committed
fix(signals): a reader mounted mid-flight reads a derived store like its memo (#4491)
`createStore(d => { d.v = m1() })` over an async memo whose input reads `latest()` has a derive that is a verdict reader through what it reads (D-25 / F-6). At a re-ask it is served `m1`'s committed value and settles over it, so its leaves read as final: a reader that mounted mid-flight showed the old question's answer before the new one (in the mounting flush once #4479 skips the unread derive, at the landing before it), where a reader of `m1()` is held (F-2). D-30: a derived store reads like its memo. `pullFamily`: a tracked reader of a settled verdict-reader derive with no window of its own reads the flights among the derive's deps, as the memo's reader would, so it is held; the derive is that hold's work until it answers. `runProjectionComputed`: a derive whose pending is on its leaves' readers (`fam.woke`) reads plainly, as a pending node's pass does (#3884), so its landing is the answer the held reader shows. Store-only: no frozen floor moves. The #4479 review's probe forms (key read, Object.keys, JSON) are pinned against the memo form: mounted mid-flight with and without a plain write beside the flight, on screen at the re-ask, and mounted after the landing. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent d0fc285 commit a78d91a

7 files changed

Lines changed: 212 additions & 7 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 reader of a derived store that mounts while the derive's input is in flight is held until it answers, as a reader of the async memo is, instead of showing the previous answer first: `createStore(d => { d.v = m1() })` over a memo that reads `latest()`, read mid-flight, no longer shows the old question's value before the new one (#4491).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ Effect-phase code that runs after the frame is decided reads the frame as it sta
252252

253253
**From:** F-9, F-1. **Scenarios:** S-36, S-37.
254254

255-
Pending is per node — per store key: a held write to a plain store pends exactly the touched keys; a derived store's leaves report the derive's new-question refetch like any async memo, in both forms, and a quiet re-ask (`refresh(store)`) is quiet on them too. A derived store's landing resumes the frame that asked it: when a consumer of its leaves goes async, the landed leaves are held with that frame and read stale and pending until it reveals (signal parity); an untouched sibling key is final. A derived store's seed is a draft for the derive, never an observable value: until the first resolution (first yield for a generator) every outside read — tracked, untracked, `in`, enumeration — throws `NotReadyError` (DEV in a component body: `PENDING_ASYNC_UNTRACKED_READ`); write-path reads see it. A seedless derived store (`createStore(fn)` with a derive that takes no draft) starts from an empty record, the same unobservable draft. `loadingValue` / `seedLoadingValue` promote a first value to commit #0: reads serve it, no boundary trips, no hold starts or extends, `isPending` is `false` on every axis until the first real landing — a hydration invariant (the server always answers `false`) — after which the node is an ordinary memo.
255+
Pending is per node — per store key: a held write to a plain store pends exactly the touched keys; a derived store's leaves report the derive's new-question refetch like any async memo, in both forms, and a quiet re-ask (`refresh(store)`) is quiet on them too. So do the leaves of a derive that is a verdict reader only through what it reads (D-25) and was served a flight's committed value: a tracked reader of them reads the flight as the memo's reader would — one that mounts mid-flight is held (F-2) and shows the flight's answer, never the old question's — and the derive computes that landing plainly, as the hold's work (D-25) (#4491, `tests/unread-derive-midflight-mount-4491.test.ts`). A derived store's landing resumes the frame that asked it: when a consumer of its leaves goes async, the landed leaves are held with that frame and read stale and pending until it reveals (signal parity); an untouched sibling key is final. A derived store's seed is a draft for the derive, never an observable value: until the first resolution (first yield for a generator) every outside read — tracked, untracked, `in`, enumeration — throws `NotReadyError` (DEV in a component body: `PENDING_ASYNC_UNTRACKED_READ`); write-path reads see it. A seedless derived store (`createStore(fn)` with a derive that takes no draft) starts from an empty record, the same unobservable draft. `loadingValue` / `seedLoadingValue` promote a first value to commit #0: reads serve it, no boundary trips, no hold starts or extends, `isPending` is `false` on every axis until the first real landing — a hydration invariant (the server always answers `false`) — after which the node is an ordinary memo.
256256

257257
**Collapses:** [A9](./SPEC-ASYNC-SEMANTICS.md#a9-store-leaves-behind-a-firewall-report-the-firewalls-new-question-refetch), [A22](./SPEC-ASYNC-SEMANTICS.md#a22-pending-is-per-node-store-wide-only-for-the-firewalls-own-work) (amended 2026-10-03), [A25](./SPEC-ASYNC-SEMANTICS.md#a25-a-derived-stores-seed-is-a-draft-never-an-observable-value), [A27](./SPEC-ASYNC-SEMANTICS.md#a27-the-commit-0-loading-window-is-loading-class-and-verdict-quiet), the L2 stores ruling (statement 7), OS-R30–R36, PJ-R23/R30/R31. **Status:** standing; one adoption shape pinned `it.fails` (`adoption-unchanged-key-read-3706.test.ts`, a row first read after the adoption).
258258

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
5959
| A12 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:199` | — | createOptimistic.test.ts×2 spec-async-semantics.test.ts×1 | [ruled, amended in place] Resting optimistic nodes report pending like a plain memo — A resting optimistic node reports pending via exactly the causes a plain async memo does (A19) — a reverting optim… |
6060
| A13 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:207` | — | spec-async-semantics.test.ts×7 | [ruled 2026-07-06 (promoted from B1)] Resting optimistic ≡ plain async memo at every checkpoint — (was B1) A resting optimistic node (no active override) is observationally identical to a plain async … |
6161
| A14 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:215` | — | spec-async-semantics.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B2)] Companion nodes get child lanes that do not merge with the owner — (was B2) `isPending`/`latest` companion nodes get child lanes that do not mer… |
62-
| A15 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:251` | async.ts×2 constants.ts×1 core.ts×4 lanes.ts×6 scheduler.ts×16 seat.ts×1 verdict.ts×4 | async-chain-supersession.test.ts×1 boundary-output-frame-reader.test.ts×1 create-deferred-3710.test.ts×2 first-observer-stale-reader.test.ts×1 fuzz-findings-l2.test.ts×17 held-restore.test.ts×1 hold-recovers-landing-reruns-4469.test.ts×1 l2-contract.test.ts×7 l2-fuzz-existing-rules.test.ts×2 lane-contract.test.ts×1 lane-hold-on-observation.test.ts×1 lane-outside-view.test.ts×1 lane-pass-stamped-effect-3662.test.ts×1 latest-memo-inline-parity.test.ts×1 latest-uninitialized-over-latest-4362.test.ts×1 loading-fallback-in-flush-3540.test.ts×4 mount-over-foreign-hold-3761.test.ts×2 overlapping-flights.test.ts×3 posture-born-held-and-observation.test.ts×4 posture-store-parity.test.ts×2 reveal-carve-out.test.ts×2 shared-effect-no-entangle.test.ts×1 show-stale-child-in-action-4137.test.ts×2 spec-async-semantics.test.ts×2 stale-read-uninitialized-cross-transition.test.ts×1 superseded-source-blocks-3462.test.ts×2 tick-scoped-pass-transaction.test.ts×1 treeshake.test.ts×4 verdict-memo-flight-no-reask.test.ts×1 visibility-oracle-store.states.ts×6 visibility-oracle.states.ts×7 visibility-oracle.test.ts×1 write-proposals-3494.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B3)] Transition entanglement is graph-driven; lanes settle as one reveal — (was B3) Transition entanglement is graph-driven: writes whose async work … |
62+
| A15 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:251` | async.ts×2 constants.ts×1 core.ts×4 lanes.ts×6 scheduler.ts×16 seat.ts×1 verdict.ts×4 store.ts×1 | async-chain-supersession.test.ts×1 boundary-output-frame-reader.test.ts×1 create-deferred-3710.test.ts×2 first-observer-stale-reader.test.ts×1 fuzz-findings-l2.test.ts×17 held-restore.test.ts×1 hold-recovers-landing-reruns-4469.test.ts×1 l2-contract.test.ts×7 l2-fuzz-existing-rules.test.ts×2 lane-contract.test.ts×1 lane-hold-on-observation.test.ts×1 lane-outside-view.test.ts×1 lane-pass-stamped-effect-3662.test.ts×1 latest-memo-inline-parity.test.ts×1 latest-uninitialized-over-latest-4362.test.ts×1 loading-fallback-in-flush-3540.test.ts×4 mount-over-foreign-hold-3761.test.ts×2 overlapping-flights.test.ts×3 posture-born-held-and-observation.test.ts×4 posture-store-parity.test.ts×2 reveal-carve-out.test.ts×2 shared-effect-no-entangle.test.ts×1 show-stale-child-in-action-4137.test.ts×2 spec-async-semantics.test.ts×2 stale-read-uninitialized-cross-transition.test.ts×1 superseded-source-blocks-3462.test.ts×2 tick-scoped-pass-transaction.test.ts×1 treeshake.test.ts×4 unread-derive-midflight-mount-4491.test.ts×1 verdict-memo-flight-no-reask.test.ts×1 visibility-oracle-store.states.ts×6 visibility-oracle.states.ts×7 visibility-oracle.test.ts×1 write-proposals-3494.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B3)] Transition entanglement is graph-driven; lanes settle as one reveal — (was B3) Transition entanglement is graph-driven: writes whose async work … |
6363
| A16 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:223` | scheduler.ts×1 | latest-uninitialized-over-latest-4362.test.ts×2 spec-async-semantics.test.ts×1 strict-read-pending-store.test.ts×2 uninitialized-visibility.test.ts×1 visibility-oracle-store.states.ts×2 visibility-oracle.states.ts×2 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from B5)] `isPending` never throws in untracked contexts — (was B5) `isPending` never throws in untracked contexts — thunks that throw real errors or read… |
6464
| A17 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:71` | async.ts×3 constants.ts×1 lanes.ts×7 scheduler.ts×2 seat.ts×1 verdict.ts×1 map.ts×1 optimistic.ts×1 store.ts×3 | create-deferred-3710.test.ts×1 fuzz-findings-l2.test.ts×6 lane-uninitialized-landing-3648.test.ts×5 optimistic-over-held-row-3796.test.ts×1 optimistic-read-lane-not-transaction-3698.test.ts×2 optimistic-undefined-override.test.ts×1 posture-store-parity.test.ts×1 refresh-await.test.ts×1 reveal-gating-contract.test.ts×3 spec-async-semantics.test.ts×10 createOptimisticStore.test.ts×2 kanban-a17-fixture.test.ts×3 optimistic-list-mutation-matrix.test.ts×1 optimistic-maparray-index-frame-f1.test.ts×1 optimistic-untracked-reads-f3-f5.test.ts×1 signal-store-twins-qd.test.ts×1 treeshake.test.ts×1 until.test.ts×1 visibility-oracle-store.states.ts×24 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×20 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-06 (promoted from C4)] An active override is the displayed value until its transaction commits, and the graph's value until its own source answers — **Statement (curre… |
6565
| A18 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:81` | action.ts×1 async.ts×2 core.ts×3 lanes.ts×8 scheduler.ts×2 types.ts×1 verdict.ts×1 map.ts×1 optimistic.ts×1 projection.ts×1 | body-end-supersession-visibility.test.ts×4 createOptimistic.test.ts×3 l2-fuzz-existing-rules.test.ts×2 lane-contract.test.ts×1 lane-frame-deferred-run-3662.test.ts×1 lane-outside-view.test.ts×1 lane-uninitialized-landing-3648.test.ts×5 optimistic-move-duplicate-3548.test.ts×2 optimistic-read-lane-not-transaction-3698.test.ts×4 posture-store-parity.test.ts×5 spec-async-semantics.test.ts×5 flight-owned-transaction.test.ts×1 lane-authority-twins.test.ts×1 optimistic-list-mutation-matrix.test.ts×1 optimistic-untracked-reads-f3-f5.test.ts×1 signal-store-twins-qd.test.ts×1 unchanged-presence-no-hold-3743.test.ts×2 superseded-before-first-commit.test.ts×4 visibility-oracle-store.states.ts×8 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×19 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-07-07 (promoted from B4)] An override lives exactly as long as its own transaction; a newer truth from the source supersedes it in the graph immediately, on screen at com… |

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import { attrHooks } from "../core/attribution-hooks.js";
2020
import {
2121
CONFIG_AUTO_DISPOSE,
22+
CONFIG_VERDICT,
2223
STATUS_ERROR,
2324
STATUS_PENDING,
2425
STATUS_UNINITIALIZED
@@ -239,6 +240,14 @@ export function runProjectionComputed<T extends object>(
239240
const target: StoreTarget = (wrappedStore as any)[$TARGET];
240241
const fam = target.fam!;
241242
const run = (fam.run = (fam.run || 0) + 1);
243+
// The derive's pending is on its leaves' readers (`wakeFamily`) and no
244+
// landing has settled them: it is pending, and its pass is the hold's work
245+
// as a pending node's is (core `recompute`, #3884, D-25): it reads plainly,
246+
// so the landing it computes is the one a reader held on it shows, not the
247+
// screen a frame reader is served (#4491; D-30, F-2). A window in the pass
248+
// re-stamps the posture (`markVerdictReader`). (The settle walk clears the
249+
// derive's own pending before the landing re-runs it.)
250+
if (fam.woke) owner._config &= ~CONFIG_VERDICT;
242251
let result: void | T | Promise<void | T> | AsyncIterable<void | T>;
243252
// Open loading window (seedLoadingValue): the observable store IS commit #0
244253
// for the whole first flight — the derive works a detached shadow of the

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,28 @@ function pullFamily(target: StoreTarget): void {
18691869
// (optimistic.ts `stands`, #4336).
18701870
if (verdict === null && fw._statusFlags & STATUS_PENDING && optHooks?.stands(target)) return;
18711871
readNode(fw);
1872-
} else untrack(() => readNode(fw));
1872+
} else {
1873+
untrack(() => readNode(fw));
1874+
// #4491: a derive that is a verdict reader only through what it reads (a
1875+
// verdict memo's value, D-25 / F-6; no window of its own) is served the
1876+
// committed value of a flight it reads (`observeFlight`, A15's stale
1877+
// reader) and settles over it: its leaves are the screen, not the
1878+
// flight's answer. A derived store reads like its memo (D-30): a tracked
1879+
// reader of the leaves reads that flight as the memo's reader would, so
1880+
// one that mounts mid-flight consumes unanswered async and is held (F-2)
1881+
// instead of shown the old question's answer. The derive is that hold's
1882+
// work until it answers (D-25): its next pass reads plainly, and the
1883+
// landing it computes is the one the held reader shows, not the screen.
1884+
// A pass that reads the verdict memo again is a verdict reader again
1885+
// (core `recompute`); one that goes pending stays plain while its pending
1886+
// is on the leaves (`fam.woke`, projection.ts). (The window flag is
1887+
// REACTIVE_WINDOW, spelled from EFFECT_RENDER: an import of it keeps a
1888+
// dead copy in bundles without the store.)
1889+
if (fw._config & CONFIG_VERDICT && !(fw._flags & (EFFECT_RENDER << 21)) && tracking)
1890+
for (let l = fw._deps; l; l = l._nextDep)
1891+
if ((l._dep as Computed<any>)._statusFlags & STATUS_PENDING)
1892+
((fw._config &= ~CONFIG_VERDICT), readNode(l._dep));
1893+
}
18731894
}
18741895

18751896
/** A projection's writes are its derive's: a staging made while the derive
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
/**
2+
* #4491: a reader that mounts while an unread derived store's derive has a
3+
* flight up reads the store like its memo (D-30, "in both forms").
4+
*
5+
* `createStore(d => { d.v = m1() }, {})` over `m1 = async(m0)` with
6+
* `m0 = memo(latest(s) + s)`: `m0` is a verdict reader, so `m1` and the
7+
* derive are verdict readers through what they read (D-25 / F-6). Nothing
8+
* reads the store when `m1` re-asks (s: 1 -> 2); a reader of the store then
9+
* mounts mid-flight.
10+
*
11+
* - memo form (the reader reads `m1()`): the mount consumes unanswered async
12+
* and is held until `m1` answers (F-2): ["r:off", "--mounted", "r:4"].
13+
* - store form, before: the derive was served `m1`'s committed value and
14+
* settled over it (A15's stale reader), so its leaf read as final. The
15+
* reader showed the old question's answer `r:2` before `r:4`: in the
16+
* mounting flush itself when the unread derive skips the flight (#4479),
17+
* at the landing otherwise (the derive's landing pass, a frame reader,
18+
* read the screen).
19+
*
20+
* The forms the #4479 review probed (a key read, `Object.keys`, a JSON
21+
* walk) must match the memo form, mounted mid-flight with and without a
22+
* plain write beside the flight (with it, the store form showed `r:5`, an
23+
* answer to a question between the two, before `r:6`). So must a reader
24+
* already on screen at the re-ask, with the plain write (before the fix the
25+
* whole-object readers showed `s:3` beside the old `v` ahead of `r:4`), and
26+
* a reader mounted after the landing, which shows at once: the hold reaches
27+
* neither.
28+
*/
29+
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
30+
import {
31+
createMemo,
32+
createRenderEffect,
33+
createRoot,
34+
createSignal,
35+
createStore,
36+
flush,
37+
latest,
38+
resetErrorHalt
39+
} from "../src/index.js";
40+
41+
beforeEach(() => vi.useFakeTimers());
42+
afterEach(() => {
43+
vi.useRealTimers();
44+
resetErrorHalt();
45+
});
46+
const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
47+
const tick = async (ms: number) => {
48+
await vi.advanceTimersByTimeAsync(ms);
49+
flush();
50+
};
51+
52+
type Form = "memo" | "storeKey" | "storeKeys" | "storeJSON";
53+
const storeForms = ["storeKey", "storeKeys", "storeJSON"] as const;
54+
55+
/** The reader's display of `v`: every form shows the same text. */
56+
async function run(form: Form, at: "before" | "during" | "duringWrite" | "afterLanding") {
57+
const log: string[] = [];
58+
const [s, setS] = createSignal(1);
59+
const [on, setOn] = createSignal(at === "before");
60+
createRoot(() => {
61+
const m0 = createMemo(() => latest(s) + s());
62+
const m1 = createMemo(async () => {
63+
const v = m0();
64+
await sleep(100);
65+
return v;
66+
});
67+
let read: () => unknown = () => m1();
68+
if (form !== "memo") {
69+
const [st] = createStore<{ v?: number }>(d => {
70+
d.v = m1();
71+
}, {});
72+
read =
73+
form === "storeKey"
74+
? () => st.v
75+
: form === "storeKeys"
76+
? () => (Object.keys(st).join() === "v" ? (st as any).v : "keys?")
77+
: () => JSON.parse(JSON.stringify(st)).v;
78+
}
79+
if (at !== "during")
80+
createRenderEffect(
81+
() => `s:${s()}`,
82+
v => void log.push(v)
83+
);
84+
createRenderEffect(
85+
() => (on() ? `r:${read()}` : "r:off"),
86+
v => void log.push(v)
87+
);
88+
});
89+
flush();
90+
await tick(150);
91+
setS(2); // m1 re-asks
92+
flush();
93+
log.push("--reask");
94+
await tick(20);
95+
if (at === "during" || at === "duringWrite") {
96+
setOn(true); // a reader mounts mid-flight; nothing read the store before
97+
flush();
98+
log.push("--mounted");
99+
}
100+
if (at !== "during") {
101+
await tick(10);
102+
setS(3); // a plain write beside the flight
103+
flush();
104+
log.push("--plain write");
105+
}
106+
await tick(300);
107+
if (at === "afterLanding") {
108+
setOn(true);
109+
flush();
110+
log.push("--mounted");
111+
}
112+
await tick(300);
113+
return log;
114+
}
115+
116+
describe("a reader mounted mid-flight reads a derived store like its memo (#4491)", () => {
117+
test("memo form: the mount is held until m1 answers (F-2)", async () => {
118+
expect(await run("memo", "during")).toEqual(["r:off", "--reask", "--mounted", "r:4"]);
119+
});
120+
test("memo form, a plain write beside the flight: held with it, re-asked once", async () => {
121+
expect(await run("memo", "duringWrite")).toEqual([
122+
"s:1",
123+
"r:off",
124+
"s:2",
125+
"--reask",
126+
"--mounted",
127+
"--plain write",
128+
"r:6",
129+
"s:3"
130+
]);
131+
});
132+
for (const form of storeForms)
133+
test(`${form}: held like the memo form, never the old question's r:2 (D-30)`, async () => {
134+
expect(await run(form, "during")).toEqual(await run("memo", "during"));
135+
// The write beside the flight shows no answer of a question between
136+
// the two (`r:5`) either: the memo form's screen, frame for frame.
137+
expect(await run(form, "duringWrite")).toEqual(await run("memo", "duringWrite"));
138+
});
139+
});
140+
141+
describe("readers the hold does not reach (#4491)", () => {
142+
test("memo form, reader on screen at the re-ask; reader mounted after the landing", async () => {
143+
expect(await run("memo", "before")).toEqual([
144+
"s:1",
145+
"r:2",
146+
"--reask",
147+
"--plain write",
148+
"r:4",
149+
"s:3",
150+
"r:6"
151+
]);
152+
expect(await run("memo", "afterLanding")).toEqual([
153+
"s:1",
154+
"r:off",
155+
"s:2",
156+
"--reask",
157+
"s:3",
158+
"--plain write",
159+
"r:6",
160+
"--mounted"
161+
]);
162+
});
163+
for (const form of storeForms)
164+
test(`${form}: as the memo form`, async () => {
165+
expect(await run(form, "before")).toEqual(await run("memo", "before"));
166+
expect(await run(form, "afterLanding")).toEqual(await run("memo", "afterLanding"));
167+
});
168+
});

0 commit comments

Comments
 (0)