Skip to content

Commit 203cbfb

Browse files
authored
Merge pull request #3492 from solidjs/test/direct-commit-readers-posture
test(signals): pin the direct-commit readers by posture over a held frame (#3482)
2 parents c54e4c0 + db1c173 commit 203cbfb

2 files changed

Lines changed: 222 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul
7373
| A26 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:67` | scheduler.ts×1 | action-await-contract.test.ts×2 visibility-oracle-store.states.ts×1 visibility-oracle.states.ts×1 visibility-oracle.test.ts×1 | [ruled 2026-07-17] An ambient transaction window is one flush; parking is flush-driven — (**ruled 2026-07-17**, #2913; **enforcement hardened 2026-08-31**, #3141 — parking is flush-driven, and a trans… |
7474
| A27 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:233` | — | loading-value.test.ts×2 visibility-oracle.states.ts×18 visibility-oracle.test.ts×1 | [ruled 2026-08-10] The commit-#0 loading window is loading-class and verdict-quiet — (**ruled 2026-08-10**) **The commit-#0 loading window is loading-class and verdict-quiet.** A node born committed v… |
7575
| A28 | ruled, mechanism landed | `docs/SPEC-ASYNC-SEMANTICS.md:51` | constants.ts×1 core.ts×18 optimistic.ts×1 scheduler.ts×3 types.ts×1 verdict.ts×6 optimistic.ts×3 store.ts×1 | createOptimistic.test.ts×5 latest-held-till-flush.test.ts×1 optimistic-store-layer-scope.test.ts×1 posture-store-parity.test.ts×3 question-scoped-pending.test.ts×3 snapshot-derived-store-rows.test.ts×1 createOptimisticStore.test.ts×10 shallow.test.ts×1 treeshake.test.ts×2 visibility-oracle-store.states.ts×8 visibility-oracle.states.ts×8 | [ruled, mechanism landed 2026-09-15] A write becomes visible at flush — to every channel — (**ruled 2026-09-08**; supersedes the #2922 mid-tick pull) \*\*A write becomes visible at flush — to every chan… |
76-
| A29 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:75` | core.ts×5 effect.ts×1 optimistic.ts×1 | body-end-supersession-visibility.test.ts×1 born-held.test.ts×3 held-conditional-memo.test.ts×1 held-frame-dependencies.test.ts×2 latest-held-till-flush.test.ts×2 posture-store-parity.test.ts×1 treeshake.test.ts×1 visibility-oracle-store.states.ts×3 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×5 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-09-13 (#3408)] A tracked read served a live transaction's staged value enters that transaction — A tracked computation served a node's staged `_pendingValue` — a value a … |
76+
| A29 | amended | `docs/SPEC-ASYNC-SEMANTICS.md:75` | core.ts×5 effect.ts×1 optimistic.ts×1 | body-end-supersession-visibility.test.ts×1 born-held.test.ts×3 direct-commit-readers-posture.test.ts×1 held-conditional-memo.test.ts×1 held-frame-dependencies.test.ts×2 latest-held-till-flush.test.ts×2 posture-store-parity.test.ts×1 treeshake.test.ts×1 visibility-oracle-store.states.ts×3 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×5 visibility-oracle.test.ts×1 | [ruled, amended in place 2026-09-13 (#3408)] A tracked read served a live transaction's staged value enters that transaction — A tracked computation served a node's staged `_pendingValue` — a value a … |
7777
| A30 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:207` | async.ts×1 attribution.ts×1 core.ts×1 effect.ts×1 scheduler.ts×3 | async-landing-deps-3461.test.ts×3 held-conditional-effect.test.ts×1 held-conditional-memo.test.ts×1 held-frame-dependencies.test.ts×2 treeshake.test.ts×1 | [ruled 2026-09-13 (#3410)] A memo's dependencies are the committed frame's until the frame is replaced — A pass that _staged_ its value has not replaced the committed frame, so the committed value sti… |
7878
| A31 | live | `docs/SPEC-ASYNC-SEMANTICS.md:83` | core.ts×2 | ispending-combined-atomic-3442.test.ts×1 | [live 2026-09-14 (#3442)] A memo computes under its own lane posture, never its puller's — A memo's value is one shared slot every reader sees, so its pass runs under the lane posture the memo itself … |
7979
| A32 | ruled | `docs/SPEC-ASYNC-SEMANTICS.md:91` | — | visibility-oracle-store.states.ts×5 visibility-oracle-store.test.ts×1 visibility-oracle.states.ts×9 visibility-oracle.test.ts×1 | [ruled 2026-09-14] Children-forbidden readers see the frame, not the graph — `createTrackedEffect` and `onSettled` callbacks are effect-phase code that runs after the frame is decided. They read the f… |
Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/**
2+
* The promise-delivery readers — until(), resolve() (and awaitable refresh()'s
3+
* waiter; CONFIG_DIRECT_COMMIT) — by posture, over a frame HELD under an
4+
* action's optimism (#3482).
5+
*
6+
* The shape: an optimistic row on the store a live stream feeds, confirmed by
7+
* the server's echo. The server broadcasts before it answers the mutation, so
8+
* the confirming frame lands while the action is in flight and is staged under
9+
* its hold — the UI keeps showing the optimistic row until the action settles.
10+
*
11+
* Two postures for a reader created after that frame is held:
12+
*
13+
* - INSIDE the action, after a bare `yield` re-entered the transaction: the
14+
* reader is the action's own. `recompute`'s DIRECT_COMMIT arm delivers its
15+
* result on a microtask under the action's own hold — the predicate sees the
16+
* held frame, the action settles, the commit reveals frame + overlay revert.
17+
* This is the documented form (`action()`: code between an `await` and the
18+
* next `yield` runs outside the transaction; the expression of
19+
* `yield until(...)` is evaluated BEFORE that yield re-enters).
20+
*
21+
* - MAINLINE — outside the action, or inside it from an `await` continuation
22+
* with no bare `yield` first: the reader is not the action's. It is born
23+
* held (A29) and replays at the commit: it serves the COMMITTED view, never
24+
* the foreign hold's unrevealed frame. From outside the action that is the
25+
* only correct answer — `resolve()` is routinely called outside actions, and
26+
* an in-flight action's speculative frame must not leak to it. From inside
27+
* the action without the bare `yield` it is the deadlock #3482 reported:
28+
* the commit the reader waits for is the settle its own promise holds open.
29+
* Same mechanism, one posture — which is why an exemption for the
30+
* direct-commit readers in `enterStagedRead` (proposed in #3482, declined in
31+
* #3490) is wrong: it cannot tell the two apart and serves the leak to fix
32+
* the deadlock.
33+
*
34+
* Reproduction and analysis by @brenelz (#3482); docs fixed in #3491.
35+
*/
36+
import { expect, test } from "vitest";
37+
import {
38+
action,
39+
createOptimisticStore,
40+
createRenderEffect,
41+
createRoot,
42+
flush,
43+
resolve,
44+
TimeoutError,
45+
until
46+
} from "../src/index.js";
47+
48+
type Row = { id: string; status: "pending" | "confirmed" };
49+
type Snapshot = { rows: Row[] };
50+
51+
const settle = async (n = 3) => {
52+
for (let i = 0; i < n; i++) {
53+
await new Promise(r => setTimeout(r, 0));
54+
flush();
55+
}
56+
};
57+
58+
// A manually pumped AsyncIterable — what a live() server-function stream
59+
// materializes as on the client.
60+
function stream<T>() {
61+
const buffered: IteratorResult<T>[] = [];
62+
let waiter: ((r: IteratorResult<T>) => void) | null = null;
63+
const iterable: AsyncIterable<T> = {
64+
[Symbol.asyncIterator]: () => ({
65+
next: () =>
66+
new Promise<IteratorResult<T>>(res => {
67+
if (buffered.length) res(buffered.shift()!);
68+
else waiter = res;
69+
}),
70+
return: () => Promise.resolve({ done: true as const, value: undefined })
71+
})
72+
};
73+
return {
74+
iterable,
75+
push(value: T) {
76+
const r = { done: false as const, value };
77+
if (waiter) {
78+
const w = waiter;
79+
waiter = null;
80+
w(r);
81+
} else buffered.push(r);
82+
}
83+
};
84+
}
85+
86+
const CONFIRMED: Snapshot = { rows: [{ id: "res_1", status: "confirmed" }] };
87+
88+
/** Optimistic row + live feed, the action's body supplied by the test. */
89+
function harness(body: (ctx: { store: Snapshot; mutation: Promise<Row> }) => AsyncGenerator) {
90+
const feed = stream<Snapshot>();
91+
let answer!: (row: Row) => void;
92+
const mutation = new Promise<Row>(res => (answer = res));
93+
const views: string[] = [];
94+
let run!: () => Promise<unknown>;
95+
let store!: Snapshot;
96+
97+
createRoot(() => {
98+
const [s, setStore] = createOptimisticStore<Snapshot>(
99+
() => feed.iterable,
100+
{ rows: [] },
101+
{
102+
key: "id"
103+
}
104+
);
105+
store = s;
106+
run = action(async function* () {
107+
setStore(d => {
108+
d.rows.push({ id: "temp", status: "pending" });
109+
});
110+
yield* body({ store: s, mutation });
111+
});
112+
createRenderEffect(
113+
() => s.rows.map(r => `${r.id}:${r.status}`).join(",") || "empty",
114+
v => {
115+
views.push(v);
116+
}
117+
);
118+
});
119+
120+
return {
121+
feed,
122+
answer: () => answer({ id: "res_1", status: "confirmed" }),
123+
views,
124+
run,
125+
get store() {
126+
return store;
127+
}
128+
};
129+
}
130+
131+
test("inside the action after a bare yield: until() sees the frame held under its own action", async () => {
132+
const h = harness(async function* ({ store, mutation }) {
133+
const saved = await mutation;
134+
yield; // re-enter the transaction — the documented form
135+
yield until(() => store.rows.some(r => r.id === saved.id), { timeout: 200 });
136+
});
137+
h.feed.push({ rows: [] });
138+
await settle();
139+
const done = h.run().then(
140+
() => "settled",
141+
e => e
142+
);
143+
await settle();
144+
h.feed.push(CONFIRMED); // broadcast first: held under the action
145+
await settle();
146+
expect(h.views.at(-1)).toBe("temp:pending");
147+
h.answer();
148+
await settle();
149+
expect(await done).toBe("settled");
150+
await settle();
151+
expect(h.views.at(-1)).toBe("res_1:confirmed");
152+
});
153+
154+
test("inside the action WITHOUT the bare yield: until() is mainline, born held, and times out (#3482 as filed)", async () => {
155+
const h = harness(async function* ({ store, mutation }) {
156+
const saved = await mutation;
157+
// `until(...)` is evaluated in the await continuation, before this yield
158+
// re-enters — a mainline reader over the action's own hold.
159+
yield until(() => store.rows.some(r => r.id === saved.id), { timeout: 100 });
160+
});
161+
h.feed.push({ rows: [] });
162+
await settle();
163+
const done = h.run().then(
164+
() => "settled",
165+
e => e
166+
);
167+
await settle();
168+
h.feed.push(CONFIRMED);
169+
await settle();
170+
h.answer();
171+
await settle();
172+
await new Promise(r => setTimeout(r, 120));
173+
await settle();
174+
expect(await done).toBeInstanceOf(TimeoutError);
175+
});
176+
177+
test("mainline, outside the action: resolve() and until() over a foreign hold serve the committed view, after the commit — never the held frame", async () => {
178+
let release!: () => void;
179+
const gate = new Promise<void>(r => (release = r));
180+
const h = harness(async function* ({ mutation }) {
181+
await mutation;
182+
yield;
183+
yield gate; // keep the hold open past the answer
184+
});
185+
h.feed.push({ rows: [] });
186+
await settle();
187+
const done = h.run().then(
188+
() => "settled",
189+
e => e
190+
);
191+
await settle();
192+
h.feed.push(CONFIRMED);
193+
await settle();
194+
expect(h.views.at(-1)).toBe("temp:pending");
195+
196+
// Outside the action, after the frame is held. A reader here is not the
197+
// action's: it must not see the unrevealed frame.
198+
let resolved: unknown = "pending";
199+
let acked: unknown = "pending";
200+
resolve(() => h.store.rows.map(r => r.id).join(",") || "empty").then(
201+
v => (resolved = v),
202+
e => (resolved = e)
203+
);
204+
until(() => h.store.rows.some(r => r.id === "res_1"), { timeout: 1000 }).then(
205+
v => (acked = v),
206+
e => (acked = e)
207+
);
208+
h.answer();
209+
await settle();
210+
expect(h.views.at(-1)).toBe("temp:pending");
211+
expect(resolved).toBe("pending");
212+
expect(acked).toBe("pending");
213+
214+
release();
215+
await settle();
216+
expect(await done).toBe("settled");
217+
await settle();
218+
expect(h.views.at(-1)).toBe("res_1:confirmed");
219+
expect(resolved).toBe("res_1");
220+
expect(acked).toBe(true);
221+
});

0 commit comments

Comments
 (0)