Skip to content

Commit 9d20d99

Browse files
committed
fix(signals): refresh(memo) inside an action with an isPending reader settles (#4349)
An action's transaction lands beneath its own refresh(): nothing on screen observed the re-ask's flight yet, so the landing commits the node in flight (CONFIG_INPUTS_PUBLISHED). A verdict reader then probes the flight before anything joined (a seam watcher) and observes it with its plain read: it holds a frame of its own, as that frame's pending observer or its staged derivation (the JSX ternary's condition memo). The lane seam re-derived the watcher on the committed world every round; the re-run re-entered the same transaction and parked it again until the dev guard threw "Potential Infinite Loop Detected". A reader a non-lane transaction holds that read none of its stagings (neither REACTIVE_STAGED_READ nor REACTIVE_PROBE_UNANSWERED) has nothing to re-derive: the seam leaves it to run at the landing. The same loop hit `[isPending(x), x()]` in one render effect after an action's write. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 56a982c commit 9d20d99

5 files changed

Lines changed: 340 additions & 7 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
`refresh(memo)` inside an action no longer loops forever when a render
6+
effect reads `isPending(memo)` (#4349). The action's transaction landed
7+
beneath the re-ask's flight, so the reader observed the flight and held a
8+
frame of its own; the lane seam then re-derived that reader on the committed
9+
world every round, and each re-run re-entered the same transaction and parked
10+
it again until the dev guard threw "Potential Infinite Loop Detected". A
11+
reader a parked transaction holds that read none of its stagings now runs at
12+
the landing instead. The same loop hit `[isPending(x), x()]` in one render
13+
effect after an action's write to `x`'s input. A bare refresh stays quiet
14+
(`v41`, then `v42`); `affects()` + `refresh()` shows `pending`, then `v42`.

‎packages/signals/docs/RULES-INDEX.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
2929
| INV | 11 | 4 | 7 | 4 |
3030
| RUL | 13 | 3 | 6 | 6 |
3131
| R (CS) | 59 | 15 | 16 | 32 |
32-
| R (OL) | 37 | 1 | 0 | 36 |
32+
| R (OL) | 37 | 1 | 1 | 35 |
3333
| R (OS) | 46 | 0 | 0 | 46 |
3434
| R (PJ) | 37 | 2 | 5 | 32 |
3535
| R (RS) | 38 | 0 | 2 | 36 |
@@ -215,7 +215,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
215215
| OL-R9 | live | `docs/rules-mining/optimistic-lanes.md:44` | — | — | Regular signals written in the same action are held (transition semantics) while optimistic writes display immediately; downstream memos and chained optimistic computeds see optimistic values and reve… |
216216
| OL-R10 | live | `docs/rules-mining/optimistic-lanes.md:48` | — | — | `refresh()` of an optimistic accessor inside an action clears the override when the refetch settles; calling `refresh()` while the upstream source is still pending must not throw. |
217217
| OL-R11 | live | `docs/rules-mining/optimistic-lanes.md:52` | — | — | Verdict channels: an optimistic override **is the value** on every channel — plain read and `latest()` both return it (including literal `undefined`); the override itself is **verdict-inert** — it nev… |
218-
| OL-R12 | live | `docs/rules-mining/optimistic-lanes.md:56` | — | — | A bare `refresh()` is a quiet re-ask — never pending; a **declared** reload (`affects(x)` + `refresh(x)` inside an action) pends the slot for the whole reload window, even when the sole consumer is a … |
218+
| OL-R12 | live | `docs/rules-mining/optimistic-lanes.md:56` | — | action-refresh-ispending-loop-4349.test.ts×1 | A bare `refresh()` is a quiet re-ask — never pending; a **declared** reload (`affects(x)` + `refresh(x)` inside an action) pends the slot for the whole reload window, even when the sole consumer is a … |
219219
| OL-R13 | live | `docs/rules-mining/optimistic-lanes.md:60` | — | — | During the pending window, a source recompute that reveals a value **different** from the current override corrects the override in place (before the action settles), triggering downstream refetch; a … |
220220
| OL-R14 | live | `docs/rules-mining/optimistic-lanes.md:67` | — | — | Independent optimistic writes to unrelated signals form independent lanes: notifications scoped to each signal's own subscribers; each action's overrides revert when _that_ action settles, regardless … |
221221
| OL-R15 | live | `docs/rules-mining/optimistic-lanes.md:71` | — | — | A shared subscriber reading multiple optimistic sources merges lanes **for scheduling only**; it must not transfer transaction ownership of overrides. Disjoint-key work settles with its owning action … |

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -846,14 +846,21 @@ const laneSeams = (leaks: Computed<any>[] | null): void => {
846846
// lane answered for itself: it re-derives at the holder's landing, not
847847
// now (one run — #3322, #3540). Other verdict-lane work re-derives only
848848
// if the frame stays parked: a transaction landing at this seam makes
849-
// what it read the screen (one run).
849+
// what it read the screen (one run). A reader a transaction holds that
850+
// read none of its stagings (a verdict reader that probed only a
851+
// flight, held as the frame's pending observer or its staged
852+
// derivation) has nothing to re-derive: its re-run re-enters that
853+
// transaction and parks it again, every round — it runs at the
854+
// landing (#4349).
850855
if (
851856
r._flags & REACTIVE_DISPOSED ||
852857
(l != null &&
853-
l._parent?._verdict === l &&
854-
(r._config & CONFIG_VERDICT
855-
? !(r._flags & REACTIVE_PROBE_UNANSWERED)
856-
: !blocked(l._parent)))
858+
(l._lane
859+
? l._parent?._verdict === l &&
860+
(r._config & CONFIG_VERDICT
861+
? !(r._flags & REACTIVE_PROBE_UNANSWERED)
862+
: !blocked(l._parent))
863+
: !(r._flags & (REACTIVE_STAGED_READ | REACTIVE_PROBE_UNANSWERED))))
857864
)
858865
continue;
859866
// A leaked effect re-derives seated in its lane from its first read: it
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
// #4349: an action's transaction lands beneath its own `refresh()`: nothing
2+
// on screen observed the re-ask's flight yet, so the landing commits the node
3+
// in flight (CONFIG_INPUTS_PUBLISHED, #3305). A render effect reading
4+
// `isPending(likes)` and `likes()` then probes the flight before anything
5+
// joined (a seam watcher, `watchVerdict`) and observes it with the plain
6+
// read: it goes pending and opens a frame of its own, which parks on it. The
7+
// lane seam re-derived the watcher "on the committed world" — and the re-run
8+
// re-entered the same transaction and parked it again, every round, until
9+
// the dev guard: "Potential Infinite Loop Detected … last staged node:
10+
// effect". A reader the parked transaction holds that read none of its
11+
// stagings has nothing to re-derive: it runs at the landing.
12+
//
13+
// Frames (F-9; optimistic-lanes R12): a bare refresh is a quiet re-ask —
14+
// `v41` stays until `v42` lands; a declared reload (`affects`) shows
15+
// `pending`, then `v42`.
16+
import { describe, expect, it } from "vitest";
17+
import {
18+
action,
19+
affects,
20+
createMemo,
21+
createRenderEffect,
22+
createRoot,
23+
createSignal,
24+
flush,
25+
isPending,
26+
refresh
27+
} from "../src/index.js";
28+
29+
const tick = async () => {
30+
for (let i = 0; i < 8; i++) await Promise.resolve();
31+
flush();
32+
};
33+
34+
type Shape = {
35+
affects?: boolean;
36+
/** "refresh-yield": refresh, then a pending step; "yield-refresh": the
37+
* Like button's save, then the refresh; "refresh": refresh only. */
38+
order: "refresh" | "refresh-yield" | "yield-refresh";
39+
/** The JSX shape: the ternary's condition is its own memo. */
40+
conditionMemo?: boolean;
41+
outside?: boolean;
42+
};
43+
44+
async function likeButton(shape: Shape) {
45+
const errors: unknown[] = [];
46+
const onError = (e: unknown) => errors.push(e);
47+
process.on("unhandledRejection", onError);
48+
process.on("uncaughtException", onError);
49+
let server = 41;
50+
const fetches: (() => void)[] = [];
51+
let saved: (() => void) | undefined;
52+
const frames: string[] = [];
53+
let likes!: () => number;
54+
const dispose = createRoot(dispose => {
55+
likes = createMemo(async () => {
56+
const v = server;
57+
await new Promise<void>(r => fetches.push(r));
58+
return v;
59+
});
60+
const pending = shape.conditionMemo
61+
? createMemo(() => isPending(likes))
62+
: () => isPending(likes);
63+
createRenderEffect(
64+
() => (pending() ? "pending" : `v${likes()}`),
65+
v => {
66+
if (frames.at(-1) !== v) frames.push(v);
67+
}
68+
);
69+
return dispose;
70+
});
71+
const steps: string[] = [];
72+
const step = async () => {
73+
await tick();
74+
steps.push(frames.at(-1)!);
75+
};
76+
try {
77+
flush();
78+
fetches.shift()!();
79+
await step();
80+
const reask = () => {
81+
server++;
82+
refresh(likes);
83+
};
84+
if (shape.outside) {
85+
if (shape.affects) affects(likes);
86+
reask();
87+
} else
88+
action(function* () {
89+
if (shape.affects) affects(likes);
90+
if (shape.order === "yield-refresh") {
91+
yield new Promise<void>(r => (saved = r));
92+
reask();
93+
} else {
94+
reask();
95+
if (shape.order === "refresh-yield") yield new Promise<void>(r => (saved = r));
96+
}
97+
})();
98+
await step();
99+
if (saved) {
100+
saved();
101+
await step();
102+
}
103+
expect(fetches.length).toBe(1);
104+
fetches.shift()!();
105+
await step();
106+
return { frames, steps, errors };
107+
} finally {
108+
dispose();
109+
process.off("unhandledRejection", onError);
110+
process.off("uncaughtException", onError);
111+
}
112+
}
113+
114+
describe("refresh(memo) inside an action with an isPending reader settles (#4349)", () => {
115+
for (const conditionMemo of [false, true]) {
116+
const via = conditionMemo ? " (condition memo)" : "";
117+
it(`the issue's shape: a bare refresh is quiet${via}`, async () => {
118+
const r = await likeButton({ order: "refresh", conditionMemo });
119+
expect(r.errors).toEqual([]);
120+
expect(r.steps).toEqual(["v41", "v41", "v42"]);
121+
expect(r.frames).toEqual(["v41", "v42"]);
122+
});
123+
124+
it(`a bare refresh, then a yield${via}`, async () => {
125+
const r = await likeButton({ order: "refresh-yield", conditionMemo });
126+
expect(r.errors).toEqual([]);
127+
expect(r.steps).toEqual(["v41", "v41", "v41", "v42"]);
128+
expect(r.frames).toEqual(["v41", "v42"]);
129+
});
130+
131+
it(`the Like button: affects, yield, refresh — pending, then v42${via}`, async () => {
132+
const r = await likeButton({ affects: true, order: "yield-refresh", conditionMemo });
133+
expect(r.errors).toEqual([]);
134+
expect(r.steps).toEqual(["v41", "pending", "pending", "v42"]);
135+
expect(r.frames).toEqual(["v41", "pending", "v42"]);
136+
});
137+
138+
it(`affects, refresh, yield — pending, then v42${via}`, async () => {
139+
const r = await likeButton({ affects: true, order: "refresh-yield", conditionMemo });
140+
expect(r.errors).toEqual([]);
141+
expect(r.steps).toEqual(["v41", "pending", "pending", "v42"]);
142+
expect(r.frames).toEqual(["v41", "pending", "v42"]);
143+
});
144+
145+
it(`affects and refresh with no step settles${via}`, async () => {
146+
// The mark is the transaction's, released at its landing in the same
147+
// flush (affects.ts); whether that flush's `pending` frame shows is the
148+
// mark's lifetime, not this loop.
149+
const r = await likeButton({ affects: true, order: "refresh", conditionMemo });
150+
expect(r.errors).toEqual([]);
151+
expect(r.steps.at(-1)).toBe("v42");
152+
expect(r.frames.at(-1)).toBe("v42");
153+
});
154+
}
155+
156+
it("the same refresh outside an action (control)", async () => {
157+
const r = await likeButton({ order: "refresh", outside: true });
158+
expect(r.errors).toEqual([]);
159+
expect(r.frames).toEqual(["v41", "v42"]);
160+
});
161+
162+
it("`[isPending(x), x()]` after an action's write holds like `x()` alone (D-23), once", async () => {
163+
// The same loop without a refresh: the action's write lands beneath its
164+
// memo's new question, and the render effect that probes and reads it
165+
// parked the frame it opened every round.
166+
const fetches: (() => void)[] = [];
167+
const frames: string[] = [];
168+
let set!: (v: number) => void;
169+
const dispose = createRoot(dispose => {
170+
const [s, setS] = createSignal(41);
171+
set = setS;
172+
const x = createMemo(async () => {
173+
const v = s();
174+
await new Promise<void>(r => fetches.push(r));
175+
return v;
176+
});
177+
createRenderEffect(
178+
() => `${isPending(x)}:${x()}`,
179+
v => {
180+
frames.push(v);
181+
}
182+
);
183+
return dispose;
184+
});
185+
try {
186+
flush();
187+
fetches.shift()!();
188+
await tick();
189+
expect(frames).toEqual(["false:41"]);
190+
action(function* () {
191+
set(42);
192+
})();
193+
expect(() => flush()).not.toThrow();
194+
await tick();
195+
expect(frames).toEqual(["false:41"]);
196+
fetches.shift()!();
197+
await tick();
198+
expect(frames).toEqual(["false:41", "false:42"]);
199+
} finally {
200+
dispose();
201+
}
202+
});
203+
});
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
// #4349: the Like button — `isPending(likes) ? … : likes()` under a
6+
// <Loading>, an action that refreshes `likes`. The action's transaction
7+
// lands beneath the re-ask's flight; the button's text then observes the
8+
// flight and holds a frame of its own, with the ternary's condition memo
9+
// held in it. That memo probed only the flight, so it re-derives at the
10+
// landing, not every round (the dev guard's "Potential Infinite Loop … last
11+
// staged node: button.children"). A bare refresh is a quiet re-ask (F-9):
12+
// the button keeps `v41` until `v42` lands. `affects(likes)` declares the
13+
// reload: `pending`, then `v42`.
14+
import { expect, test } from "vitest";
15+
import { Loading, action, affects, createMemo, flush, isPending, refresh } from "solid-js";
16+
import { render } from "../src/index.js";
17+
18+
const tick = async () => {
19+
for (let i = 0; i < 8; i++) await Promise.resolve();
20+
flush();
21+
};
22+
23+
type Shape = { affects: boolean; order: "yield-refresh" | "refresh-yield" | "refresh" };
24+
const shapes: Shape[] = [];
25+
for (const a of [true, false])
26+
for (const order of ["yield-refresh", "refresh-yield", "refresh"] as const)
27+
shapes.push({ affects: a, order });
28+
29+
for (const shape of shapes)
30+
test(`Like button settles: ${shape.affects ? "affects + " : ""}${shape.order}`, async () => {
31+
const errors: unknown[] = [];
32+
const onError = (e: unknown) => errors.push(e);
33+
process.on("unhandledRejection", onError);
34+
try {
35+
let server = 41;
36+
const fetches: (() => void)[] = [];
37+
let saved!: () => void;
38+
const container = document.body.appendChild(document.createElement("div"));
39+
let clickLike!: () => void;
40+
const frames: string[] = [];
41+
const observer = new MutationObserver(() => {
42+
const text = container.textContent!;
43+
if (frames.at(-1) !== text) frames.push(text);
44+
});
45+
observer.observe(container, { subtree: true, childList: true, characterData: true });
46+
const LikeButton = () => {
47+
const likes = createMemo(async () => {
48+
const v = server;
49+
await new Promise<void>(r => fetches.push(r));
50+
return v;
51+
});
52+
const like = action(function* () {
53+
if (shape.affects) affects(likes);
54+
if (shape.order === "yield-refresh") {
55+
yield new Promise<void>(r => (saved = r));
56+
server++;
57+
refresh(likes);
58+
} else {
59+
server++;
60+
refresh(likes);
61+
if (shape.order === "refresh-yield") yield new Promise<void>(r => (saved = r));
62+
}
63+
});
64+
clickLike = () => like();
65+
return (
66+
<button onClick={() => like()}>{isPending(likes) ? "pending" : `v${likes()}`}</button>
67+
);
68+
};
69+
render(
70+
() => (
71+
<Loading fallback={<i>loading</i>}>
72+
<LikeButton />
73+
</Loading>
74+
),
75+
container
76+
);
77+
await tick();
78+
expect(container.textContent).toBe("loading");
79+
fetches.shift()!();
80+
await tick();
81+
expect(container.textContent).toBe("v41");
82+
83+
clickLike();
84+
await tick();
85+
const during = shape.affects ? "pending" : "v41";
86+
expect(container.textContent).toBe(during);
87+
if (shape.order !== "refresh") {
88+
saved();
89+
await tick();
90+
expect(container.textContent).toBe(during);
91+
}
92+
expect(fetches.length).toBe(1);
93+
fetches.shift()!();
94+
await tick();
95+
expect(container.textContent).toBe("v42");
96+
await tick();
97+
observer.disconnect();
98+
container.remove();
99+
expect(frames).toEqual([
100+
"loading",
101+
"v41",
102+
...(during === "pending" ? ["pending"] : []),
103+
"v42"
104+
]);
105+
expect(errors).toEqual([]);
106+
} finally {
107+
process.off("unhandledRejection", onError);
108+
}
109+
});

0 commit comments

Comments
 (0)