Skip to content

Commit 44cdb35

Browse files
committed
fix(signals): an answered-then-reasked first answer reaches a reader its landing released
servesStaged served a reader with no value only while it still waited on the source. When the memo's input is read through latest() or a sync memo, the write's re-ask reaches the memo after its landing ran, and the landing had already released the identity memo's wait: it ran with no value, waiting on nothing, and threw, so latest(identity(m)) and a derived store stayed on [ROOT-L] while latest(m) showed the answer (async-gen E2/E3 3289 #310, 91501 #1993, #2609). D-22 as amended says a derivation with no value of its own reads like the memo at any depth; which the flush ran first is not the reader's. A reader that is not pending is served too (one served pass a flush, as before); one pending on another source is not. The seat's D3' row says the same. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 37d60d1 commit 44cdb35

4 files changed

Lines changed: 128 additions & 10 deletions

File tree

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,17 +1964,20 @@ export function devRead(el: Signal<any> | Computed<any>): void {
19641964
* D-22 (amended 2026-10-30), F-9: `el`'s first value was staged this flush
19651965
* and `el` (`owner`) went pending on a re-ask in it — an async first answer
19661966
* landing in the tick a write re-asks it: answered, then re-asked. A memo
1967-
* `c` with no value, waiting on `el` (on the source a read of `el` parks on:
1968-
* `el` itself, or the async it derives from — at any depth), derives from
1969-
* that value, as a flush of the answer alone would have let it, then runs
1967+
* `c` with no value — waiting on `el` (on the source a read of `el` parks
1968+
* on: `el` itself, or the async it derives from — at any depth), or not
1969+
* waiting at all (its wait released by the landing, or a first pass) —
1970+
* derives from that value, as a flush of the answer alone would have let it
1971+
* (whether the landing ran before the re-ask reached `c` is the flush's
1972+
* order, not the reader's: D-22, at any depth), then runs
19701973
* again (the missed-wake latch) and waits on the re-ask holding it:
19711974
* `latest`/`isPending` of it read as of `el`. One pass: every read of it is
19721975
* served (the latch marks it); its wait on `el` is dropped, so the re-run
19731976
* throws and waits on `el` afresh. One served pass a flush (`_served`): a
19741977
* pass that throws anyway, on a read beside it, parks on `el` again, and its
19751978
* re-run is not served — served, it would re-run without end. Not a reader
1976-
* that does not wait on `el` (a first pass: no first load to have been
1977-
* answered), a render effect (the frame: its plain read throws), lane work
1979+
* waiting on another source, a render effect (the frame: its plain read
1980+
* throws), lane work
19781981
* or a verdict lane's pass (D-37: the screen plus the lane's own guesses),
19791982
* or a held node (the staging is the hold's). A plain read of such a memo
19801983
* throws as one of a memo with no value; only `latest`/`isPending` tell them
@@ -1991,8 +1994,9 @@ function servesStaged(c: Computed<any>, el: Signal<any> | Computed<any>, owner:
19911994
(c._flags & REACTIVE_MISSED_WAKE ||
19921995
(c._pendingValue === NOT_PENDING &&
19931996
c._x?._served !== clock &&
1994-
c._x?._pendingSources?.delete((owner._x!._error as NotReadyError).source) &&
1995-
((c._x!._served = clock), (c._flags |= REACTIVE_MISSED_WAKE))))
1997+
(!(c._statusFlags & STATUS_PENDING) ||
1998+
c._x?._pendingSources?.delete((owner._x!._error as NotReadyError).source)) &&
1999+
((ext(c)._served = clock), (c._flags |= REACTIVE_MISSED_WAKE))))
19962000
);
19972001
}
19982002

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,9 @@ export function slot(
919919
// D-22 (amended 2026-10-30), F-9: a first answer staged this flush and
920920
// re-asked in it — answered, then re-asked. A pass with no value of its
921921
// own, waiting on it (on the source a read of it parks on — the node,
922-
// or the async it derives from, at any depth), derives from it, once a
923-
// flush (`read`). Not lane work or a verdict lane's pass (D-37).
922+
// or the async it derives from, at any depth) or not waiting at all (its
923+
// wait released by the landing, or a first pass), derives from it, once
924+
// a flush (`read`). Not lane work or a verdict lane's pass (D-37).
924925
if (
925926
observeFlightHook &&
926927
cUninit &&
@@ -933,7 +934,8 @@ export function slot(
933934
(c!._flags & REACTIVE_MISSED_WAKE ||
934935
(c!._pendingValue === NOT_PENDING &&
935936
c!._x?._served !== clock &&
936-
!!c!._x?._pendingSources?.has(((el as Computed<any>)._x!._error as any).source)))
937+
(!(c!._statusFlags & STATUS_PENDING) ||
938+
!!c!._x?._pendingSources?.has(((el as Computed<any>)._x!._error as any).source))))
937939
)
938940
return carry(pred(rowBase + "D3'", SLOT_P));
939941
return carry(pred(leaf ? "L5" : rowBase + "D3", SLOT_NR)); // F-2 D-28 / F-2 D-9
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { describe, expect, it } from "vitest";
2+
import {
3+
createLoadingBoundary,
4+
createMemo,
5+
createRenderEffect,
6+
createRoot,
7+
createSignal,
8+
flush,
9+
latest
10+
} from "../src/index.js";
11+
12+
// D-22 (amended 2026-10-30), F-9, async generator E2 (3289 #310, 91501 #1993,
13+
// #2609): an async memo answered, then re-asked in one flush. When the
14+
// memo's input is read through `latest()` (or through a sync memo), the
15+
// write's re-ask reaches the memo after its landing has run, and the landing
16+
// released the identity memo's wait: it runs with no value and waits on
17+
// nothing. It still derives from the answer — which of the two the flush ran
18+
// first is the flush's order, not the reader's — so `latest` of it shows
19+
// what `latest` of the memo shows.
20+
const task = () => new Promise<void>(r => setTimeout(r, 0));
21+
22+
async function run(shape: "latest" | "superseded" | "via-memo", view: "memo" | "identity") {
23+
const asks: { v: number; resolve: (v: number) => void }[] = [];
24+
const [s, setS] = createSignal(shape === "via-memo" ? 2 : 1);
25+
const flight = (v: number) => new Promise<number>(resolve => asks.push({ v, resolve }));
26+
let m: () => number;
27+
if (shape === "via-memo") {
28+
const m0 = createMemo(() => latest(s) + 1);
29+
m = createMemo(() => flight(m0() + 1));
30+
} else m = createMemo(() => flight(latest(s) + 1));
31+
const x = view === "memo" ? m : createMemo(() => m());
32+
let leaf = "";
33+
let output = "";
34+
createRoot(() => {
35+
const b = createLoadingBoundary(
36+
() => {
37+
createRenderEffect(
38+
() => `l:${latest(x)}`,
39+
v => {
40+
leaf = v;
41+
}
42+
);
43+
return "content";
44+
},
45+
() => "fallback"
46+
);
47+
createRenderEffect(b, v => {
48+
output = v;
49+
});
50+
});
51+
const settle = (pick: number) => {
52+
const a = asks.splice(pick % asks.length, 1)[0];
53+
a.resolve(a.v);
54+
};
55+
flush();
56+
if (shape === "superseded") {
57+
// A second question while the first is in flight; its answer lands in
58+
// the tick a write asks a third.
59+
setS(2);
60+
await Promise.resolve();
61+
settle(1);
62+
setS(1);
63+
} else {
64+
await task();
65+
flush();
66+
settle(0);
67+
setS(0);
68+
}
69+
await task();
70+
flush();
71+
return output === "content" ? leaf : output;
72+
}
73+
74+
describe("an answered-then-reasked first answer reaches a reader its landing released", () => {
75+
for (const [shape, shown] of [
76+
["latest", "l:2"],
77+
["superseded", "l:3"],
78+
["via-memo", "l:4"]
79+
] as const)
80+
it(`${shape}: an identity memo shows what the memo shows`, async () => {
81+
expect(await run(shape, "memo")).toBe(shown);
82+
expect(await run(shape, "identity")).toBe(shown);
83+
});
84+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
// async-gen E2/E3 (3289 #310, 91501 #1993, 91501 #2609; D-22 amended
5+
// 2026-10-30, F-9): an async memo answered, then re-asked in one flush, with
6+
// its input read through `latest()`. The landing ran before the re-ask
7+
// reached the memo, so it released the identity memo's (and the derived
8+
// store's) wait, and the served-only-while-waiting rule left them on
9+
// `[ROOT-L]` while the plain form showed the answer.
10+
import { expect, test } from "vitest";
11+
import fc from "fast-check";
12+
import { runScenario } from "./run.js";
13+
import { scenarioArb } from "./scenario.js";
14+
15+
for (const [seed, i] of [
16+
[3289, 310],
17+
[91501, 1993],
18+
[91501, 2609]
19+
] as [number, number][])
20+
test(`${seed} #${i}: an identity memo and a derived store show what the memo shows`, async () => {
21+
const sc = fc.sample(scenarioArb(), { seed, numRuns: i + 1 })[i];
22+
const a = await runScenario(sc);
23+
for (const variant of [{ identity: true }, { store: true }]) {
24+
const b = await runScenario(sc, variant);
25+
const at = (r: typeof a) => r.screens.filter((_, j) => a.settled[j] && b.settled[j]);
26+
expect(at(b), JSON.stringify(variant)).toEqual(at(a));
27+
}
28+
});

0 commit comments

Comments
 (0)