Skip to content

Commit b8af6b9

Browse files
committed
fix(signals): a derivation of a staged error re-runs instead of carrying it
D-41: a derivation that reads a staged error throws it, as it would read a staged value: its own pass errors and stages with the same frame. The earlier change marked every derivation under a staged error with that error without running it, so a memo or derived-store derive that catches the error showed an error it never threw for the whole hold, and a derived store's keys that never read the source threw it too (D-30). Read only through latest(), both ended errored after the commit. The settle walk now re-runs those derivations; a pass that catches answers with its own value. A boundary's output and tree, async memos, effects and nodes that own children (a boundary's content, #3945) are left to the commit's publish as before. A projection's derive stages its pass's error like a memo. A published error no longer marks a node that holds its own pass's answer to the frame, which also fixes a catching memo ending errored when the error's flush has no hold. The silent-recovery pin counts the dependent's own errored pass. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent d526a01 commit b8af6b9

5 files changed

Lines changed: 240 additions & 37 deletions

File tree

‎.changeset/staged-error-reaches-derivations.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
A memo's error staged with a hold now reaches the memos and derived stores that derive from it: `latest()` of them throws the proposed error and `isPending()` of them is `true` until the hold commits, as for the memo itself (D-41).
5+
A memo's error staged with a hold now reaches the memos and derived stores that derive from it: they re-run against it, so `latest()` of them throws the proposed error and `isPending()` of them is `true` until the hold commits, as for the memo itself (D-41). A memo or derived-store derive that catches the error answers with its own value instead, during the hold and after it: it no longer ends errored with an error its pass never threw, and a derived store's other keys keep their values (D-30).

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

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,13 @@ export function stageError(el: Computed<any>, error: any, inPass?: boolean): boo
195195
// Staged whether or not the flush has joined a hold yet: a pass that runs
196196
// before its async sibling joins is the seam's to decide, like a value
197197
// (#4109). A flush that parks nothing publishes it at its commit (S-38).
198-
// Not a projection's derive: its readers learn its error through the
199-
// family, which the commit's publish never reaches (#4189).
198+
// A projection's derive too (D-41): its pass's throw has already sent the
199+
// family's readers to re-derive (store/projection.ts `errorFamily`), and
200+
// they derive from the staging like a memo's readers (#4189).
200201
if (
201202
!(
202203
el._config & CONFIG_HELD ||
203-
(inPass && el._flags & REACTIVE_STAGED_READ && globalQueue._running && !(el as any).fam)
204+
(inPass && el._flags & REACTIVE_STAGED_READ && globalQueue._running)
204205
) ||
205206
passLane
206207
)
@@ -215,38 +216,12 @@ export function stageError(el: Computed<any>, error: any, inPass?: boolean): boo
215216
el._config |= CONFIG_STAGED | CONFIG_STAGED_ERROR;
216217
// Readers parked on the flight re-run and derive from the staging; the
217218
// verdict readers the walk wakes need a flush (#4106); the derivations it
218-
// reaches carry the staging (`settlePendingSource`).
219+
// reaches derive from it (`settlePendingSource`).
219220
settlePendingSource(el);
220221
schedule();
221222
return true;
222223
}
223224

224-
/** D-41: a derivation of a staged error carries it, staged with the same
225-
* frame — marked, not re-run, as a published error marks it (a re-run would
226-
* replace the frame its content owns) — and its own verdict readers
227-
* re-derive (the settle walk). Only a verdict reads a derivation's staging
228-
* before its commit, so only the verdict engine marks. Not a boundary's
229-
* output, an async memo, an effect or a boundary's tree (`EFFECT_TRACKED <<
230-
* 23` spans CONFIG_FRAME and every effect type), nor one already carrying a
231-
* staged error: the commit's publish reaches them. A pending mark supersedes
232-
* the staging (`propagateStatus`). */
233-
function stageMark(node: Computed<any>, el: Computed<any>): void {
234-
if (
235-
!(
236-
node._config &
237-
(CONFIG_REDERIVE | CONFIG_STAGED_ERROR | CONFIG_ASYNC | (EFFECT_TRACKED << 23))
238-
)
239-
) {
240-
node._pendingValue = el._pendingValue;
241-
node._config |= CONFIG_STAGED_ERROR;
242-
queuePendingNode(node);
243-
// A derived store's readers are its family's (store/projection.ts): they
244-
// re-derive from the staging, as from the derive's status (#4189).
245-
(node as any).fam?.st(STATUS_ERROR);
246-
settlePendingSource(node);
247-
}
248-
}
249-
250225
export function settleErroredDependents(el: Computed<any>, error: any): void {
251226
let scheduled = false;
252227
const visited = new NewSet<Computed<any>>();
@@ -345,12 +320,24 @@ export function settlePendingSource(el: Computed<any>, source: Computed<any> = e
345320
// equal to the committed value notifies nobody — the source settling
346321
// is its verdict changing: it runs again. (Verdict readers exist only
347322
// with the verdict engine installed.)
348-
// D-41: a derivation of a staged error carries it (`stageMark`).
323+
// D-41: a derivation that reads a staged error throws it, as it would
324+
// read a staged value — its own pass errors and stages with the same
325+
// frame. So it re-runs: the pass decides, and one that catches answers
326+
// with its own value (a derived store's derive included: each key is
327+
// its own, D-30). Only a verdict reads a derivation's staging before
328+
// its commit, so only the verdict engine re-runs it. Not a boundary's
329+
// output, an async memo, an effect or a boundary's tree (`EFFECT_TRACKED
330+
// << 23` spans CONFIG_FRAME and every effect type), nor a node that
331+
// owns children (a boundary's content, #3945: a re-run would replace
332+
// the frame they are): the commit's publish reaches them.
349333
return (
350334
holdWorkHook &&
351335
(node._config & CONFIG_VERDICT
352336
? enqueueSub(node)
353-
: el._config & CONFIG_STAGED_ERROR && stageMark(node, el))
337+
: el._config & CONFIG_STAGED_ERROR &&
338+
!(node._config & (CONFIG_REDERIVE | CONFIG_ASYNC | (EFFECT_TRACKED << 23))) &&
339+
!node._firstChild &&
340+
enqueueSub(node))
354341
);
355342
visited.add(node);
356343
node._time = clock;
@@ -1040,8 +1027,8 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
10401027
// asks that question again. It waits on the source like any derivation
10411028
// (F-9), and asks its next question once, at the landing (#4397).
10421029
// A node carrying a staged error re-derives on a pending mark: its
1043-
// source asks again, and the staging it carried is not the answer
1044-
// (#4109, `stageMark`).
1030+
// source asks again, and the staging it re-derived from is not the
1031+
// answer (#4109).
10451032
if (
10461033
sub._config & CONFIG_REDERIVE ||
10471034
(holdWorkHook &&
@@ -1072,7 +1059,13 @@ export function propagateStatus(el: Computed<any>, status: number, error: any):
10721059
}
10731060
if (
10741061
(status === STATUS_PENDING && pendingSource) ||
1075-
(status !== STATUS_PENDING && (sub._x?._error !== error || sub._x?._pendingSources))
1062+
// An error does not mark a node holding an answer of its own pass to
1063+
// this frame (D-41: it derived from the staging this commit
1064+
// publishes): that answer commits with the frame, a caught error a
1065+
// value.
1066+
(status !== STATUS_PENDING &&
1067+
sub._pendingValue === NOT_PENDING &&
1068+
(sub._x?._error !== error || sub._x?._pendingSources))
10761069
) {
10771070
// The marked subscriber is queued for the commit sweep so its status
10781071
// bookkeeping (loading window, uninitialized clear) runs with the flush

‎packages/signals/tests/error-silent-recovery.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ describe("silent error recovery (#2949)", () => {
7272
setB(-1);
7373
flush();
7474
expect(out()).toBe("err:b=-1");
75+
// The dependent's own pass meets the error (D-41: a derivation that
76+
// reads a staged error throws it — its own pass errors).
77+
expect(runs()).toBe(runsBefore + 1);
7578

7679
setB(0);
7780
flush();
7881
expect(out()).toBe("ok:0");
7982
// The sweep re-runs the errored dependent once; it commits an unchanged
8083
// value and everything settles.
81-
expect(runs()).toBe(runsBefore + 1);
84+
expect(runs()).toBe(runsBefore + 2);
8285
});
8386

8487
test("changed-value recovery still rides normal notification", () => {
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* D-41: a derivation that reads a staged error throws it, as it would read a
3+
* staged value — its own pass errors and stages with the same frame. A memo
4+
* whose pass catches the error does not error: its answer is its own value,
5+
* staged with the frame and committed with it.
6+
*
7+
* `m0` throws for the action's write, which a hold keeps; `m` catches it.
8+
*/
9+
import { afterEach, expect, it } from "vitest";
10+
import {
11+
action,
12+
createMemo,
13+
createRenderEffect,
14+
createRoot,
15+
createSignal,
16+
flush,
17+
isPending,
18+
latest,
19+
resetErrorHalt
20+
} from "../src/index.js";
21+
22+
afterEach(() => resetErrorHalt());
23+
const tick = async () => {
24+
await new Promise(r => setTimeout(r, 0));
25+
flush();
26+
};
27+
const attempt = (fn: () => unknown) => {
28+
try {
29+
return fn();
30+
} catch (e: any) {
31+
return `throws ${e?.cause?.message ?? e?.message}`;
32+
}
33+
};
34+
35+
function setup(plainReader: boolean) {
36+
const [s, setS] = createSignal(2);
37+
let m!: () => unknown;
38+
const latestScreens: unknown[] = [];
39+
const dispose = createRoot(d => {
40+
const m0 = createMemo(() => {
41+
const v = s() + 1;
42+
if (v % 4 === 1) throw new Error(`m0:${v}`);
43+
return v;
44+
});
45+
m = createMemo(() => {
46+
try {
47+
return `ok:${m0()}`;
48+
} catch {
49+
return "caught";
50+
}
51+
});
52+
createRenderEffect(
53+
() => attempt(() => latest(m)),
54+
v => void latestScreens.push(v)
55+
);
56+
if (plainReader)
57+
createRenderEffect(
58+
() => [attempt(() => m()), isPending(m)],
59+
() => {}
60+
);
61+
return d;
62+
});
63+
flush();
64+
return { setS, m: () => m, latestScreens, dispose };
65+
}
66+
67+
it("a catching memo never shows its source's staged error", async () => {
68+
const { setS, m, latestScreens, dispose } = setup(true);
69+
let release!: () => void;
70+
action(function* () {
71+
setS(0);
72+
yield new Promise<void>(r => (release = r));
73+
})();
74+
await tick();
75+
// Its own pass's answer to the frame, seen early through `latest`.
76+
expect(attempt(() => latest(m()))).toBe("caught");
77+
expect(attempt(() => m()())).toBe("ok:3");
78+
release();
79+
await tick();
80+
await tick();
81+
expect(latestScreens).toEqual(["ok:3", "caught"]);
82+
expect(attempt(() => m()())).toBe("caught");
83+
dispose();
84+
});
85+
86+
it("a catching memo read only through latest() commits its own answer", async () => {
87+
const { setS, m, latestScreens, dispose } = setup(false);
88+
let release!: () => void;
89+
action(function* () {
90+
setS(0);
91+
yield new Promise<void>(r => (release = r));
92+
})();
93+
await tick();
94+
release();
95+
await tick();
96+
await tick();
97+
expect(attempt(() => m()())).toBe("caught");
98+
expect(latestScreens).toEqual(["ok:3", "caught"]);
99+
dispose();
100+
});
101+
102+
it("a catching memo over an error of a flush with no hold commits its own answer", () => {
103+
const { setS, m, latestScreens, dispose } = setup(false);
104+
setS(0);
105+
flush();
106+
expect(attempt(() => m()())).toBe("caught");
107+
expect(latestScreens).toEqual(["ok:3", "caught"]);
108+
dispose();
109+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* D-41 and D-30: a derived store whose derive catches its source's staged
3+
* error answers with its own pass: no key throws the error, and a key that
4+
* never read the source (`a`) keeps its value — pending is per key.
5+
*
6+
* `d => { d.a = 7; try { d.b = m0() } catch { d.b = "caught" } }`; an action
7+
* makes `m0` throw and a hold keeps the write.
8+
*/
9+
import { afterEach, expect, it } from "vitest";
10+
import {
11+
action,
12+
createMemo,
13+
createRenderEffect,
14+
createRoot,
15+
createSignal,
16+
createStore,
17+
flush,
18+
latest,
19+
resetErrorHalt
20+
} from "../src/index.js";
21+
22+
afterEach(() => resetErrorHalt());
23+
const tick = async () => {
24+
await new Promise(r => setTimeout(r, 0));
25+
flush();
26+
};
27+
const attempt = (fn: () => unknown) => {
28+
try {
29+
return fn();
30+
} catch (e: any) {
31+
return `throws ${e?.cause?.message ?? e?.message}`;
32+
}
33+
};
34+
35+
function setup(plainReader: boolean) {
36+
const [s, setS] = createSignal(2);
37+
let st!: { a?: number; b?: unknown };
38+
const dispose = createRoot(d => {
39+
const m0 = createMemo(() => {
40+
const v = s() + 1;
41+
if (v % 4 === 1) throw new Error(`m0:${v}`);
42+
return v;
43+
});
44+
const [store] = createStore<{ a?: number; b?: unknown }>(dr => {
45+
dr.a = 7;
46+
try {
47+
dr.b = m0();
48+
} catch {
49+
dr.b = "caught";
50+
}
51+
}, {});
52+
st = store;
53+
createRenderEffect(
54+
() => [attempt(() => latest(() => store.a)), attempt(() => latest(() => store.b))],
55+
() => {}
56+
);
57+
if (plainReader)
58+
createRenderEffect(
59+
() => [attempt(() => store.a), attempt(() => store.b)],
60+
() => {}
61+
);
62+
return d;
63+
});
64+
flush();
65+
return { setS, st: () => st, dispose };
66+
}
67+
68+
it("latest() of a key that never read the source does not throw its staged error", async () => {
69+
const { setS, st, dispose } = setup(true);
70+
let release!: () => void;
71+
action(function* () {
72+
setS(0);
73+
yield new Promise<void>(r => (release = r));
74+
})();
75+
await tick();
76+
expect(attempt(() => latest(() => st().a))).toBe(7);
77+
expect(attempt(() => latest(() => st().b))).toBe("caught");
78+
release();
79+
await tick();
80+
await tick();
81+
expect([attempt(() => st().a), attempt(() => st().b)]).toEqual([7, "caught"]);
82+
dispose();
83+
});
84+
85+
it("a store read only through latest() commits its derive's own answer", async () => {
86+
const { setS, st, dispose } = setup(false);
87+
let release!: () => void;
88+
action(function* () {
89+
setS(0);
90+
yield new Promise<void>(r => (release = r));
91+
})();
92+
await tick();
93+
release();
94+
await tick();
95+
await tick();
96+
expect([attempt(() => st().a), attempt(() => st().b)]).toEqual([7, "caught"]);
97+
dispose();
98+
});

0 commit comments

Comments
 (0)