Skip to content

Commit 2861ab8

Browse files
ryansolidclaude
andauthored
fix(signals): an error staged on its source keeps <Errored> waiting (#4364) (#4387)
* fix(signals): an error staged on its source keeps <Errored> waiting (#4364) An uninitialized async memo whose re-run throws from a dependency lost the error, and Loading revealed an empty hole. The dependency's error was staged for the commit (D-41), but a reader threw it at once: a render effect reading a memo born staged, or the memo itself through latest(). That error reached <Errored> naming the source. The boundary's output pass then took the source as settled, because its error was only staged, so it dropped the source and showed the content. When the commit published the error, every reader already held it, and nothing reached the boundary again. prune() now treats a staged error on an error boundary's source as unsettled. It also inlines the one-line `unsettled` helper and restructures the loop, so the change is -12 B minified. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * docs(signals): why catchStatus's cleared path needs no staged-error guard (#4364) 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 910abc1 commit 2861ab8

4 files changed

Lines changed: 160 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+
An uninitialized async memo whose re-run throws from a dependency now reaches the wrapping `<Errored>` (#4364). The dependency's error was staged for the commit (D-41), but a reader threw it at once: a render effect reading a memo born staged, or the memo itself through `latest()`. The error reached the boundary naming that source, and the boundary's next pass took the source as settled because its error was only staged. It dropped the source and showed the content, so `Loading` revealed an empty hole that never recovered. An error staged on its source now keeps the boundary waiting until the commit publishes it.

‎packages/signals/src/boundaries.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import {
4343
CAUGHT_REARMED,
4444
CONFIG_REDERIVE,
45+
CONFIG_STAGED_ERROR,
4546
CONFIG_FRAME,
4647
CONFIG_HELD,
4748
CONFIG_OVERRIDE,
@@ -232,16 +233,20 @@ function catchStatus(
232233
): boolean | typeof CAUGHT_REARMED {
233234
if (flags === 0) {
234235
// A status cleared. Judged by the node's status now, against each
235-
// boundary's own rule (`unsettled`) — the error path clears pending
236+
// boundary's own rule (`prune`'s mask) — the error path clears pending
236237
// first and the node is still errored then. The last reader a fallback
237238
// waited on settling re-derives the output now, in the same heap (a
238239
// held reader's landing has joined its transaction, so the content
239240
// lands with its run, not a round after). Others still waiting: nothing
240-
// on screen changes, the fallback stays as it is.
241+
// on screen changes, the fallback stays as it is. `node` here is an
242+
// effect reporting its own status. An error boundary records an effect
243+
// only while it is errored, and a staged error keeps STATUS_ERROR on a
244+
// node that had it (`stageError`), so `prune`'s CONFIG_STAGED_ERROR case
245+
// cannot reach this path (#4364).
241246
if (collecting.size !== 0)
242247
for (let b: Boundary | null | undefined = boundaryOf(node); b; b = b._parent)
243248
if (
244-
!(node._statusFlags & unsettled(b)) &&
249+
!(node._statusFlags & (b._type | STATUS_PENDING)) &&
245250
b._readers.delete(node) &&
246251
b._readers.size === 0
247252
) {
@@ -316,15 +321,18 @@ function release(b: Boundary): void {
316321
* that landed but is held (its value staged in a transaction) is not: the
317322
* content reveals at the commit, with its run. The tree is: the output reads
318323
* it and enters the transaction itself (its committed value shows until the
319-
* landing). */
320-
const unsettled = (b: Boundary): number =>
321-
b._type === STATUS_ERROR ? STATUS_ERROR | STATUS_PENDING : STATUS_PENDING;
322-
324+
* landing). The statuses that keep a reader: pending — and its error, for an
325+
* error boundary (`_type` is the status it catches). */
323326
export function prune(b: Boundary, pass: boolean): number {
324-
const mask = unsettled(b);
327+
const mask = b._type | STATUS_PENDING;
325328
for (const r of b._readers) {
326-
if (r._flags & REACTIVE_DISPOSED) b._readers.delete(r);
327-
else if (!(r._statusFlags & mask)) {
329+
if (!(r._flags & REACTIVE_DISPOSED)) {
330+
// An error staged on its source (D-41, `stageError`) is not settled:
331+
// it publishes with the commit. A reader that threw it now (a lane
332+
// pass, a reader of a node born staged) reached the boundary first
333+
// (#4364).
334+
if (r._statusFlags & mask || (mask & STATUS_ERROR && r._config & CONFIG_STAGED_ERROR))
335+
continue;
328336
if (
329337
(r !== b._tree || b._armed) &&
330338
(r._config & (CONFIG_HELD | CONFIG_OVERRIDE)) === CONFIG_HELD
@@ -339,8 +347,8 @@ export function prune(b: Boundary, pass: boolean): number {
339347
if (!pass || !b._initialized) continue;
340348
joinFuture(txOf(r));
341349
}
342-
b._readers.delete(r);
343350
}
351+
b._readers.delete(r);
344352
}
345353
return b._readers.size;
346354
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// #4364 (D-41, F-12): an error staged on its source is not settled. A memo's
2+
// pass throws in a flush that carries a write, so its error is staged
3+
// (`stageError`) and publishes with the commit. A reader that throws the same
4+
// error during the pass publishes it at once: an uninitialized async memo's
5+
// render effect reading the memo's staged error (it read a node born staged),
6+
// or the memo itself reading it through `latest()` (a verdict lane pass). The
7+
// error reaches `<Errored>` naming the source, whose own error is still only
8+
// staged. The boundary's output pass took that source as settled, dropped it,
9+
// and showed the content: an empty hole under a revealed `<Loading>`, and the
10+
// commit's publish found every reader already holding that error, so nothing
11+
// told the boundary again.
12+
import { expect, it } from "vitest";
13+
import {
14+
createErrorBoundary,
15+
createLoadingBoundary,
16+
createMemo,
17+
createRenderEffect,
18+
createRoot,
19+
createSignal,
20+
flush,
21+
latest,
22+
untrack
23+
} from "../src/index.js";
24+
25+
const tick = async () => {
26+
for (let i = 0; i < 8; i++) await Promise.resolve();
27+
flush();
28+
};
29+
30+
it.each(["m0() + s()", "latest(m0)", "m0()"])(
31+
"an uninitialized async memo reading %s reaches <Errored> when its dependency throws",
32+
async shape => {
33+
const [s, set] = createSignal(0);
34+
let release!: (v: unknown) => void;
35+
const gate = new Promise(r => (release = r));
36+
let out = "";
37+
const dispose = createRoot(dispose => {
38+
const m0 = createMemo(() => {
39+
if (s() === 2) throw new Error("boom");
40+
return s();
41+
});
42+
const m1 = createMemo(() => {
43+
const v = shape === "latest(m0)" ? latest(m0) : shape === "m0()" ? m0() : m0() + s();
44+
return gate.then(() => v);
45+
});
46+
// `<Errored><Loading><span>{m1()}</span></Loading></Errored>`
47+
const view = createErrorBoundary(
48+
() =>
49+
createLoadingBoundary(
50+
() =>
51+
untrack(() => {
52+
createRenderEffect(
53+
() => m1(),
54+
() => {}
55+
);
56+
return "content";
57+
}),
58+
() => "[L]"
59+
),
60+
(e: () => any) => `[E ${e()?.message}]`
61+
);
62+
createRenderEffect(
63+
() => {
64+
const v = view();
65+
return typeof v === "function" ? (v as () => unknown)() : v;
66+
},
67+
v => void (out = String(v))
68+
);
69+
return dispose;
70+
});
71+
flush();
72+
expect(out).toBe("[L]");
73+
set(2);
74+
flush();
75+
expect(out).toBe("[E boom]");
76+
release(1);
77+
await tick();
78+
expect(out).toBe("[E boom]");
79+
dispose();
80+
}
81+
);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*
5+
* #4364: an uninitialized async memo whose re-run throws from a dependency
6+
* reaches the wrapping <Errored>. The dependency's error is staged (D-41)
7+
* while the read that throws it publishes at once; the boundary took the
8+
* staged source as settled and `Loading` revealed an empty hole.
9+
*/
10+
import { expect, test } from "vitest";
11+
import { render } from "../src/index.js";
12+
import { createMemo, createSignal, Errored, Loading, flush, latest } from "solid-js";
13+
14+
const settle = async () => {
15+
for (let i = 0; i < 6; i++) {
16+
await Promise.resolve();
17+
flush();
18+
}
19+
};
20+
21+
test.each(["m0() + s()", "latest(m0)", "m0()"])(
22+
"Errored > Loading > an async memo reading %s shows the dependency's error",
23+
async shape => {
24+
const el = document.createElement("div");
25+
const [s, set] = createSignal(0);
26+
let release!: (v: unknown) => void;
27+
const gate = new Promise(r => (release = r));
28+
const dispose = render(() => {
29+
const m0 = createMemo(() => {
30+
if (s() === 2) throw new Error("boom");
31+
return s();
32+
});
33+
const m1 = createMemo(() => {
34+
const v = shape === "latest(m0)" ? latest(m0) : shape === "m0()" ? m0() : m0() + s();
35+
return gate.then(() => v);
36+
});
37+
return (
38+
<Errored fallback={(e: () => any) => `[E ${e()?.message}]`}>
39+
<Loading fallback="[L]">
40+
<span>{m1()}</span>
41+
</Loading>
42+
</Errored>
43+
);
44+
}, el);
45+
flush();
46+
expect(el.innerHTML).toBe("[L]");
47+
set(2);
48+
flush();
49+
expect(el.innerHTML).toBe("[E boom]");
50+
release(1);
51+
await settle();
52+
expect(el.innerHTML).toBe("[E boom]");
53+
dispose();
54+
}
55+
);

0 commit comments

Comments
 (0)