Skip to content

Commit c24e38e

Browse files
Brenley Dueckclaude
authored andcommitted
fix(signals): an async memo's held landing keeps the committed frame's dependencies (#3461)
`selected = createMemo(async () => (b() ? b() : a()))` with `b` held by a slow flight: selected's held pass read only `b`, and asyncWrite trimmed the dependency tail unconditionally before the write, so the `selected -> a` edge was gone even though the landed value was then staged under the hold. The committed `Selected: 0` still derived from `a`, but the mainline `a` write no longer reached selected or entered its hold through the stamp, and committed beside the stale derivation: `A: 1 | B: 0 | Selected: 0`. The landing now follows A30's gate (the arm `recompute` and `commitPendingNode` already implement for sync passes and effects): trim after the write, only when the landing published (`_pendingValue === NOT_PENDING`); a transition-held landing leaves the tail for its commit. The `a` write reaches selected, joins the hold, and the four values reveal as one frame, identical to the sync control. Spec: A30 gains the landing arm; rules index regenerated. Core floor flat (23,752); brotli core-floor cap ratcheted 8.85 -> 8.90 KB for +2 B of noise. Changeset included. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent a8a8949 commit c24e38e

7 files changed

Lines changed: 228 additions & 22 deletions

File tree

‎.changeset/async-landing-deps.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
An async memo's held landing keeps the committed frame's dependencies (#3461).
6+
7+
- `selected = createMemo(async () => (b() ? b() : a()))` with `b` held by a slow flight: selected's held pass read only `b`, and its landing trimmed `a` at once, before the write staged the landed value under the hold. A later mainline `a` write no longer reached selected, so `A: 1` committed beside `Selected: 0` while `B` still read 0. The landing now trims only when it published (A30, the landing twin of a staged sync pass); a transition-held landing leaves the tail for its commit, so the `a` write reaches selected and joins the hold, and the four values reveal as one frame.

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

Lines changed: 18 additions & 18 deletions
Large diffs are not rendered by default.

‎packages/signals/docs/SPEC-ASYNC-SEMANTICS.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,15 @@ A resting optimistic node reports pending via exactly the causes a plain async m
199199
### A30. A memo's dependencies are the committed frame's until the frame is replaced
200200

201201
**Status:** **ruled** 2026-09-13 (#3410) — maintainer ruling, Cluster 4 triage; the dependency twin of held children (#3404)
202-
**Pinned by:** `tests/held-conditional-memo.test.ts` (#3410: a memo whose held pass stopped reading an input still follows a mainline write to it); `tests/held-conditional-effect.test.ts` (#3438: an effect whose held pass stopped reading an input, its run stashed, still follows a mainline write to it)
203-
**Mechanism (index, 2026-09-14):** `recompute` trims the previous pass's dependency tail (`trimStaleDeps`) only when the pass published or changed nothing (`_pendingValue === NOT_PENDING`, no `_error`) and, for an effect, owes no run (`_modified` clear); a pass that staged its value leaves the tail linked and `commitPendingNode` trims it after a clean pass (`_error == null`); an effect pass that direct-committed and owes a run leaves it for `runEffect` to trim once the run applies (again after a clean pass). `__OBSERVE__` fan-in counting and the attribution engine's subscription diff walk the validated prefix only.
202+
**Pinned by:** `tests/held-conditional-memo.test.ts` (#3410: a memo whose held pass stopped reading an input still follows a mainline write to it); `tests/held-conditional-effect.test.ts` (#3438: an effect whose held pass stopped reading an input, its run stashed, still follows a mainline write to it); `tests/async-landing-deps-3461.test.ts` (#3461: an async memo whose held flight stopped reading an input, its landing staged, still follows a mainline write to it)
203+
**Mechanism (index, 2026-09-15):** `recompute` trims the previous pass's dependency tail (`trimStaleDeps`) only when the pass published or changed nothing (`_pendingValue === NOT_PENDING`, no `_error`) and, for an effect, owes no run (`_modified` clear); a pass that staged its value leaves the tail linked and `commitPendingNode` trims it after a clean pass (`_error == null`); an effect pass that direct-committed and owes a run leaves it for `runEffect` to trim once the run applies (again after a clean pass). `__OBSERVE__` fan-in counting and the attribution engine's subscription diff walk the validated prefix only. An async landing (`asyncWrite`) follows the same gate (#3461): it trims only when it published (`_pendingValue === NOT_PENDING` after the write, an equal-value or lane landing); a transition-held landing leaves the tail for `commitPendingNode`.
204204

205205
A pass that _staged_ its value has not replaced the committed frame, so the committed value still derives from the previous pass's dependencies and a write to one of them must reach the node — and, through the node's `_transition` stamp, join its hold — exactly as an unconditional read would. Before: `selected = fixed() ? 2 : count()` held on `fixed → true` dropped `count` at its held pass, and a mainline `count` write then published `Count: 1` beside the committed `Selected: 0` / `Fixed: false`. Decided at commit rather than at the pass because a plain flush knows nothing at recompute time: the transaction that ends up holding the pass may open later in the same flush (an async memo downstream pends and the batch is adopted). An errored pass (a throw, NotReady included, a comparator throw) keeps its full list as before, and the commit skips the trim by the same `_error`. Cost on the plain path: none — a pass that publishes trims at its tail as before; only a staged pass moves the trim to the same flush's commit.
206206

207207
An effect's frame is the run its value is applied by, not its value slot (#3438). A plain-flush pass direct-commits `_value` and enqueues the run, but the same flush can still become a hold (the async memo downstream pends, the batch is adopted) and stash that run with the transaction — so the committed frame is still what the _last_ run published, and it still derives from the previous pass's dependencies. Before: `{show() ? count() : "hidden"}` (the compiler's insert effect) held on `show → false` dropped `count` at its pass, and the mainline `count` write then published `Count: 1` beside `Panel: 0` while `Show` still read `true`. Now the pass leaves the tail while a run is owed and `runEffect` trims once it applies. The write reaches the effect; a render effect is a mainline reader of the foreign hold (it is served the committed `show`, A15's stale-reader term), so it re-derives `Panel: 1` beside `Count: 1` in the mainline frame, and the hold's reveal re-derives it to `hidden` — unlike a memo, which is served the staged value and joins the hold (A29). Both frames are coherent; the shapes differ because effects render mainline by design. A pass that changed nothing owes no run and trims at its tail as before.
208208

209+
An async memo's frame is replaced by its landing, not by the pass that registered the flight (#3461). The flight's pass throws `NotReady` and keeps its full list; the landing used to trim unconditionally, before the write, even when `setSignal` then staged the value under a live transaction. Before: `selected = async () => (b() ? b() : a())` held on `b → 1` dropped `a` at its landing, and a mainline `a` write then published `A: 1` beside `Selected: 0` while `B` still read 0. Now the landing trims only when it published; a held landing leaves the tail for its commit, so the `a` write reaches `selected` and joins its hold (its stamp), exactly as the sync memo's staged pass does. Unchanged by design: a landing equal to the committed value published nothing new and trims at once, like a sync pass that changed nothing, so `b() ? 0 : a()` still drops `a` in both shapes.
210+
209211
## Loading window and seeds
210212

211213
### A27. The commit-#0 loading window is loading-class and verdict-quiet

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,6 @@ export function handleAsync<T>(
484484
// old value as pending, a one-frame pulse to direct observers (#3178).
485485
// A truthy capture implies `_x` exists, so the restore writes it directly.
486486
const wasReask = el._x?._reask;
487-
trimStaleDeps(el);
488487
landStatus(el);
489488
if (wasReask) el._x!._reask = true;
490489
const lane = resolveLane(el as any);
@@ -590,6 +589,15 @@ export function handleAsync<T>(
590589
if (el._pendingValue === NOT_PENDING) {
591590
el._loading = false;
592591
if (wasReask) el._x!._reask = false;
592+
// The landing published: the dependency tail the flight's pass left
593+
// linked goes now (A30, #3410). A transition-held landing has not
594+
// replaced the committed frame — the committed value still derives
595+
// from the previous pass's inputs, and a mainline write to one of them
596+
// must reach this node and join its hold (its stamp) instead of
597+
// publishing beside the stale derivation (#3461: `b() ? b() : a()`
598+
// held on `b` dropped `a` at its landing, and `A: 1` then committed
599+
// beside `Selected: 0`). `commitPendingNode` trims a held landing.
600+
trimStaleDeps(el);
593601
}
594602
settlePendingSource(el);
595603
schedule();
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { describe, expect, it } from "vitest";
2+
import { createMemo, createRenderEffect, createRoot, createSignal, flush } from "../src/index.js";
3+
4+
// Manual clock (see async-chain-supersession.test.ts).
5+
let now = 0;
6+
let timers: { at: number; run: () => void }[] = [];
7+
function delay<T>(ms: number, value?: T): Promise<T> {
8+
return new Promise<T>(r => timers.push({ at: now + ms, run: () => r(value as T) }));
9+
}
10+
async function settle() {
11+
for (let r = 0; r < 3; r++) {
12+
for (let i = 0; i < 10; i++) await Promise.resolve();
13+
flush();
14+
}
15+
}
16+
async function advanceTo(t: number) {
17+
while (true) {
18+
timers.sort((a, b) => a.at - b.at);
19+
const next = timers[0];
20+
if (!next || next.at > t) break;
21+
timers.shift();
22+
now = next.at;
23+
next.run();
24+
await settle();
25+
}
26+
now = t;
27+
await settle();
28+
}
29+
function reset() {
30+
now = 0;
31+
timers = [];
32+
}
33+
function frames(log: string[], when: number[]): string[] {
34+
const byTime = new Map<number, string[]>();
35+
log.forEach((v, i) => (byTime.get(when[i]) ?? byTime.set(when[i], []).get(when[i])!).push(v));
36+
return [...byTime].map(([t, vs]) => `${t}: ${vs.sort().join(" | ")}`);
37+
}
38+
function text(fn: () => string, log: string[], when: number[]) {
39+
let last: string | undefined;
40+
createRenderEffect(fn, v => {
41+
if (v !== last) {
42+
last = v;
43+
log.push(v);
44+
when.push(now);
45+
}
46+
});
47+
}
48+
49+
// #3461: `selected = async () => (b() ? b() : a())`. `setB(1)` is held by `slow`;
50+
// selected's held pass reads only `b` and its flight lands into the hold. The
51+
// landing used to trim `a` at once, so the later mainline `setA(1)` never
52+
// reached selected: `A: 1` committed beside `Selected: 0` while `B` still read
53+
// 0. A30: the landed value is staged, the committed 0 still derives from `a`.
54+
async function scenario(asyncSelected: boolean) {
55+
reset();
56+
const log: string[] = [];
57+
const when: number[] = [];
58+
let setA!: (v: number) => void;
59+
let setB!: (v: number) => void;
60+
createRoot(() => {
61+
const [a, sA] = createSignal(0);
62+
const [b, sB] = createSignal(0);
63+
setA = sA;
64+
setB = sB;
65+
const slow = createMemo(() => delay(2000, b()));
66+
const selected = asyncSelected
67+
? createMemo(async () => (b() ? b() : a()))
68+
: createMemo(() => (b() ? b() : a()));
69+
text(() => `A: ${a()}`, log, when);
70+
text(() => `B: ${b()}`, log, when);
71+
text(() => `Slow: ${slow()}`, log, when);
72+
text(() => `Selected: ${selected()}`, log, when);
73+
});
74+
flush();
75+
await settle();
76+
await advanceTo(3000);
77+
setB(1);
78+
await settle();
79+
await advanceTo(3500);
80+
setA(1);
81+
await settle();
82+
await advanceTo(6000);
83+
return frames(log, when);
84+
}
85+
86+
describe("async memo landing keeps the committed frame's dependencies", () => {
87+
it("A30 / #3461 a held async landing does not trim the input its committed value derives from", async () => {
88+
// The `a` write (3500) reaches selected through the dependency its
89+
// committed value still has, and joins the hold through selected's
90+
// stamp: one frame when slow lands, never `A: 1` beside `Selected: 0`.
91+
// (The async memo's first landing is held with the mount's slow flight
92+
// and reveals with it at 2000; the sync control publishes it at 0.)
93+
expect(await scenario(true)).toEqual([
94+
"0: A: 0 | B: 0",
95+
"2000: Selected: 0 | Slow: 0",
96+
"5000: A: 1 | B: 1 | Selected: 1 | Slow: 1"
97+
]);
98+
});
99+
100+
it("A30 / #3410 sync control: the same conditional as a plain memo", async () => {
101+
expect(await scenario(false)).toEqual([
102+
"0: A: 0 | B: 0 | Selected: 0",
103+
"2000: Slow: 0",
104+
"5000: A: 1 | B: 1 | Selected: 1 | Slow: 1"
105+
]);
106+
});
107+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
6+
import { describe, expect, test } from "vitest";
7+
import { createMemo, createSignal, flush } from "solid-js";
8+
import { render } from "@solidjs/web";
9+
10+
const delay = <T = void,>(ms: number, value?: T) =>
11+
new Promise<T>(r => setTimeout(r, ms, value as T));
12+
13+
function snapshot(div: HTMLElement) {
14+
return Array.from(div.querySelectorAll("p"))
15+
.map(p => p.textContent)
16+
.join(" | ");
17+
}
18+
19+
// Exact port of the issue's playground (https://s.olid.uk/id/XOQbd6CFTfy2QhZK4EiQ7g),
20+
// timings scaled 2000/500 -> 200/50.
21+
function App(props: { asyncSelected: boolean }) {
22+
const [a, setA] = createSignal(0);
23+
const [b, setB] = createSignal(0);
24+
const slow = createMemo(() => delay(200, b()));
25+
const selected = props.asyncSelected
26+
? createMemo(async () => (b() ? b() : a()))
27+
: createMemo(() => (b() ? b() : a()));
28+
return (
29+
<>
30+
<button
31+
onClick={async () => {
32+
setB(1);
33+
await delay(50);
34+
setA(1);
35+
}}
36+
>
37+
Run
38+
</button>
39+
<p>A: {a()}</p>
40+
<p>B: {b()}</p>
41+
<p>Slow: {slow()}</p>
42+
<p>Selected: {selected()}</p>
43+
</>
44+
);
45+
}
46+
47+
async function run(asyncSelected: boolean) {
48+
const div = document.createElement("div");
49+
document.body.appendChild(div);
50+
const dispose = render(() => <App asyncSelected={asyncSelected} />, div);
51+
await delay(250);
52+
flush();
53+
expect(snapshot(div)).toBe("A: 0 | B: 0 | Slow: 0 | Selected: 0");
54+
55+
div.querySelector("button")!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
56+
const log: string[] = [];
57+
for (let t = 0; t <= 300; t += 20) {
58+
flush();
59+
log.push(`t=${t}: ${snapshot(div)}`);
60+
await delay(20);
61+
}
62+
dispose();
63+
div.remove();
64+
65+
expect(log.at(-1)).toBe("t=300: A: 1 | B: 1 | Slow: 1 | Selected: 1");
66+
// While B still reads 0, Selected (b() ? b() : a()) must agree with A: the
67+
// `a` write joins the hold rather than publishing beside the stale derivation.
68+
for (const line of log) {
69+
const m = /A: (\d) \| B: 0 \| Slow: 0 \| Selected: (\d)/.exec(line);
70+
if (m) expect(m[2], line).toBe(m[1]);
71+
}
72+
}
73+
74+
describe("async conditional memo across a held branch change (#3461)", () => {
75+
test("A30 / #3461 `createMemo(async () => b() ? b() : a())`", () => run(true));
76+
test("plain createMemo conditional (control)", () => run(false));
77+
});

‎scripts/size/.size-limit.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ module.exports = [
237237
// mainline). +316 B minified in the in-package floor (23,365 -> 23,681).
238238
// Rebased over #3443/#3444 (2026-09-15): measured at 8820 B against
239239
// `next`'s 8708 (+112); 23,419 -> 23,753 minified.
240-
limit: "8.85 KB",
240+
// Async landing keeps the committed frame's deps (A30 landing arm,
241+
// #3461, 2026-09-15): 8.85 -> 8.90 KB, measured at 8851 B against
242+
// `next`'s 8849 (+2, brotli noise for a moved call: asyncWrite's
243+
// trimStaleDeps now runs after the write, only when the landing
244+
// published; 0 B minified in the in-package floor, 23,752 flat).
245+
limit: "8.90 KB",
241246
modifyEsbuildConfig
242247
},
243248
{

0 commit comments

Comments
 (0)