Skip to content

Commit c384c88

Browse files
committed
fix(signals): a latest()/isPending() reader born in a newly opened branch reads that branch's world (#4341)
A memo created in a <Match> that a staged or held condition just opened, reading latest() of an unrelated memo, threw "Attempting to access a stale value from <Match>" and halted the reactive system. Entering a verdict window made the memo's first pass a verdict reader, and a verdict reader reads the screen (`frameRead`, `stagedScreen`). `bornInto` exempts a node born into a transaction, but only once it is held, at the end of that same pass. So the pass read the committed world, where its own branch was still closed, and the <Match> accessor guard (`untrack(switchFunc)`) threw. `markVerdictReader` now walks the passes still running above the reader (its creator's, theirs). When one of them read the future or a staging, the pass does not become a verdict reader yet. It takes those flags, so `route` keeps it in its frame, and it reads that world like any derivation of it. It is a verdict reader from its next pass (REACTIVE_PROBED, as before). The walk is in verdict.ts, so apps without latest()/isPending() ship nothing new. quietPending is golfed to offset the bytes. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 17d4d1b commit c384c88

5 files changed

Lines changed: 260 additions & 9 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 memo or render effect born in a branch that a staged or held condition just opened no longer reads the screen when it calls `latest()` or `isPending()` (#4341). Its first pass runs inside the pass that opened the branch (or inside passes that branch is still creating, such as a `<Show>` or a component inside it), and the branch exists only in that world, so the pass reads that world like any derivation of it, and like `bornInto` does once the node is held. It becomes a verdict reader from its next pass. Before, the reader read the screen, where its branch was still closed: a `<Match>` callback's narrowed accessor saw its own branch as unmounted and threw "Attempting to access a stale value from <Match>", which halted the reactive system. The order of reads inside the reader and whether a memo sits between the read and the binding no longer change this. Apps that do not use `latest()` or `isPending()` ship no new bytes.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ export function bornInto(c: Computed<any>, t: Transaction): unknown {
12101210

12111211
/** The computed whose pass a creation under `owner` belongs to (a root's is
12121212
* its nearest computed ancestor's), or null outside every pass. */
1213-
function creatorPass(owner: Owner | null): Computed<any> | null {
1213+
export function creatorPass(owner: Owner | null): Computed<any> | null {
12141214
return owner !== null && (owner as Root)._root
12151215
? (owner as Root)._parentComputed
12161216
: (owner as Computed<any> | null);

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
REACTIVE_LANE_READ,
2626
REACTIVE_PROBED,
2727
REACTIVE_PROBE_UNANSWERED,
28+
REACTIVE_RECOMPUTING_DEPS,
2829
REACTIVE_STAGED_READ,
2930
STATUS_ERROR,
3031
STATUS_PENDING,
@@ -33,6 +34,7 @@ import {
3334
import {
3435
bornInto,
3536
context,
37+
creatorPass,
3638
joinPass,
3739
markLateLinker,
3840
pullComputed,
@@ -63,7 +65,7 @@ import {
6365
type Transaction,
6466
installVerdictHooks
6567
} from "./scheduler.js";
66-
import type { Computed, Root, Signal } from "./types.js";
68+
import type { Computed, Signal } from "./types.js";
6769

6870
/** A verdict reader whose answer this flush could not give yet: the node it
6971
* asked about is staged and not held — whether it will be is the seam's
@@ -186,8 +188,8 @@ function provisionalVerdict(c: Computed<any>, notFinal: boolean): void {
186188
* when every flight it waits on is. */
187189
function quietPending(el: Computed<any>): boolean {
188190
const x = el._x;
189-
if (x === null) return false;
190-
if (x._pendingSources !== undefined) {
191+
if (!x) return false;
192+
if (x._pendingSources) {
191193
for (const source of x._pendingSources) if (!source._x?._reask) return false;
192194
return true;
193195
}
@@ -235,11 +237,21 @@ function pendingVerdict(c: Computed<any> | null, el: Computed<any>): void {
235237
* a router's link claim probing inside the computation that creates the
236238
* anchor). A throw still reaches the pass through `untrack`. */
237239
function markVerdictReader(window: number): void {
238-
let c = context;
239-
if ((c as Root)?._root) c = (c as Root)._parentComputed;
240-
if (c !== null && tracking) {
241-
c._config |= CONFIG_VERDICT;
242-
(c as Computed<any>)._flags |= REACTIVE_PROBED;
240+
const c = creatorPass(context);
241+
if (c && tracking) {
242+
// A pass run inside passes still running — its creator's, theirs — one
243+
// of which read the future or a staging (a branch a held or staged
244+
// condition opened, and whatever that branch creates) has no screen: the
245+
// branch exists only in that world. It reads that world as any
246+
// derivation of it does (`bornInto` once held) and stays its pass
247+
// (`route`); a verdict reader from its next pass. #4341: a memo born in
248+
// a newly opened <Match> read the screen, where its branch was closed.
249+
let p: Computed<any> | null = c,
250+
born = 0;
251+
while ((p = creatorPass(p!._parent))?._flags! & REACTIVE_RECOMPUTING_DEPS)
252+
born |= p!._flags & (REACTIVE_JOINED | REACTIVE_STAGED_READ);
253+
if (!born) c._config |= CONFIG_VERDICT;
254+
c._flags |= REACTIVE_PROBED | born;
243255
// Observe tier: which windows this pass entered (1 `isPending`, 2
244256
// `latest`) — the hold census names the affordance (attribution.ts).
245257
if (__OBSERVE__) (c as any)._devWindows = ((c as any)._devWindows ?? 0) | window;
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// #4341: a memo born in a branch that a staged or held condition opened has
2+
// no screen of its own — the branch exists only in the world that opened it.
3+
// Its first pass reads that world like any derivation of it, whether or not
4+
// it enters a `latest()`/`isPending()` window, and in whatever order: a
5+
// verdict window must not turn the branch's own first pass into a reader of
6+
// the screen, where its condition is still closed (the <Match> accessor then
7+
// saw its branch as unmounted and threw, halting the app).
8+
import {
9+
createLoadingBoundary,
10+
createMemo,
11+
createRenderEffect,
12+
createRoot,
13+
createSignal,
14+
flush,
15+
isPending,
16+
latest,
17+
untrack,
18+
type Accessor
19+
} from "../src/index.js";
20+
21+
const settle = async () => {
22+
for (let i = 0; i < 3; i++) {
23+
await Promise.resolve();
24+
flush();
25+
}
26+
};
27+
28+
type Read = (zone: Accessor<string>, format: Accessor<string>, open: () => string) => string;
29+
30+
const reads: [string, Read][] = [
31+
["latest(format) first", (zone, format, open) => `${latest(format)}:${zone()}:${open()}`],
32+
["latest(format) between", (zone, format, open) => `${zone()}:${latest(format)}:${open()}`],
33+
["latest(zone)", (zone, format, open) => `${format()}:${latest(zone)}:${open()}`],
34+
[
35+
"isPending(format)",
36+
(zone, format, open) => `${isPending(format) ? "?" : format()}:${zone()}:${open()}`
37+
],
38+
["plain reads (control)", (zone, format, open) => `${format()}:${zone()}:${open()}`]
39+
];
40+
41+
/**
42+
* `zone` gates a branch (as `<Match when={zone() === "ready"}>`), and an
43+
* async memo under its own boundary re-asks on `zone`, so the frame that
44+
* flips it has a transaction.
45+
* - "staged": one write opens the branch; the selecting chain is deep
46+
* enough that it runs after the frame has its transaction, so the
47+
* selection reads a staging of that flush.
48+
* - "joined": the write is held first; a second, unrelated write re-runs
49+
* the selection, which reads the held condition (it joins the hold) and
50+
* opens the branch.
51+
* The reader is a memo, a render effect, or a memo a plain wrapper memo
52+
* creates (its creator is not the selecting pass itself).
53+
*/
54+
function setup(read: Read, reader: "memo" | "effect" | "nested memo", opener: "staged" | "joined") {
55+
const [ready, setReady] = createSignal(false);
56+
const [gate, setGate] = createSignal(opener === "staged");
57+
let resolve!: (v: string) => void;
58+
const screen: Record<string, unknown> = {};
59+
const seen: string[] = [];
60+
createRoot(() => {
61+
const zone = createMemo(() => (ready() ? "ready" : "loading"));
62+
const format = createMemo(() => "ru");
63+
const data = createMemo(() => {
64+
if (zone() !== "ready") return "none";
65+
return new Promise<string>(r => (resolve = r));
66+
});
67+
let selected: Accessor<boolean> = createMemo(() => zone() === "ready");
68+
for (let i = 0; i < 6; i++) {
69+
const prev = selected;
70+
selected = createMemo(() => prev());
71+
}
72+
const sel = selected;
73+
const show = (label: Accessor<string>) =>
74+
createRenderEffect(label, v => void seen.push((screen.span = v)));
75+
const branch = createMemo(() => {
76+
if (!gate() || !sel()) return "closed";
77+
return untrack(() => {
78+
// The narrowed accessor's guard: is this branch still the selected one?
79+
const open = () => (untrack(sel) ? "sel" : "stale");
80+
if (reader === "effect") show(() => read(zone, format, open));
81+
else if (reader === "memo") show(createMemo(() => read(zone, format, open)));
82+
else createMemo(() => show(createMemo(() => read(zone, format, open))));
83+
return "open";
84+
});
85+
});
86+
createRenderEffect(branch, v => void (screen.branch = v));
87+
createRenderEffect(
88+
createLoadingBoundary(
89+
() => data(),
90+
() => "wait"
91+
),
92+
v => void (screen.data = v)
93+
);
94+
});
95+
flush();
96+
return { screen, seen, setReady, setGate, resolve: (v: string) => resolve(v) };
97+
}
98+
99+
describe("a memo born in a newly opened branch reads the world that opened it (#4341)", () => {
100+
for (const opener of ["staged", "joined"] as const)
101+
for (const reader of ["memo", "effect", "nested memo"] as const)
102+
for (const [name, read] of reads)
103+
it(`${opener}, ${reader}: ${name}`, async () => {
104+
const { screen, seen, setReady, setGate, resolve } = setup(read, reader, opener);
105+
expect(screen).toEqual({ branch: "closed", data: "none" });
106+
setReady(true);
107+
flush();
108+
if (opener === "joined") {
109+
expect(screen).toEqual({ branch: "closed", data: "none" });
110+
setGate(true);
111+
flush();
112+
}
113+
// Held on `data`: the branch has not opened on screen.
114+
expect(screen.branch).toBe("closed");
115+
resolve("data");
116+
await settle();
117+
const want = name === "latest(format) between" ? "ready:ru:sel" : "ru:ready:sel";
118+
expect(screen).toEqual({ branch: "open", span: want, data: "data" });
119+
// No pass of the branch's reader saw its branch closed.
120+
expect(seen.filter(v => v !== want)).toEqual([]);
121+
});
122+
});
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
6+
// #4341: a memo born in a newly opened <Match> (or <Show>) that reads
7+
// `latest()` of an unrelated memo. The update that opens the branch also
8+
// re-asks a sibling async memo under its own <Loading>, so the frame that
9+
// mounts the branch has a transaction. The memo's first pass is the branch's
10+
// — its creator read the staged condition — so it has no screen of its own:
11+
// it must read the world that created it, or the narrowed accessor sees the
12+
// branch as closed and throws (REACTIVITY_HALTED). Reading `format()`
13+
// instead of `latest(format)` always worked; the order of reads inside the
14+
// memo, or whether the binding has a memo between, must not matter either.
15+
import { afterEach, describe, expect, test, vi } from "vitest";
16+
import {
17+
createMemo,
18+
createSignal,
19+
latest,
20+
Loading,
21+
Match,
22+
resetErrorHalt,
23+
Show,
24+
Switch,
25+
type Accessor
26+
} from "solid-js";
27+
import { render } from "@solidjs/web";
28+
29+
const wait = (ms: number) => new Promise(r => setTimeout(r, ms));
30+
31+
afterEach(() => {
32+
resetErrorHalt();
33+
vi.restoreAllMocks();
34+
});
35+
36+
type Label = (zone: Accessor<string>, format: Accessor<string>, s: Accessor<string>) => string;
37+
38+
const labels: [string, Label][] = [
39+
["latest(format) first (the issue)", (zone, format, s) => `${latest(format)}:${zone()}:${s()}`],
40+
["latest(format) between", (zone, format, s) => `${zone()}:${latest(format)}:${s()}`],
41+
["latest(zone)", (zone, format, s) => `${format()}:${latest(zone)}:${s()}`],
42+
["plain reads (control)", (zone, format, s) => `${format()}:${zone()}:${s()}`]
43+
];
44+
45+
type Reader = "memo" | "binding" | "memo in an inner <Show>";
46+
47+
async function run(kind: "switch" | "show", label: Label, reader: Reader) {
48+
const error = vi.spyOn(console, "error").mockImplementation(() => {});
49+
const el = document.createElement("div");
50+
const [ready, setReady] = createSignal(false);
51+
const zone = createMemo(() => (ready() ? "ready" : "loading"));
52+
const format = createMemo(() => "ru");
53+
function Picker(props: { value: Accessor<string> }) {
54+
if (reader === "binding") return <span>{label(zone, format, props.value)}</span>;
55+
const Text = () => {
56+
const text = createMemo(() => label(zone, format, props.value));
57+
return <span>{text()}</span>;
58+
};
59+
if (reader === "memo") return <Text />;
60+
return <Show when={format()}>{_ => <Text />}</Show>;
61+
}
62+
const dispose = render(() => {
63+
const data = createMemo(async () => {
64+
if (zone() !== "ready") return "none";
65+
await wait(30);
66+
return "data";
67+
});
68+
return (
69+
<>
70+
{kind === "switch" ? (
71+
<Switch>
72+
<Match when={zone() === "loading"}>
73+
<i>loading</i>
74+
</Match>
75+
<Match when={zone() === "ready" && "sel"}>{s => <Picker value={s} />}</Match>
76+
</Switch>
77+
) : (
78+
<Show when={zone() === "ready" && "sel"} fallback={<i>loading</i>}>
79+
{s => <Picker value={s} />}
80+
</Show>
81+
)}
82+
<Loading fallback={<b>wait</b>}>
83+
<b>{data()}</b>
84+
</Loading>
85+
</>
86+
);
87+
}, el);
88+
await wait(5);
89+
expect(el.textContent).toBe("loadingnone");
90+
setReady(true);
91+
await wait(100);
92+
const text = el.textContent;
93+
dispose();
94+
expect(error).not.toHaveBeenCalled();
95+
return text;
96+
}
97+
98+
describe("a memo born in a newly opened branch may read latest() (#4341)", () => {
99+
test("the issue's repro: <Switch>/<Match>, latest(format) in the memo", async () => {
100+
expect(await run("switch", labels[0][1], "memo")).toBe("ru:ready:seldata");
101+
});
102+
103+
for (const kind of ["switch", "show"] as const)
104+
for (const reader of ["memo", "binding", "memo in an inner <Show>"] as const)
105+
for (const [name, label] of labels)
106+
test(`${kind}, ${reader}: ${name}`, async () => {
107+
const text = await run(kind, label, reader);
108+
expect(text).toBe(
109+
name.startsWith("latest(format) between") ? "ready:ru:seldata" : "ru:ready:seldata"
110+
);
111+
});
112+
});

0 commit comments

Comments
 (0)