Skip to content

Commit 3a73a36

Browse files
ryansolidclaude
andauthored
fix(signals): keep an error boundary's fallback until a held list recovery's frame reveals (#4526) (#4532)
* fix(signals): keep an error boundary's fallback until a held list recovery's frame reveals (#4526) When the error's replacement landed held with an action (a guess on the errored store re-asked it), the list's re-map was a parked pass that disposed the old rows' effects, readers the boundary had caught the error on. The in-heap clear of the list's effect saw those dead readers still in the set and skipped the redraw that joins the hold; the seam then dropped them and revealed outside the hold, showing the pre-error rows. Judge the last reader as the seam does (`prune`), so the reveal joins the hold. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * perf(signals): keep an error boundary's recovery linear in its readers (#4526) The #4526 fix asked `prune` on every reader clear, and `prune` walked every reader still waiting: a recovery of N readers was N²/2 steps (8000 render effects under <Errored>: ~820 ms vs ~40 ms). `catchStatus` now asks with `pass` 0, which ends the walk at the first reader kept by its status — one is left, so this clear is not the last — and leaves the readers behind it to a later prune. The seam, `ready` and the output's pass walk as before. A deterministic pin counts the Map iteration steps of a 1000-reader recovery (linear: ~N; quadratic: ~N²/2). 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 d9aa8f7 commit 3a73a36

3 files changed

Lines changed: 183 additions & 8 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>` boundary over a list no longer reveals the list from before the error when the error's replacement lands held with an action (a guess on the errored store re-asked it): the error stays until that action's frame reveals, instead of showing the previous question's rows.

‎packages/signals/src/boundaries.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,17 @@ function catchStatus(
255255
// effect reporting its own status. An error boundary records an effect
256256
// only while it is errored, and a staged error keeps STATUS_ERROR on a
257257
// node that had it (`stageError`), so `prune`'s CONFIG_STAGED_ERROR case
258-
// cannot reach this path (#4364).
259-
if (collecting.size !== 0)
258+
// cannot reach this path (#4364). "Last" is judged as the seam would
259+
// (`prune`): readers the same held pass disposed — a list re-mapped
260+
// under its parked pass drops the rows that read the error — are gone,
261+
// and left for the seam, they revealed the stale content outside the
262+
// hold (#4526).
263+
if (collecting.size)
260264
for (let b: Boundary | null | undefined = boundaryOf(node); b; b = b._parent)
261265
if (
262266
!((b._readers.get(node)?._statusFlags as number) & (b._type | STATUS_PENDING)) &&
263267
b._readers.delete(node) &&
264-
b._readers.size === 0
268+
!prune(b, 0)
265269
) {
266270
if (b._fallback) redraw(b);
267271
if (revealHooks && b._reveal !== null) revealHooks._changed(b._reveal);
@@ -339,8 +343,13 @@ function release(b: Boundary): void {
339343
* source. A reader pending again after the source that threw answered (it
340344
* re-asks through a memo that carried the error) has no new answer yet: the
341345
* error stays the display (D-39: a pending is not a value; async-gen 91501
342-
* #1831). */
343-
export function prune(b: Boundary, pass: boolean): number {
346+
* #1831).
347+
*
348+
* `pass` 0: a reader clearing asks whether it was the last (`catchStatus`),
349+
* once per clear. The first reader kept by its status ends the walk — one
350+
* is left — and the ones behind it wait for a later prune, so a recovery of
351+
* N readers walks each once, not N²/2 times (#4526). */
352+
export function prune(b: Boundary, pass?: boolean | 0): number {
344353
const mask = b._type | STATUS_PENDING;
345354
for (const [r, s] of b._readers) {
346355
if (!((r._flags | s._flags) & REACTIVE_DISPOSED)) {
@@ -351,8 +360,10 @@ export function prune(b: Boundary, pass: boolean): number {
351360
if (
352361
(r._statusFlags | s._statusFlags) & mask ||
353362
(mask & STATUS_ERROR && s._config & CONFIG_STAGED_ERROR)
354-
)
363+
) {
364+
if (pass === 0) break;
355365
continue;
366+
}
356367
if (
357368
(s !== b._tree || b._armed) &&
358369
(s._config & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD
@@ -388,7 +399,7 @@ export function ready(b: Boundary): boolean {
388399
b._tree._config & CONFIG_HELD &&
389400
(b._output === null || b._output._statusFlags & STATUS_UNINITIALIZED)
390401
) &&
391-
prune(b, false) === 0)
402+
prune(b) === 0)
392403
);
393404
}
394405

@@ -404,7 +415,7 @@ function boundarySeam(): void {
404415
}
405416
// A parked frame's boundary (zombie) keeps its state for its revival.
406417
if (flags & REACTIVE_ZOMBIE) continue;
407-
prune(b, false);
418+
prune(b);
408419
const o = b._output!;
409420
// A fallback staged into the very hold its readers landed in folds back
410421
// (D-16, U-9): the content is ready when that hold commits (D-15). Not
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* #4526: a keyed optimistic list under an error boundary. A new question
3+
* (`filter`) rejects and the boundary shows the error; an action then
4+
* guesses on the store (a removal), which re-asks. The re-ask's answer lands
5+
* held with the action (the guess's hold, D-34): the list's re-map is a
6+
* parked pass, and it disposes the old rows' effects — readers the boundary
7+
* caught the error on. The boundary revealed anyway, at the seam: the
8+
* content it showed was the list from before the error, the old question's
9+
* rows (`[#1][#3][#4]` under "closed").
10+
*
11+
* Ruled (D-41 recovery, D-39, F-1): a shown error stays the display until
12+
* the frame that carries its replacement reveals, through a held recovery.
13+
* The error holds until the action's hold commits, and the reveal shows that
14+
* frame's rows.
15+
*/
16+
import { expect, it } from "vitest";
17+
import {
18+
action,
19+
createErrorBoundary,
20+
createMemo,
21+
createOptimisticStore,
22+
createRenderEffect,
23+
createRoot,
24+
createSignal,
25+
flush,
26+
mapArray,
27+
refresh
28+
} from "../src/index.js";
29+
30+
const tick = async () => {
31+
for (let i = 0; i < 4; i++) {
32+
await new Promise(r => setTimeout(r, 0));
33+
flush();
34+
}
35+
};
36+
37+
it("#4526: an errored list's held recovery keeps the error until its frame reveals (D-41)", async () => {
38+
const asks: { key: string; res: (v: any) => void; rej: (e: any) => void }[] = [];
39+
const ask = (key: string) => new Promise<any>((res, rej) => asks.push({ key, res, rej }));
40+
const settle = async (key: string, f: (a: (typeof asks)[number]) => void) => {
41+
const i = asks.findIndex(a => a.key === key);
42+
f(asks.splice(i, 1)[0]);
43+
await tick();
44+
};
45+
// A DOM stand-in: the content is a list of cells each row's render effect
46+
// writes, attached to the screen when the boundary shows it.
47+
type Cell = { text: string };
48+
let shown: string | { cells: Cell[] } = "";
49+
const screen = () => (typeof shown === "string" ? shown : shown.cells.map(c => c.text).join(""));
50+
let setFilter!: (f: string) => void;
51+
let close!: (id: number) => any;
52+
const dispose = createRoot(d => {
53+
const [filter, sf] = createSignal("open");
54+
setFilter = sf;
55+
const [store, setStore] = createOptimisticStore(
56+
() => ask(`issues:${filter()}`),
57+
[] as { id: number }[],
58+
{ key: "id" }
59+
);
60+
close = action(function* (id: number) {
61+
setStore(list => {
62+
const i = list.findIndex(t => t.id === id);
63+
if (i >= 0) list.splice(i, 1);
64+
});
65+
yield ask(`close:${id}`);
66+
refresh(store);
67+
});
68+
const view = createErrorBoundary(
69+
() => {
70+
const rows = mapArray(
71+
() => store,
72+
(it: { id: number }) => {
73+
const cell: Cell = { text: "" };
74+
createRenderEffect(
75+
() => `[#${it.id}]`,
76+
v => void (cell.text = v)
77+
);
78+
return cell;
79+
}
80+
);
81+
const content = { cells: [] as Cell[] };
82+
createRenderEffect(rows, cells => void (content.cells = cells));
83+
return content;
84+
},
85+
e => `error ${(e() as Error).message}`
86+
);
87+
createRenderEffect(view, v => void (shown = v));
88+
return d;
89+
});
90+
await tick();
91+
await settle("issues:open", a => a.res([{ id: 1 }, { id: 3 }, { id: 4 }]));
92+
expect(screen()).toBe("[#1][#3][#4]");
93+
setFilter("closed");
94+
await tick();
95+
await settle("issues:closed", a => a.rej(new Error("declined")));
96+
expect(screen()).toBe("error declined");
97+
close(1);
98+
await tick();
99+
expect(screen()).toBe("error declined");
100+
// The re-ask's answer lands held with the action: the error stays.
101+
await settle("issues:closed", a => a.res([{ id: 2 }]));
102+
expect(screen()).toBe("error declined");
103+
await settle("close:1", a => a.res({}));
104+
expect(screen()).toBe("error declined");
105+
// The action's refresh lands: its frame reveals.
106+
await settle("issues:closed", a => a.res([{ id: 1 }, { id: 2 }]));
107+
expect(screen()).toBe("[#1][#2]");
108+
dispose();
109+
});
110+
111+
it("#4526: a recovery's reader clears walk the boundary's readers once, not once per clear", () => {
112+
// N render effects read one memo under the boundary; the memo errors and
113+
// recovers. Each effect's clear asks whether it was the last reader: that
114+
// must not re-walk the remaining readers (`prune`) every time — N²/2 steps.
115+
// Counted as Map iteration steps during the recovery flush (deterministic).
116+
const N = 1000;
117+
const [bad, setBad] = createSignal(false);
118+
let shown: unknown;
119+
const dispose = createRoot(d => {
120+
const m = createMemo(() => {
121+
if (bad()) throw new Error("x");
122+
return 1;
123+
});
124+
const view = createErrorBoundary(
125+
() => {
126+
for (let i = 0; i < N; i++) createRenderEffect(m, () => {});
127+
return "ok";
128+
},
129+
() => "err"
130+
);
131+
createRenderEffect(view, v => void (shown = v));
132+
return d;
133+
});
134+
flush();
135+
setBad(true);
136+
flush();
137+
expect(shown).toBe("err");
138+
const proto = Map.prototype as any;
139+
const iterate = proto[Symbol.iterator];
140+
let steps = 0;
141+
proto[Symbol.iterator] = function (this: Map<unknown, unknown>) {
142+
const it = iterate.call(this);
143+
return {
144+
next: () => (steps++, it.next()),
145+
[Symbol.iterator]() {
146+
return this;
147+
}
148+
};
149+
};
150+
try {
151+
setBad(false);
152+
flush();
153+
} finally {
154+
proto[Symbol.iterator] = iterate;
155+
}
156+
expect(shown).toBe("ok");
157+
expect(steps).toBeLessThan(4 * N);
158+
dispose();
159+
});

0 commit comments

Comments
 (0)