Skip to content

Commit 71a6a75

Browse files
ryansolidclaude
andauthored
fix(signals): a memo over a re-asked async memo is born held with the mount that first reads it (#4504) (#4509)
* fix(signals): a memo over a re-asked async memo is born held with the mount that first reads it (#4504; D-2, D-22, D-25, D-27) An async memo `m1` reads `m0 = createMemo(() => latest(s) + s())`; a write re-asks `m1` while nothing reads it, and mid-flight a render effect mounts reading `mb = createMemo(() => m1())` (or `m1() * 10`). The screen was `--mounted, r:2, r:4`: the held reader published the old answer at the landing and the re-ask's only at the commit. Reading `m1` directly, or with a plain input, gives `--mounted, r:4`. Cause (core.ts `read`, the #4050 posture inheritance): `mb`'s pass in the mount was not a verdict reader (it was pending, so `recompute`'s head cleared CONFIG_VERDICT, the #3884 clause of D-25) and its plain read of the in-flight `m1` threw, holding the mount (D-2, F-2). But the read stamped `m1`'s CONFIG_VERDICT onto the pass before throwing, so `mb`'s next run — the hold's re-run at the landing — was a verdict reader and was served `m1`'s committed value (`stagedScreen`), and the effect published it. Fix: a read of a pending node hands on no posture to a pass that is not a verdict reader already. Such a read throws: it read no value, and a node whose flight is up is the hold's work, not a verdict reader (D-25, #3884); a memo's posture is what it reads. A verdict reader served the screen of a flight (`observeFlight`) keeps the posture, so a memo already on screen before the re-ask shows exactly what it showed before. The plain mount is not display-ahead (D-27): its reader is born held and shows the re-ask's answer (D-22's identity-memo clause, F-9); `latest`/`isPending` of `mb` read `20`/`true` during the flight (D-22, D-23 "a derivation of it ... at any depth"). The line sits behind `observeFlightHook`, which folds away in bundles without the verdict engine (core floor, hello world: 0 B); +15 B minified where `isPending`/`latest` are imported. Pinned in tests/midflight-memo-over-reask-4504.test.ts: identity, `* 10` and a two-memo chain; verdict and plain inputs; the memo made before the re-ask or mid-flight; a prior user effect (same screen as no reader); a memo already on screen before the re-ask (unchanged); latest/isPending of the memo after a plain mount and from a verdict leaf. #4474's pins (tests/latest-unread-identity-memo-4474.test.ts) are untouched: same six expected failures, same received values. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * test(signals): #4491's later-flight pin compares the held and from-start screens (#4504; D-25, D-30) After an equal landing, a later flight wakes the held derive pending, and that pass reads the pending m1 plainly: under #4504 a read of a pending node hands no posture to a pass that is the hold's work (D-25), as an identity memo between m1 and the reader posts none. The screens of the held, from-start and memo forms are unchanged; the landing's posture is still asserted. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent bb51da6 commit 71a6a75

4 files changed

Lines changed: 259 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 memo between a render effect and an async memo in flight is born held with the mount that first reads it (#4504, D-2, D-22, D-27). With an input that reads `latest()`/`isPending()`, the memo carried that verdict posture by what it read (D-25), and its pass in a plain mount mid-flight stamped the posture from the read of the pending async memo even though the read threw: at the landing the memo re-ran as a verdict reader and was served the async memo's committed value, so the held reader showed the old answer at the landing and the new one only at the commit (`r:2`, then `r:4`). A read of a pending node now hands on no posture to a pass that is not a verdict reader already: the reader waits for the re-ask's answer and shows it once (`r:4`), as when it reads the async memo directly. A memo that was already a verdict reader on screen keeps its posture.

‎packages/signals/src/core/core.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2102,9 +2102,18 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
21022102
// it probed inline: it reads held values as the screen and shows the
21032103
// verdict now. Not an effect: a render effect is a frame reader
21042104
// already, and a user effect's posture is its own reads'.
2105+
// A read of a pending node hands on no posture to a pass that is
2106+
// not a verdict reader already (#4504): such a read throws — it read
2107+
// no value, and a node whose flight is up is the hold's work, not a
2108+
// verdict reader (D-25, #3884). Stamped, the pass's next run — the
2109+
// hold's re-run at the landing of a memo whose plain mount read
2110+
// held its reader (D-2, D-27) — was served the screen beside the
2111+
// answer its reader waited for (F-2, D-22). A verdict reader keeps
2112+
// the posture it is served the screen with (`observeFlight`).
21052113
// (A verdict memo exists only with the verdict engine installed.)
21062114
if (observeFlightHook && !(c as any)._type)
2107-
(c as Computed<any>)._flags |= owner._config & CONFIG_VERDICT;
2115+
(c as Computed<any>)._flags |=
2116+
owner._config & CONFIG_VERDICT & (owner._statusFlags & STATUS_PENDING ? c._config : -1);
21082117
}
21092118
}
21102119
if (__TEST__)
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
/**
2+
* #4504: a memo between a render effect and an async memo in flight, read for
3+
* the first time by a mount made mid-flight.
4+
*
5+
* `m0` is the async memo's input; `m1 = createMemo(async () => m0())` takes
6+
* 100 ms. A plain write re-asks `m1` while nothing reads it, and 20 ms into
7+
* the flight a render effect mounts and reads `mb`, a memo over `m1`
8+
* (`() => m1()`, `() => m1() * 10`) that nothing has read before.
9+
*
10+
* With `m0 = createMemo(() => latest(s) + s())` (a verdict reader: it called
11+
* `latest`) `m1` and `mb` carried that posture by what they read (D-25: a
12+
* memo's posture is what it reads). The mount's pass of `mb` read `m1`
13+
* plainly — `mb` was pending, so the pass is not a verdict reader (D-25,
14+
* #3884) — and threw, so the reader held the mount (D-2, F-2). But the read
15+
* of the pending `m1` still stamped the posture on `mb` for its next pass,
16+
* so at the landing `mb` re-ran as a verdict reader and was served `m1`'s
17+
* committed value (the screen): `r:2` published at the landing, then `r:4`
18+
* at the commit — a held reader shown a value older than the one it waited
19+
* for (F-2, F-1).
20+
*
21+
* Ruled: a plain read in a mount is not display-ahead (D-27); the reader of
22+
* a derivation with no value to show for the re-ask is born held (D-2) and
23+
* shows the re-ask's answer (D-22, F-9) — the screen of the direct read
24+
* (`r:4`) and of a plain input. A pending read yields no value, so it hands
25+
* on no posture (D-25: a node whose flight is up is the hold's work, not a
26+
* verdict reader). A memo that is a verdict reader already keeps its
27+
* posture when it is served the screen of a flight: a prior on-screen
28+
* reader of `mb` sees what it saw before this fix.
29+
*/
30+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
31+
import {
32+
createEffect,
33+
createMemo,
34+
createRenderEffect,
35+
createRoot,
36+
createSignal,
37+
flush,
38+
isPending,
39+
latest,
40+
resetErrorHalt
41+
} from "../src/index.js";
42+
43+
beforeEach(() => vi.useFakeTimers());
44+
afterEach(() => {
45+
vi.useRealTimers();
46+
resetErrorHalt();
47+
});
48+
49+
const sleep = (ms: number) => new Promise<void>(r => setTimeout(r, ms));
50+
const tick = async (ms: number) => {
51+
await vi.advanceTimersByTimeAsync(ms);
52+
flush();
53+
};
54+
55+
type Input = "verdict" | "plain";
56+
type Between = "identity" | "times10" | "chain";
57+
type Made = "before" | "mid-flight";
58+
type Prior = "none" | "render effect" | "user effect";
59+
60+
const between: Record<Between, (m1: () => number) => () => number> = {
61+
identity: m1 => createMemo(() => m1()),
62+
times10: m1 => createMemo(() => m1() * 10),
63+
chain: m1 => {
64+
const inner = createMemo(() => m1() * 10);
65+
return createMemo(() => inner() + 1);
66+
}
67+
};
68+
69+
async function run(input: Input, shape: Between, made: Made, prior: Prior) {
70+
const log: string[] = [];
71+
const [s, setS] = createSignal(1);
72+
const [on, setOn] = createSignal(prior === "render effect");
73+
let m1!: () => number;
74+
let mb!: () => number;
75+
createRoot(() => {
76+
const m0 = createMemo(() => (input === "plain" ? s() * 2 : latest(s) + s()));
77+
m1 = createMemo(async () => {
78+
const v = m0();
79+
await sleep(100);
80+
return v;
81+
});
82+
if (made === "before") mb = between[shape](m1);
83+
});
84+
if (prior === "user effect")
85+
createRoot(() =>
86+
createEffect(
87+
() => mb(),
88+
() => {}
89+
)
90+
);
91+
createRoot(() =>
92+
createRenderEffect(
93+
() => (on() ? `r:${mb()}` : "r:off"),
94+
v => void log.push(v)
95+
)
96+
);
97+
flush();
98+
await tick(150);
99+
setS(2);
100+
flush();
101+
await tick(20);
102+
if (made === "mid-flight") createRoot(() => (mb = between[shape](m1)));
103+
setOn(true);
104+
flush();
105+
log.push("--mounted");
106+
await tick(300);
107+
return log;
108+
}
109+
110+
const screen = (shape: Between, v: number) =>
111+
`r:${shape === "identity" ? v : shape === "times10" ? v * 10 : v * 10 + 1}`;
112+
113+
describe("#4504: a memo over a re-asked async memo, first read by a mount mid-flight", () => {
114+
it("control: the mount reading the async memo directly is held and shows the re-ask's answer", async () => {
115+
const log: string[] = [];
116+
const [s, setS] = createSignal(1);
117+
const [on, setOn] = createSignal(false);
118+
createRoot(() => {
119+
const m0 = createMemo(() => latest(s) + s());
120+
const m1 = createMemo(async () => {
121+
const v = m0();
122+
await sleep(100);
123+
return v;
124+
});
125+
createRenderEffect(
126+
() => (on() ? `r:${m1()}` : "r:off"),
127+
v => void log.push(v)
128+
);
129+
});
130+
flush();
131+
await tick(150);
132+
setS(2);
133+
flush();
134+
await tick(20);
135+
setOn(true);
136+
flush();
137+
log.push("--mounted");
138+
await tick(300);
139+
expect(log).toEqual(["r:off", "--mounted", "r:4"]);
140+
});
141+
142+
for (const input of ["verdict", "plain"] as const)
143+
for (const shape of ["identity", "times10", "chain"] as const)
144+
for (const made of ["before", "mid-flight"] as const)
145+
it(`${input} input, ${shape} memo made ${made === "before" ? "before the re-ask" : "mid-flight"}: the reader is born held and shows the re-ask's answer`, async () => {
146+
expect(await run(input, shape, made, "none")).toEqual([
147+
"r:off",
148+
"--mounted",
149+
screen(shape, 4)
150+
]);
151+
});
152+
153+
// #4474's equivalence: a reader that holds nothing does not change the
154+
// screen (laziness is never observable, D-22).
155+
for (const input of ["verdict", "plain"] as const)
156+
it(`${input} input, a prior user effect reading the memo: the same screen as with no reader`, async () => {
157+
expect(await run(input, "times10", "before", "user effect")).toEqual([
158+
"r:off",
159+
"--mounted",
160+
"r:40"
161+
]);
162+
});
163+
164+
// Unchanged by #4504: the memo already had a value and an on-screen reader
165+
// when the write re-asked the async memo. With the verdict input that
166+
// reader shows the screen of the flight (D-25: the memo is a verdict
167+
// reader by what it reads), the verdict lane's reveal of `latest(s) + s()`
168+
// over the held write (`3`, D-26), then the commit.
169+
it("verdict input, the memo already on screen before the re-ask: unchanged", async () => {
170+
expect(await run("verdict", "times10", "before", "render effect")).toEqual([
171+
"r:20",
172+
"--mounted",
173+
"r:30",
174+
"r:40"
175+
]);
176+
expect(await run("verdict", "identity", "before", "render effect")).toEqual([
177+
"r:2",
178+
"--mounted",
179+
"r:3",
180+
"r:4"
181+
]);
182+
});
183+
it("plain input, the memo already on screen before the re-ask: unchanged", async () => {
184+
expect(await run("plain", "times10", "before", "render effect")).toEqual([
185+
"r:20",
186+
"--mounted",
187+
"r:40"
188+
]);
189+
});
190+
191+
// D-22 / D-23: the memo has produced a value (the async memo's landed
192+
// answer, through it) and is pending on the re-ask, at any depth — read
193+
// unowned after the plain mount, and by a verdict leaf mounted mid-flight.
194+
for (const input of ["verdict", "plain"] as const)
195+
for (const mount of ["plain reader", "verdict leaf"] as const)
196+
it(`${input} input, ${mount} mounted mid-flight: latest/isPending of the memo`, async () => {
197+
const log: string[] = [];
198+
const [s, setS] = createSignal(1);
199+
const [on, setOn] = createSignal(false);
200+
let mb!: () => number;
201+
createRoot(() => {
202+
const m0 = createMemo(() => (input === "plain" ? s() * 2 : latest(s) + s()));
203+
const m1 = createMemo(async () => {
204+
const v = m0();
205+
await sleep(100);
206+
return v;
207+
});
208+
mb = createMemo(() => m1() * 10);
209+
createRenderEffect(
210+
() =>
211+
!on()
212+
? "off"
213+
: mount === "plain reader"
214+
? `r:${mb()}`
215+
: `l:${latest(mb)} p:${isPending(mb)}`,
216+
v => void log.push(v)
217+
);
218+
});
219+
flush();
220+
await tick(150);
221+
setS(2);
222+
flush();
223+
await tick(20);
224+
setOn(true);
225+
flush();
226+
log.push("--mounted");
227+
expect([latest(mb), isPending(mb)]).toEqual([20, true]);
228+
await tick(300);
229+
expect([latest(mb), isPending(mb)]).toEqual([40, false]);
230+
expect(log).toEqual(
231+
mount === "plain reader"
232+
? ["off", "--mounted", "r:40"]
233+
: ["off", "l:20 p:true", "--mounted", "l:40 p:false"]
234+
);
235+
});
236+
});

‎packages/signals/tests/unread-derive-midflight-mount-4491.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,13 @@ describe("readers the hold does not reach (#4491)", () => {
173173
* writes nothing on the derive: it marks the derive's next pass (`fam.w`),
174174
* a read that finds no flight up clears the mark, and the posture stays the
175175
* pass's own reads (core `recompute`). A later flight of `m1`, the reader on
176-
* screen, then runs as it does for a store read from the start, and as the
177-
* memo form: the same screen, the same posture — whether the landing changed
178-
* the leaf (the derive re-ran) or equalled it (it did not).
176+
* screen, then shows what a store read from the start and the memo form
177+
* show — whether the landing changed the leaf (the derive re-ran) or equalled
178+
* it (it did not). After an equal landing the flight wakes the held derive
179+
* pending, and that pass reads the pending `m1` plainly: a read of a pending
180+
* node hands no posture to a pass that is the hold's work (#4504; D-25), as
181+
* a memo between `m1` and the reader posts none. The posture during the
182+
* flight is internal; the screen is the rule (D-30).
179183
*/
180184
describe("the derive's posture after the held mount (#4491)", () => {
181185
async function later(form: "memo" | "storeKey", mountMidFlight: boolean, equal: boolean) {
@@ -230,7 +234,7 @@ describe("the derive's posture after the held mount (#4491)", () => {
230234
for (const equal of [false, true])
231235
test(`a later flight after ${equal ? "an equal" : "a changed"} landing`, async () => {
232236
const held = await later("storeKey", true, equal);
233-
expect(held).toEqual(await later("storeKey", false, equal));
237+
expect(screen(held)).toEqual(screen(await later("storeKey", false, equal)));
234238
expect(screen(held)).toEqual(screen(await later("memo", true, equal)));
235239
expect(held).toContain("--landed verdict=true pending=false");
236240
});

0 commit comments

Comments
 (0)