Skip to content

Commit 49a765c

Browse files
ryansolidclaude
andauthored
fix(signals): a boundary's fallback is instantiated once per show (#3992)
* fix(signals): a boundary's fallback is instantiated once per show (#3991) The fallback was built inside the boundary's output pass, so every re-derive of the output while it showed rebuilt it: a sibling verdict reader re-running on the error re-ran the tree, which threw the same error again and re-derived the output (two fallbacks for one failure); a reset() whose retry was pending re-derived it through prune (a third); the Retry button was replaced and focus lost. Build the fallback in a computed of its own beside the output, with the output's id, and keep it while the fallback shows; dispose it when content shows. A later error reaches it through the error accessor, as intended all along (the once-per-show pin in createErrorBoundary.test.ts held for a render effect erroring directly, not for a memo between). Size-Exception: CSR with Show/For/Loading/Errored 12.976 -> 12.99 KB, observe-tier CSR 14.56 -> 14.59 KB, compiled CSR 25.465 -> 25.49 KB (+146 B minified each); core, isPending/latest and render unchanged. Accepted by the maintainer 2026-10-12. Fixes #3991 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01LVm3wseHt9wRTY8LfPiHmQ * fix(signals): dispose the fallback once the content that replaces it is on screen A held swap to content keeps the fallback on screen, live, until the commit (S-14): the disposal is a queued run — this flush's effect phase when the pass is the frame's, the landing when the frame parks — and a pass back to the fallback before then finds it still built and keeps it. Pinned on the S-14 shape; fails with an immediate dispose. Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01LVm3wseHt9wRTY8LfPiHmQ --------- Co-authored-by: Claude <[email protected]>
1 parent ade4c46 commit 49a765c

6 files changed

Lines changed: 233 additions & 11 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 boundary's fallback is instantiated once per show. An `<Errored>` whose content reads a failing async memo through a `<Show>` rendered its fallback twice for one failure and rebuilt it when a `reset()` retry failed again, losing focus on the Retry button (#3991). The fallback is now built in a scope of its own beside the boundary's output and kept while it shows; a later error reaches it through the error accessor. The same holds for a `<Loading>` fallback across the boundary's re-derives.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,9 @@ The optimistic world is the screen plus the action's guesses: derivations of a g
328328

329329
**From:** F-12, F-3, F-1. **Scenarios:** S-38.
330330

331-
An `<Errored>` flipping to its fallback is a boundary going back to its fallback: the nodes behind it stop holding (unless read elsewhere, where they error too), and non-errored nodes that lose their only visible consumer that way stop holding as well. The error stays on screen until a new answer replaces the errored one — a pending is not a value: over `Errored > Loading(never shown)`, a write publishes beside the old error (`0 | Fetch error for 0` → `1 | Fetch error for 0` → `1 | Fetch error for 1`); the inner fallback is hidden behind the error. `Errored` builds its function fallback in its own scope, like `Show`.
331+
An `<Errored>` flipping to its fallback is a boundary going back to its fallback: the nodes behind it stop holding (unless read elsewhere, where they error too), and non-errored nodes that lose their only visible consumer that way stop holding as well. The error stays on screen until a new answer replaces the errored one — a pending is not a value: over `Errored > Loading(never shown)`, a write publishes beside the old error (`0 | Fetch error for 0` → `1 | Fetch error for 0` → `1 | Fetch error for 1`); the inner fallback is hidden behind the error. `Errored` builds its function fallback in its own scope, like `Show`, once per show: a re-derive of the boundary while the fallback shows — a second reader caught, the same error propagated again, a retry pending after `reset()`, a later error — keeps the built fallback, its DOM and focus with it; a later error reaches it through the error accessor; the fallback is disposed once the content that replaces it is on screen — at the commit, when the swap is held (#3991).
332332

333-
**Collapses:** §23.1 items 2–3, §27 7b (2026-10-02; the top-level `isPending(count)` affordance was lost by this ruling, knowingly), recovery 9.1–9.3. **Status:** standing.
333+
**Collapses:** §23.1 items 2–3, §27 7b (2026-10-02; the top-level `isPending(count)` affordance was lost by this ruling, knowingly), recovery 9.1–9.3. **Status:** standing. Once per show: `tests/createErrorBoundary.test.ts` ("instantiates the fallback once per show"), `packages/web/test/errored-show-retry-3991.spec.tsx`; the held swap: `tests/boundary-fallback-held-swap-3991.test.ts`.
334334

335335
---
336336

‎packages/signals/src/boundaries.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import { NotReadyError, unwrapStatusError } from "./core/error.js";
6161
import { reportClientError } from "./core/error-hooks.js";
6262
import { link } from "./core/graph.js";
6363
import { enqueueSub } from "./core/heap.js";
64-
import { cleanup, createOwner, getOwner } from "./core/owner.js";
64+
import { cleanup, createOwner, dispose, getOwner } from "./core/owner.js";
6565
import {
6666
flushTransaction,
6767
GlobalQueue,
@@ -120,6 +120,10 @@ interface Boundary {
120120
_collapsed: boolean;
121121
_error: Signal<unknown> | null;
122122
_show: (b: Boundary) => unknown;
123+
/** The fallback's own computed, in its own scope like `Show`'s function
124+
* child: instantiated once per show and kept across the output's
125+
* re-derives while it shows (#3991). */
126+
_fb: Computed<unknown> | null;
123127
}
124128

125129
/** Context key: the nearest boundary of a node, inherited at creation. */
@@ -505,7 +509,8 @@ function createBoundary<T>(
505509
_gated: false,
506510
_collapsed: false,
507511
_error: type === STATUS_ERROR ? signal<unknown>(undefined, ERROR_SIGNAL) : null,
508-
_show: show
512+
_show: show,
513+
_fb: null
509514
};
510515
const controller = type === STATUS_PENDING ? (owner._context[REVEAL] as object | null) : null;
511516
if (getOwner())
@@ -646,6 +651,20 @@ function createBoundary<T>(
646651
if (__OBSERVE__ && b._fallback && b._type & STATUS_PENDING && attrHooks !== null)
647652
attrHooks.boundaryFallback(b, b._tree, false);
648653
b._fallback = false;
654+
// The fallback dies when the content that replaces it is on screen —
655+
// with this flush's commits when the pass is the frame's, at the
656+
// landing when the frame parks (the run is held with it, as the old
657+
// children of a held pass are) — not now: a held swap keeps the
658+
// fallback on screen, live, until it commits. A pass back to the
659+
// fallback before then finds it still built and keeps it.
660+
const fb = b._fb;
661+
if (fb !== null)
662+
globalQueue.enqueue(EFFECT_RENDER, () => {
663+
if (!b._fallback && b._fb === fb) {
664+
dispose(fb);
665+
b._fb = null;
666+
}
667+
});
649668
release(b);
650669
// Shown: the slot leaves its reveal order (a later re-arm gates nobody
651670
// else; the frontier moves on).
@@ -689,7 +708,22 @@ function fallback<T>(b: Boundary): T {
689708
if (!globalQueue._running) schedule();
690709
}
691710
b._fallback = true;
692-
return b._show(b) as T;
711+
// Built in its own scope, like `Show`'s function child, once per show
712+
// (D-39: the fallback is the display until a new answer replaces the
713+
// error): a re-derive of the output while it shows — a second reader
714+
// caught, the same error propagated again through a sibling's re-run, a
715+
// retry pending after `reset()`, a later error — keeps the built
716+
// fallback, its DOM and focus with it (#3991). A later error reaches it
717+
// through the error accessor; only a fallback reading the accessor at its
718+
// top level is rebuilt by it. Created beside the output — in its owner,
719+
// outside the boundary, as the output is: its errors and runs are the
720+
// enclosing boundary's — with the output's id, so what it builds gets the
721+
// ids a build under the output would; disposed once content is on screen.
722+
if (b._fb === null) {
723+
const out = getOwner() as Computed<any>;
724+
b._fb = runWithOwner(out._parent, () => computed(() => b._show(b), { id: out.id }))!;
725+
}
726+
return read(b._fb) as T;
693727
}
694728

695729
const ERROR_SIGNAL = { ownedWrite: true, _noSnapshot: true } as const;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
2+
import {
3+
action,
4+
createLoadingBoundary,
5+
createMemo,
6+
createRenderEffect,
7+
createRoot,
8+
createSignal,
9+
flush,
10+
onCleanup,
11+
untrack
12+
} from "../src/index.js";
13+
beforeEach(() => vi.useFakeTimers());
14+
afterEach(() => vi.useRealTimers());
15+
const resolve = (v: unknown): unknown => {
16+
while (typeof v === "function") v = (v as () => unknown)();
17+
return v;
18+
};
19+
20+
// S-14: a fresh Loading mounted beside a hold shows its fallback now; its
21+
// content derives from the hold and reveals at the commit. The fallback stays
22+
// on screen, live, until then, and is disposed only with the commit.
23+
it("a held swap to content keeps the fallback live until the commit", async () => {
24+
let releaseGate!: () => void;
25+
const gate = new Promise<void>(r => (releaseGate = r));
26+
const [count, setCount] = createSignal(0);
27+
const [open, setOpen] = createSignal(false);
28+
const screen: unknown[] = [];
29+
const fb: string[] = [];
30+
createRoot(() => {
31+
const slow = createMemo(async () => {
32+
const c = count();
33+
await new Promise(r => setTimeout(r, 10));
34+
return c;
35+
});
36+
createRenderEffect(count, () => {});
37+
createRenderEffect(slow, () => {});
38+
const view = createMemo(() =>
39+
open()
40+
? untrack(() =>
41+
createLoadingBoundary(
42+
() => () => `content ${slow()}`,
43+
() => {
44+
fb.push("built");
45+
onCleanup(() => fb.push("disposed"));
46+
return "fallback";
47+
}
48+
)
49+
)
50+
: "closed"
51+
);
52+
createRenderEffect(
53+
() => resolve(view()),
54+
v => void screen.push(v)
55+
);
56+
});
57+
flush();
58+
await vi.advanceTimersByTimeAsync(20);
59+
flush();
60+
expect(screen.at(-1)).toBe("closed");
61+
const p = action(function* () {
62+
setCount(1);
63+
yield gate;
64+
})();
65+
flush();
66+
setOpen(true);
67+
flush();
68+
expect(screen.at(-1)).toBe("fallback");
69+
expect(fb).toEqual(["built"]);
70+
await vi.advanceTimersByTimeAsync(20);
71+
flush(); // slow lands 1 — inside the hold
72+
expect(screen.at(-1)).toBe("fallback"); // the swap is held
73+
expect(fb).toEqual(["built"]); // the shown fallback is still live
74+
releaseGate();
75+
await p;
76+
flush();
77+
expect(screen.at(-1)).toBe("content 1");
78+
expect(fb).toEqual(["built", "disposed"]); // disposed with the commit
79+
});
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
// #3991: an <Errored> whose content reads a failing async memo through a
6+
// <Show> renders its fallback once per failure, and a reset() whose retry
7+
// fails again leaves the same fallback in place (D-39: the error fallback
8+
// is the display until a new answer replaces the error).
9+
import { afterEach, beforeEach, expect, test, vi } from "vitest";
10+
import { Errored, Loading, Show, createMemo, flush, isPending } from "solid-js";
11+
import { render } from "../src/index.js";
12+
13+
beforeEach(() => vi.useFakeTimers());
14+
afterEach(() => vi.useRealTimers());
15+
16+
const settle = async () => {
17+
for (let i = 0; i < 5; i++) {
18+
await vi.advanceTimersByTimeAsync(5);
19+
flush();
20+
}
21+
};
22+
23+
for (const variant of ["show", "text", "outside"] as const)
24+
test(`one fallback per failure; a failed retry keeps the fallback (${variant})`, async () => {
25+
let calls = 0;
26+
let fallbacks = 0;
27+
let reset!: () => void;
28+
let rejectSecond!: (e: unknown) => void;
29+
const second = new Promise<number>((_, r) => (rejectSecond = r));
30+
second.catch(() => {});
31+
function App() {
32+
const data = createMemo(async (): Promise<number> => {
33+
calls++;
34+
if (calls === 1) throw new Error("first failure");
35+
return second;
36+
});
37+
return (
38+
<>
39+
{variant === "outside" && (
40+
<Loading>
41+
<Errored fallback={null}>
42+
<Show when={data()}>
43+
<span>outside</span>
44+
</Show>
45+
</Errored>
46+
</Loading>
47+
)}
48+
<Loading>
49+
<Errored
50+
fallback={(_e, r) => {
51+
fallbacks++;
52+
reset = r;
53+
return <button type="button">Retry</button>;
54+
}}
55+
>
56+
<Show when={isPending(() => data())}>
57+
<p>updating</p>
58+
</Show>
59+
{variant === "text" ? (
60+
<p>{data()}</p>
61+
) : (
62+
<Show when={data() > 0}>
63+
<p>content</p>
64+
</Show>
65+
)}
66+
</Errored>
67+
</Loading>
68+
</>
69+
);
70+
}
71+
const container = document.createElement("div");
72+
const dispose = render(() => <App />, container);
73+
await settle();
74+
const first = container.querySelector("button");
75+
expect(first).not.toBeNull();
76+
expect(fallbacks).toBe(1);
77+
reset();
78+
await settle();
79+
expect(calls).toBe(2);
80+
rejectSecond(new Error("second failure"));
81+
await settle();
82+
// The fallback is instantiated once per show: the second error reaches
83+
// it through the error accessor, the same button stays (focus with it).
84+
expect(fallbacks).toBe(1);
85+
expect(container.querySelector("button")).toBe(first);
86+
dispose();
87+
});

‎scripts/size/scenarios.js‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,8 +2594,16 @@ module.exports = [
25942594
// condition hello world stays under 10 KB.
25952595
// #3928 merged with next @ 617d19511 (2026-10-09): measured at 12,976 B
25962596
// (36,793 B minified). Cap set to that brotli, not above it.
2597-
limit: "12.976 KB",
2598-
capMinified: 36793,
2597+
// Size-Exception (a boundary's fallback is instantiated once per show,
2598+
// #3991, 2026-10-12): 12.976 KB -> 13.03 KB, measured locally at 13,020 B
2599+
// against `next`'s 12,946 (+74 B; +186 B minified, 36,746 -> 36,932) —
2600+
// boundaries.ts: the fallback is built in a computed of its own beside
2601+
// the output and kept across the output's re-derives while it shows
2602+
// (`_fb`), disposed by a queued run once the content that replaces it is
2603+
// on screen. Cap set at measured + 10 B rounded up to 0.01 KB. Accepted
2604+
// by the maintainer 2026-10-12.
2605+
limit: "13.03 KB",
2606+
capMinified: 36932,
25992607
alias
26002608
},
26012609
{
@@ -2882,8 +2890,12 @@ module.exports = [
28822890
// condition hello world stays under 10 KB.
28832891
// #3884 stacked on next (2026-10-08): measured at 14,552 B brotli / 41,305 B minified
28842892
// by CI (Size run 37877131031). Cap set to that brotli size, rounded up to 0.01 KB.
2885-
limit: "14.56 KB",
2886-
capMinified: 41305,
2893+
// Size-Exception (#3991, 2026-10-12; the CSR note): 14.56 KB -> 14.59 KB,
2894+
// measured locally at 14,573 B against `next`'s 14,565 (+8 B; +186 B
2895+
// minified, 41,274 -> 41,460). Cap set at measured + 10 B rounded up to
2896+
// 0.01 KB. Accepted by the maintainer 2026-10-12.
2897+
limit: "14.59 KB",
2898+
capMinified: 41460,
28872899
alias: observeAlias
28882900
},
28892901
{
@@ -3439,8 +3451,13 @@ module.exports = [
34393451
// #3928 (2026-10-09): a select value is reapplied when its options arrive,
34403452
// including multiple. Merged with next @ 617d19511: 25,465 B brotli
34413453
// (79,753 B minified). Cap set to that brotli, not above it.
3442-
limit: "25.465 KB",
3443-
capMinified: 79753,
3454+
// Size-Exception (#3991, 2026-10-12; the CSR note): the todo app's
3455+
// Loading pays the once-per-show fallback — 25.465 KB -> 25.51 KB,
3456+
// measured locally at 25,491 B against `next`'s 25,424 (+67 B; +186 B
3457+
// minified, 79,719 -> 79,905). Cap set at measured + 10 B rounded up to
3458+
// 0.01 KB. Accepted by the maintainer 2026-10-12.
3459+
limit: "25.51 KB",
3460+
capMinified: 79905,
34443461
alias
34453462
},
34463463
{

0 commit comments

Comments
 (0)