Skip to content

Commit b0a5803

Browse files
committed
fix(signals): a memo reading through a linked re-based lane no longer loops at the seam (#4643)
A memo seated in one lane whose pass read through a lane linked to it, re-based over a parked frame's answer (`over`, #4525), read that frame's staging there (#4536). The seam's `stagedReaders` loop re-derived it on the committed world: the re-run re-entered the linked lane, read the staging again and parked again, every round (Potential Infinite Loop Detected). The #4536 carve-out in `laneSeams` checked only the memo's own lane; it now checks the lane's link group (one reveal unit). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE
1 parent 6e569c7 commit b0a5803

3 files changed

Lines changed: 240 additions & 2 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+
Fix an infinite flush loop when a memo over an optimistic store list (an "N left" count, a filter) read through a lane linked to one re-based over a parked refresh (#4643).

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,15 +1111,18 @@ const laneSeams = (t: Transaction | null): void => {
11111111
// lane holds while the frame does (`laneSeam`).
11121112
// Re-derived on the committed world, it paired the new rows with the
11131113
// old inputs, and the landing revealed that beside them (F-1, D-34 ¶3,
1114-
// #4577).
1114+
// #4577). A lane linked to such a lane counts (one reveal unit): a
1115+
// memo seated in one whose pass read through the other read the
1116+
// frame's staging there, and, re-derived, it re-entered that lane,
1117+
// read it again and parked again, every round (#4643).
11151118
if (
11161119
r._flags & REACTIVE_DISPOSED ||
11171120
(l?._lane
11181121
? (l._parent?._verdict === l &&
11191122
(r._config & CONFIG_VERDICT
11201123
? !(r._flags & REACTIVE_PROBE_UNANSWERED)
11211124
: !blocked(t!) || heldBy(t!, r))) ||
1122-
(l.over && liveTx(l.over) === t)
1125+
(l._links || [l]).some(u => u.over && liveTx(u.over) === t)
11231126
: l &&
11241127
!(
11251128
r._flags & REACTIVE_PROBE_UNANSWERED ||
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/**
2+
* #4643: an optimistic todo list with an "N left" memo never settled. A
3+
* memo seated in one lane (its guess's) whose pass read through a lane
4+
* linked to it, re-based over a parked frame's answer (#4525, `over`), read
5+
* that frame's staging there (#4536). The seam re-derived it on the
6+
* committed world (`stagedReaders`): its re-run re-entered the linked lane,
7+
* read the staging again and parked again, every round: "Potential Infinite
8+
* Loop Detected ... last staged node: computed". The seam's #4536 carve-out
9+
* looked only at the memo's own lane; the link group (one reveal unit) counts.
10+
*
11+
* Every landing settles (the Hold ends with one frame that reveals the
12+
* change; D-33: the hold commits), each frame shows the list and its count
13+
* together (D-37), and the end is the server's list.
14+
*
15+
* The issue's second shape (an inbox: a filter memo over a polled list,
16+
* optimistic deletes) looped the same way ("last staged node: signal").
17+
*/
18+
import { expect, it } from "vitest";
19+
import {
20+
action,
21+
createMemo,
22+
createOptimisticStore,
23+
createRenderEffect,
24+
createRoot,
25+
createSignal,
26+
flush,
27+
mapArray,
28+
refresh
29+
} from "../src/index.js";
30+
31+
const tick = async () => {
32+
for (let i = 0; i < 2; i++) {
33+
await new Promise(r => setTimeout(r, 0));
34+
flush();
35+
}
36+
};
37+
38+
function fakeServer<T extends { id: number }>(rows: T[]) {
39+
const server = { rows };
40+
const out: { key: string; go: () => void }[] = [];
41+
const call = (key: string, apply?: () => void) =>
42+
new Promise<any>(resolve =>
43+
out.push({
44+
key,
45+
go: () => {
46+
apply?.();
47+
resolve(key === "list" ? server.rows.map(t => ({ ...t })) : true);
48+
}
49+
})
50+
);
51+
const land = (key: string) =>
52+
out
53+
.splice(
54+
out.findIndex(f => f.key === key),
55+
1
56+
)[0]
57+
.go();
58+
return { server, out, call, land };
59+
}
60+
61+
type Todo = { id: number; done: boolean };
62+
63+
for (const withCount of [true, false])
64+
it(`#4643: toggle, toggle, remove, add, add: the toggle's save lands and settles (count=${withCount})`, async () => {
65+
const { server, out, call, land } = fakeServer<Todo>([
66+
{ id: 1, done: false },
67+
{ id: 2, done: false }
68+
]);
69+
let toggle!: (id: number) => Promise<unknown>,
70+
remove!: (id: number) => Promise<unknown>,
71+
add!: (id: number) => Promise<unknown>;
72+
let list = "",
73+
count = "";
74+
createRoot(() => {
75+
const [todos, setTodos] = createOptimisticStore<Todo[]>(() => call("list"), []);
76+
const remaining = createMemo(() => todos.filter(t => !t.done).length);
77+
toggle = action(function* (id: number) {
78+
setTodos(s => {
79+
const t = s.find(x => x.id === id);
80+
if (t) t.done = !t.done;
81+
});
82+
yield call("toggle:" + id, () => {
83+
server.rows = server.rows.map(t => (t.id === id ? { ...t, done: !t.done } : t));
84+
});
85+
refresh(todos);
86+
});
87+
remove = action(function* (id: number) {
88+
setTodos(s => s.filter(x => x.id !== id));
89+
yield call("remove:" + id, () => {
90+
server.rows = server.rows.filter(t => t.id !== id);
91+
});
92+
refresh(todos);
93+
});
94+
add = action(function* (id: number) {
95+
setTodos(s => {
96+
s.push({ id, done: false });
97+
});
98+
yield call("add:" + id, () => {
99+
server.rows = [...server.rows, { id, done: false }];
100+
});
101+
refresh(todos);
102+
});
103+
const rows = mapArray(
104+
() => todos,
105+
(t: Todo) => t
106+
);
107+
createRenderEffect(
108+
() =>
109+
rows()
110+
.map(t => `[${t.id}${t.done ? "x" : "o"}]`)
111+
.join(""),
112+
v => void (list = v)
113+
);
114+
if (withCount)
115+
createRenderEffect(
116+
() => remaining(),
117+
v => void (count = ` ${v} left`)
118+
);
119+
});
120+
const frames: string[] = [];
121+
const step = async (name: string, fn: () => void) => {
122+
fn();
123+
flush();
124+
await tick();
125+
frames.push(`${name}=${list}${count}`);
126+
};
127+
await tick();
128+
await step("list", () => land("list"));
129+
await step("t1", () => void toggle(1));
130+
await step("t2", () => void toggle(2));
131+
await step("rm2", () => void remove(2));
132+
await step("add3", () => void add(3));
133+
await step("land rm2", () => land("remove:2"));
134+
await step("add4", () => void add(4));
135+
await step("land list", () => land("list"));
136+
// Looped here: Potential Infinite Loop Detected.
137+
await step("land t1", () => land("toggle:1"));
138+
while (out.length) {
139+
const k = out[0].key;
140+
await step(`land ${k}`, () => land(k));
141+
}
142+
const c = (n: number) => (withCount ? ` ${n} left` : "");
143+
expect(frames).toEqual([
144+
`list=[1o][2o]${c(2)}`,
145+
`t1=[1x][2o]${c(1)}`,
146+
`t2=[1x][2x]${c(0)}`,
147+
`rm2=[1x]${c(0)}`,
148+
`add3=[1x][3o]${c(1)}`,
149+
`land rm2=[1x][3o]${c(1)}`,
150+
`add4=[1x][3o][4o]${c(2)}`,
151+
`land list=[1x][3o][4o]${c(2)}`,
152+
`land t1=[1x][3o][4o]${c(2)}`,
153+
`land toggle:2=[1x][3o][4o]${c(2)}`,
154+
`land add:3=[1x][3o][4o]${c(2)}`,
155+
`land add:4=[1x][3o][4o]${c(2)}`,
156+
`land list=[1x][3o][4o]${c(2)}`,
157+
`land list=[1x][3o][4o]${c(2)}`,
158+
`land list=[1x][3o][4o]${c(2)}`,
159+
`land list=[1x][3o][4o]${c(2)}`
160+
]);
161+
expect(server.rows).toEqual([
162+
{ id: 1, done: true },
163+
{ id: 3, done: false },
164+
{ id: 4, done: false }
165+
]);
166+
});
167+
168+
type Mail = { id: number; read: boolean };
169+
170+
for (const withMemo of [true, false])
171+
it(`#4643: an inbox filter over a polled list with optimistic deletes settles (memo=${withMemo})`, async () => {
172+
const { server, out, call, land } = fakeServer<Mail>([
173+
{ id: 1, read: true },
174+
{ id: 2, read: false },
175+
{ id: 3, read: true },
176+
{ id: 4, read: false }
177+
]);
178+
let del!: (id: number) => Promise<unknown>, poll!: () => void;
179+
let setFilter!: (f: "all" | "read") => void;
180+
let screen = "";
181+
createRoot(() => {
182+
const [filter, sf] = createSignal<"all" | "read">("all");
183+
setFilter = sf;
184+
const [inbox, set] = createOptimisticStore<Mail[]>(() => call("list"), [], { key: "id" });
185+
poll = () => refresh(inbox);
186+
const pick = () => inbox.filter(m => filter() === "all" || m.read);
187+
const visible = withMemo ? createMemo(pick) : pick;
188+
del = action(function* (id: number) {
189+
set(s => s.filter(x => x.id !== id));
190+
yield call("delete:" + id, () => {
191+
server.rows = server.rows.filter(t => t.id !== id);
192+
});
193+
refresh(inbox);
194+
});
195+
const rows = mapArray(visible, (m: Mail) => m);
196+
createRenderEffect(
197+
() =>
198+
`${filter()}:` +
199+
rows()
200+
.map(m => `[${m.id}]`)
201+
.join(""),
202+
v => void (screen = v)
203+
);
204+
});
205+
const step = async (fn: () => void) => {
206+
fn();
207+
flush();
208+
await tick();
209+
};
210+
await tick();
211+
await step(() => land("list"));
212+
expect(screen).toBe("all:[1][2][3][4]");
213+
await step(() => setFilter("read"));
214+
expect(screen).toBe("read:[1][3]");
215+
await step(() => void del(3));
216+
expect(screen).toBe("read:[1]");
217+
await step(() => land("delete:3"));
218+
await step(() => void del(2));
219+
await step(() => poll());
220+
await step(() => land("list"));
221+
await step(() => land("list"));
222+
// Looped here: Potential Infinite Loop Detected (last staged node: signal).
223+
await step(() => setFilter("all"));
224+
while (out.length) {
225+
const k = out[0].key;
226+
await step(() => land(k));
227+
}
228+
expect(screen).toBe("all:[1][4]");
229+
expect(server.rows.map(m => m.id)).toEqual([1, 4]);
230+
});

0 commit comments

Comments
 (0)