Skip to content

Commit 1e270cd

Browse files
committed
fix(signals): keep a verdict reader's lane routing and own-error recovery (async-gen #119, #1238)
- verdictValue: read the lane value before routing the reader into the verdict lane, so a node born in the lane with nothing in it yet is read as any uninitialized node (NotReady) without moving its reader (#1238). - reruns: a stale reader whose error is its own (thrown by its pass over the committed value it was served) re-derives at the landing; only an error from an errored source is skipped (#3945). With #4363 no longer committing `undefined`, async-gen case 119 reached this stuck error. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 25e42cb commit 1e270cd

3 files changed

Lines changed: 91 additions & 22 deletions

File tree

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import {
4949
} from "./dev.js";
5050
import { sweepDormant, trimStaleDeps } from "./graph.js";
5151
import { deleteFromHeap, enqueueSub, runHeap, type Heap } from "./heap.js";
52+
import type { StatusError } from "./error.js";
5253
import { devCheckQuiescent } from "./invariants.js";
5354
import { rethrowCleanupError } from "./owner.js";
5455
import type { Computed, Owner, Signal } from "./types.js";
@@ -1170,8 +1171,14 @@ export function reruns(u: Transaction): unknown {
11701171
const r = u._reruns[i];
11711172
// An errored reader's display is its error (D-39): the landing gives it
11721173
// nothing to re-derive, and a re-run would be a read that retries the
1173-
// errored source (#3945).
1174-
if (r._flags & REACTIVE_DISPOSED || r._statusFlags & STATUS_ERROR) continue;
1174+
// errored source (#3945). Unless the error is its own — thrown by its
1175+
// pass over the committed value it was served: the landing is the new
1176+
// answer of that input, and the reader's own re-derives on it (A15).
1177+
if (
1178+
r._flags & REACTIVE_DISPOSED ||
1179+
(r._statusFlags & STATUS_ERROR && (r._x!._error as StatusError).source !== r)
1180+
)
1181+
continue;
11751182
if (r._flags & REACTIVE_FRAME_READ || r._config & CONFIG_VERDICT) {
11761183
frame ||= (r as any)._type;
11771184
r._flags &= ~REACTIVE_FRAME_READ;

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -396,27 +396,29 @@ function verdictValue(el: Signal<any> | Computed<any>, c: Computed<any> | null):
396396
el._config & CONFIG_OVERRIDE &&
397397
!(owner._statusFlags & STATUS_ERROR && !(el._config & CONFIG_GUESS))
398398
) {
399-
const l = txOf(el);
400-
if (tracked) verdictRead(c!, l._parent ?? l);
401-
const flying = (owner._statusFlags & STATUS_PENDING) !== 0 && !quietPending(owner);
402-
if (el._config & CONFIG_GUESS) {
403-
if (
404-
probing &&
405-
(flying ||
406-
(el._pendingValue !== NOT_PENDING &&
407-
(!el._equals || !el._equals(el._x!._lane as any, el._pendingValue as any))))
408-
)
409-
probeFound = true;
410-
// A lane a correction folded under (D-34): `latest()` shows the hold's
411-
// world whole — the truth beneath each guess, else what it covered.
412-
if (latestActive && l.folded) return l.folded(null, el);
413-
return el._x!._lane;
414-
}
415-
// NOT_PENDING: a node born in the lane with nothing in it yet has no
416-
// value in either view — loading, not pending: NotReady below (A7, A19
417-
// exc. 1; #4363).
418-
const v = latestActive && !flying ? laneValueOf(el) : display(el);
399+
const l = txOf(el),
400+
flying = (owner._statusFlags & STATUS_PENDING) !== 0 && !quietPending(owner),
401+
guess = el._config & CONFIG_GUESS,
402+
// NOT_PENDING: a node born in the lane with nothing in it yet has no
403+
// value in either view — loading, not pending, and no verdict to read
404+
// ahead: it is read as any uninitialized node, NotReady below (A7, A19
405+
// exc. 1; #4363), its reader not moved into the verdict lane.
406+
v = guess ? 0 : latestActive && !flying ? laneValueOf(el) : display(el);
419407
if (v !== NOT_PENDING) {
408+
if (tracked) verdictRead(c!, l._parent ?? l);
409+
if (guess) {
410+
if (
411+
probing &&
412+
(flying ||
413+
(el._pendingValue !== NOT_PENDING &&
414+
(!el._equals || !el._equals(el._x!._lane as any, el._pendingValue as any))))
415+
)
416+
probeFound = true;
417+
// A lane a correction folded under (D-34): `latest()` shows the hold's
418+
// world whole — the truth beneath each guess, else what it covered.
419+
if (latestActive && l.folded) return l.folded(null, el);
420+
return el._x!._lane;
421+
}
420422
if (probing && flying) probeFound = true;
421423
return v;
422424
}

‎packages/signals/tests/latest-uninitialized-over-latest-4362.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
// grouped into effects.
1818
import { describe, expect, it } from "vitest";
1919
import {
20+
createErrorBoundary,
21+
createLoadingBoundary,
2022
createMemo,
2123
createRenderEffect,
2224
createRoot,
@@ -228,4 +230,62 @@ describe("uninitialized async memo over latest(s) (#4362, #4363)", () => {
228230
expect(Object.keys(out).length).toBe(9);
229231
expect(isPending(m)).toBe(false); // unowned: false (A16)
230232
});
233+
234+
// A verdict reader of a flight is served its committed value (A10, a memo
235+
// has no DOM) and re-derives at the landing (A15). A pass over that value
236+
// that throws is the reader's own error, not an errored source's (#3945):
237+
// the landing is a new answer of its input, and the reader re-derives on
238+
// it (D-39: the error stays until a new answer replaces it). Async-gen
239+
// case 119 reached this once #4363 stopped committing `undefined`.
240+
it("a verdict reader's own error from the served value recovers at the landing", async () => {
241+
const [s0, setS0] = createSignal(0);
242+
const [s1, setS1] = createSignal(0);
243+
const asks: [number, PromiseWithResolvers<number>][] = [];
244+
let screen: unknown = "init";
245+
createRoot(() => {
246+
const m0 = createMemo(() => {
247+
const d = Promise.withResolvers<number>();
248+
asks.push([1 + s1() + s0(), d]);
249+
return d.promise;
250+
});
251+
const m1 = createMemo(() => {
252+
const v = 1 + latest(s1) + m0();
253+
if (v === 8) throw new Error("m1:8");
254+
return v;
255+
});
256+
const b = createErrorBoundary(
257+
() =>
258+
createLoadingBoundary(
259+
() => m1(),
260+
() => "loading"
261+
),
262+
() => "error"
263+
);
264+
createRenderEffect(b, v => {
265+
screen = v;
266+
});
267+
});
268+
flush();
269+
const answer = async (i: number) => {
270+
asks[i][1].resolve(asks[i][0]);
271+
await new Promise(r => setTimeout(r, 0));
272+
flush();
273+
};
274+
const log: unknown[] = [];
275+
await answer(0);
276+
log.push(screen);
277+
setS1(3);
278+
flush();
279+
log.push(screen);
280+
await answer(1);
281+
log.push(screen);
282+
setS0(2);
283+
flush();
284+
log.push(screen);
285+
await answer(2);
286+
log.push(screen);
287+
expect(asks.map(a => a[0])).toEqual([1, 4, 6]);
288+
// 1+3+4 = 8 throws; m0 lands 6: 1+3+6.
289+
expect(log).toEqual([2, 5, "error", "error", 10]);
290+
});
231291
});

0 commit comments

Comments
 (0)