Skip to content

Commit dd5f9d1

Browse files
ryansolidclaude
andauthored
fix(signals): an Errored heals when nothing reads the failed source any more (#4644) (#4653)
* fix(signals): an Errored heals when nothing reads the failed source any more (#4644) An error boundary waits on the source that threw. When the reader it caught was its own tree (the error reached it only through memos, e.g. a Show/Match condition), the tree re-ran clean after navigating away but prune kept it because the source itself was still errored. A source read by nothing now settles: no live reader holds the error (D-39, F-3, F-12). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE * test(web): import JSX from @solidjs/web in the #4644 spec Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE * fix(signals): only readers the Errored caught keep its fallback up prune kept an error boundary's reader while the source that threw was errored and subscribed by anything at all. A memo hoisted outside the boundary, a derived store or projection, or a reader under another Errored kept the source subscribed, so the fallback stayed up after every reader under the boundary was gone. D-39: the boundary waits on what threw only while a reader that caught the error is alive. The source now keeps the reader only while that reader still reads it, directly or through derivations (#4644). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE * fix(signals): walk each upstream node once in the Errored heal check (#4644) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE * refactor(signals): shorter heal-check walk (#4644) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE --------- Co-authored-by: Claude <[email protected]>
1 parent 5ae39fc commit dd5f9d1

4 files changed

Lines changed: 379 additions & 2 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+
An `<Errored>` heals once no reader it caught still reads the failed source, instead of staying on its fallback after navigating to another branch (#4644). This covers an error read only by memos (a `<Show>`/`<Match>` condition), and a source something outside the boundary still reads: a hoisted memo, a derived store or projection, a reader under another `<Errored>`.

‎packages/signals/src/boundaries.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,23 @@ function caughtBelow(b: Boundary, r: Computed<any>, s: Computed<any>): boolean {
369369
return false;
370370
}
371371

372+
/** `n` reads `s`, directly or through derivations wherever they live (a
373+
* hoisted memo, a derived store's derive, a loading boundary's output). Each
374+
* upstream node is walked once (`seen`), so a shared upstream graph costs its
375+
* size, not its paths. */
376+
function reads(n: Computed<any>, s: Computed<any>, seen?: Set<any>): any {
377+
for (let l = n._deps, h; l; l = l._nextDep)
378+
if ((h = l._dep) === s || ((seen ||= new Set()).size < seen.add(h).size && reads(h, s, seen)))
379+
return 1;
380+
}
381+
372382
/** Drop the readers that settled or died. Settled: a loading reader no
373-
* longer pending; an error's source neither errored nor in flight. A reader
383+
* longer pending; an error's source neither errored nor in flight, or no
384+
* longer read by the reader that caught it: the boundary waits on what threw
385+
* only while such a reader is alive. One that re-ran clean (the tree, a
386+
* memo-only read gated away) holds nothing, and a reader outside the
387+
* boundary — a hoisted memo, a derived store, a reader under another
388+
* boundary — never caught its error (D-39, #4644). A reader
374389
* that landed but is held (its value staged in a transaction) is not: the
375390
* content reveals at the commit, with its run. The tree is: the output reads
376391
* it and enters the transaction itself (its committed value shows until the
@@ -400,7 +415,7 @@ export function prune(b: Boundary, pass?: boolean | 0): number {
400415
// settled before it ran, a fresh boundary revealed into a hold its
401416
// pass then went pending in, which waited on that flight (#4568).
402417
if (
403-
((r._statusFlags | s._statusFlags) & mask &&
418+
((r._statusFlags & mask || (s._statusFlags & mask && reads(r, s))) &&
404419
!(mask & STATUS_ERROR && caughtBelow(b, r, s))) ||
405420
(mask & STATUS_ERROR && s._config & CONFIG_STAGED_ERROR) ||
406421
(r._config & CONFIG_VERDICT && r._flags & REACTIVE_IN_HEAP)
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import { expect, it } from "vitest";
2+
import {
3+
createErrorBoundary,
4+
createMemo,
5+
createProjection,
6+
createRenderEffect,
7+
createRoot,
8+
createSignal,
9+
createStore,
10+
flush
11+
} from "../src/index.js";
12+
13+
// #4644 (D-39, F-3, F-12): a fallback no live reader holds heals. The shape:
14+
// `<Errored><Show when={page() === "list"} fallback="about">{X}</Show>
15+
// </Errored>`, `rows` an async memo owned outside the boundary that rejects.
16+
// X = "memo-only": `rows` is read only by memos (a `<Show when>` condition)
17+
// whose value the boundary's tree flattens, so the reader the boundary
18+
// caught is its own tree. X = "effect": a render effect in the branch reads
19+
// it. Navigating away disposes every reader of `rows`: nothing on the new
20+
// branch reads the error, so the boundary shows it, as a fresh render with
21+
// the same inputs would. With another live reader of `rows` under the same
22+
// boundary ("kept"), the error stays up.
23+
const tick = async () => {
24+
for (let i = 0; i < 3; i++) {
25+
await new Promise(r => setTimeout(r, 0));
26+
flush();
27+
}
28+
};
29+
30+
for (const shape of ["memo-only", "effect", "kept"] as const)
31+
it(`${shape}: navigating away from a failed memo (#4644)`, async () => {
32+
let reject!: (e: Error) => void;
33+
const [page, setPage] = createSignal("list");
34+
const frames: unknown[] = [];
35+
const dispose = createRoot(d => {
36+
const rows = createMemo(
37+
() =>
38+
new Promise<number[]>((_, r) => {
39+
reject = r;
40+
})
41+
);
42+
const view = createErrorBoundary(
43+
() => {
44+
if (shape === "kept")
45+
createRenderEffect(
46+
() => rows().length,
47+
() => {}
48+
);
49+
const onList = createMemo(() => page() === "list");
50+
return createMemo(() => {
51+
if (!onList()) return "about";
52+
if (shape === "effect") {
53+
createRenderEffect(
54+
() => rows().length,
55+
() => {}
56+
);
57+
return "list";
58+
}
59+
const cond = createMemo(() => rows().length === 0);
60+
return createMemo(() => (cond() ? "none" : "some"));
61+
});
62+
},
63+
() => "ERR"
64+
);
65+
createRenderEffect(
66+
() => view(),
67+
v => {
68+
frames.push(v);
69+
}
70+
);
71+
return d;
72+
});
73+
flush();
74+
await tick();
75+
reject(new Error("boom"));
76+
await tick();
77+
expect(frames[frames.length - 1]).toBe("ERR");
78+
setPage("about");
79+
flush();
80+
await tick();
81+
dispose();
82+
expect(frames[frames.length - 1]).toBe(shape === "kept" ? "ERR" : "about");
83+
});
84+
85+
// The boundary waits on what threw only while a reader THAT CAUGHT ITS ERROR
86+
// is alive (D-39). Something else still reading `rows` is not one: a memo
87+
// hoisted outside the boundary, a derived store or projection built from it,
88+
// a reader under another error boundary. Each still keeps `rows` subscribed,
89+
// so "is `rows` read at all" kept this boundary stuck. While the list is
90+
// mounted the error stays (across a prune: another reader's settling); away,
91+
// the boundary heals; back, the error shows again (`sync`: a sync throw
92+
// re-thrown by the new reader — an async source is re-asked on the new read
93+
// instead, which is not this rule).
94+
const flat = (v: unknown) => {
95+
while (typeof v === "function") v = v();
96+
return v;
97+
};
98+
for (const via of [
99+
"hoisted memo",
100+
"derived store",
101+
"projection",
102+
"other boundary",
103+
"sync"
104+
] as const)
105+
it(`${via}: a reader outside the boundary does not keep its error up (#4644)`, async () => {
106+
let reject!: (e: Error) => void;
107+
let resolveP!: (v: number) => void;
108+
const [page, setPage] = createSignal("list");
109+
const [fail, setFail] = createSignal(false);
110+
const frames: unknown[] = [];
111+
const other: unknown[] = [];
112+
const dispose = createRoot(d => {
113+
const rows = createMemo(() => {
114+
if (via !== "sync") return new Promise<number[]>((_, r) => (reject = r));
115+
if (fail()) throw new Error("sync boom");
116+
return [] as number[];
117+
});
118+
let read: () => number[] = rows;
119+
if (via === "hoisted memo" || via === "sync") read = createMemo(() => [...rows()]);
120+
if (via === "derived store") {
121+
const [st] = createStore<{ list: number[] }>(
122+
dr => {
123+
dr.list = rows();
124+
},
125+
{ list: [] }
126+
);
127+
read = () => st.list;
128+
}
129+
if (via === "projection") {
130+
const st = createProjection<{ list: number[] }>(
131+
dr => {
132+
dr.list = rows();
133+
},
134+
{ list: [] }
135+
);
136+
read = () => st.list;
137+
}
138+
if (via === "other boundary") {
139+
const header = createErrorBoundary(
140+
() => createMemo(() => rows().length),
141+
() => "ERR2"
142+
);
143+
createRenderEffect(
144+
() => flat(header()),
145+
v => void other.push(v)
146+
);
147+
}
148+
const p = createMemo(() => new Promise<number>(r => (resolveP = r)));
149+
const view = createErrorBoundary(
150+
() => {
151+
createRenderEffect(
152+
() => p(),
153+
() => {}
154+
);
155+
const onList = createMemo(() => page() === "list");
156+
return createMemo(() => {
157+
if (!onList()) return "about";
158+
const cond = createMemo(() => read().length === 0);
159+
return createMemo(() => (cond() ? "none" : "some"));
160+
});
161+
},
162+
() => "ERR"
163+
);
164+
createRenderEffect(
165+
() => flat(view()),
166+
v => void frames.push(v)
167+
);
168+
return d;
169+
});
170+
const last = () => frames[frames.length - 1];
171+
flush();
172+
await tick();
173+
if (via === "sync") setFail(true);
174+
else reject(new Error("boom"));
175+
await tick();
176+
const failed = last();
177+
resolveP(1);
178+
await tick();
179+
const afterPrune = last();
180+
setPage("about");
181+
await tick();
182+
const away = last();
183+
setPage("list");
184+
await tick();
185+
const back = last();
186+
dispose();
187+
expect({ failed, afterPrune, away }).toEqual({
188+
failed: "ERR",
189+
afterPrune: "ERR",
190+
away: "about"
191+
});
192+
if (via === "sync") expect(back).toBe("ERR");
193+
if (via === "other boundary") expect(other[other.length - 1]).toBe("ERR2");
194+
});
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
// #4644 (D-39): an <Errored> whose fallback no live reader holds heals. A
6+
// list page reads a failed async memo and the boundary shows its fallback;
7+
// navigating to another branch under the same boundary disposes every reader
8+
// of the memo, so the new branch shows. A reader that is only a memo — the
9+
// condition of a <Show> or <Match> — used to keep the boundary stuck.
10+
import { expect, test } from "vitest";
11+
import { render, type JSX } from "@solidjs/web";
12+
import {
13+
createMemo,
14+
createSignal,
15+
createStore,
16+
Errored,
17+
flush,
18+
For,
19+
Loading,
20+
Match,
21+
Show,
22+
Switch
23+
} from "solid-js";
24+
25+
const tick = async () => {
26+
for (let i = 0; i < 2; i++) {
27+
await new Promise(r => setTimeout(r, 0));
28+
flush();
29+
}
30+
};
31+
type Row = { id: number };
32+
33+
const readers: Record<string, (rows: () => Row[]) => JSX.Element> = {
34+
"Show when={rows().length === 0}": rows => (
35+
<Show when={rows().length === 0} fallback={<p>{rows().length} rows</p>}>
36+
<p>no rows</p>
37+
</Show>
38+
),
39+
"Show when={rows()}": rows => <Show when={rows()}>{r => <p>{r().length} rows</p>}</Show>,
40+
"Show keyed": rows => (
41+
<Show when={rows()} keyed>
42+
{r => <p>{r.length} rows</p>}
43+
</Show>
44+
),
45+
"Switch/Match": rows => (
46+
<Switch fallback={<p>{rows().length} rows</p>}>
47+
<Match when={rows().length === 0}>
48+
<p>no rows</p>
49+
</Match>
50+
</Switch>
51+
),
52+
"memo of the condition": rows => {
53+
const empty = createMemo(() => rows().length === 0);
54+
return <p>{empty() ? "none" : "some"}</p>;
55+
},
56+
text: rows => <p>{rows().length} rows</p>,
57+
For: rows => (
58+
<ul>
59+
<For each={rows()}>{r => <li>{r.id}</li>}</For>
60+
</ul>
61+
)
62+
};
63+
64+
for (const withLoading of [true, false])
65+
for (const [name, view] of Object.entries(readers))
66+
test(`${withLoading ? "Loading" : "no Loading"} | ${name}: navigating away from a failed list heals <Errored> (#4644)`, async () => {
67+
let rejectList!: (e: Error) => void;
68+
let setPage!: (p: string) => void;
69+
const el = document.createElement("div");
70+
const dispose = render(() => {
71+
const [page, set] = createSignal("list");
72+
setPage = set;
73+
const rows = createMemo(
74+
() =>
75+
new Promise<Row[]>((_, reject) => {
76+
rejectList = reject;
77+
})
78+
);
79+
return (
80+
<Errored fallback={<p>could not load</p>}>
81+
<Show when={page() === "list"} fallback={<p>about page</p>}>
82+
{withLoading ? <Loading fallback={<p>loading</p>}>{view(rows)}</Loading> : view(rows)}
83+
</Show>
84+
</Errored>
85+
);
86+
}, el);
87+
await tick();
88+
rejectList(new Error("boom"));
89+
await tick();
90+
const failed = el.textContent;
91+
setPage("about");
92+
flush();
93+
await tick();
94+
const after = el.textContent;
95+
dispose();
96+
expect({ failed, after }).toEqual({ failed: "could not load", after: "about page" });
97+
});
98+
99+
// Only a reader the boundary caught keeps it waiting (D-39): something
100+
// outside it still reading the failed memo — a memo or a derived store
101+
// hoisted above the <Errored>, a header count under another <Errored> — did
102+
// not catch this boundary's error and does not keep its fallback up.
103+
for (const shape of [
104+
"derived memo outside",
105+
"derived store outside",
106+
"header reader outside"
107+
] as const)
108+
test(`${shape}: navigating away from a failed list heals <Errored> (#4644)`, async () => {
109+
let rejectList!: (e: Error) => void;
110+
let setPage!: (p: string) => void;
111+
const el = document.createElement("div");
112+
const dispose = render(() => {
113+
const [page, set] = createSignal("list");
114+
setPage = set;
115+
const rows = createMemo(
116+
() =>
117+
new Promise<number[]>((_, reject) => {
118+
rejectList = reject;
119+
})
120+
);
121+
let read: () => number[] = rows;
122+
if (shape === "derived memo outside") read = createMemo(() => [...rows()].sort());
123+
if (shape === "derived store outside") {
124+
const [st] = createStore<{ l: number[] }>(
125+
d => {
126+
d.l = rows();
127+
},
128+
{ l: [] }
129+
);
130+
read = () => st.l;
131+
}
132+
return (
133+
<>
134+
{shape === "header reader outside" && (
135+
<Errored fallback={<i>hdr-err</i>}>
136+
<i>{rows().length}</i>
137+
</Errored>
138+
)}
139+
<Errored fallback={<p>could not load</p>}>
140+
<Show when={page() === "list"} fallback={<p>about page</p>}>
141+
<Show when={read().length === 0} fallback={<p>some</p>}>
142+
<p>no rows</p>
143+
</Show>
144+
</Show>
145+
</Errored>
146+
</>
147+
);
148+
}, el);
149+
await tick();
150+
rejectList(new Error("boom"));
151+
await tick();
152+
const failed = el.textContent;
153+
setPage("about");
154+
flush();
155+
await tick();
156+
const after = el.textContent;
157+
dispose();
158+
const hdr = shape === "header reader outside" ? "hdr-err" : "";
159+
expect({ failed, after }).toEqual({
160+
failed: hdr + "could not load",
161+
after: hdr + "about page"
162+
});
163+
});

0 commit comments

Comments
 (0)