Skip to content

Commit aa03f76

Browse files
fix(signals): share the held-landing visibility decision with deep()/snapshot readers (#3147)
Co-authored-by: Cursor <[email protected]>
1 parent 6b743c6 commit aa03f76

3 files changed

Lines changed: 170 additions & 20 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+
Share the pending-backing visibility decision between per-key store reads and deep()/snapshot composition (#3147). deep()/snapshot remain the speculative peek channel (they see ordinary pending staging synchronously), but a transition-HELD landing (#3164 fold) is now masked to the committed view for them exactly as it is for per-key readers, so the two reader families can no longer disagree while a transaction holds store landings.

‎packages/signals/src/store/next/store.ts‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,33 +1291,44 @@ function readSource(target: StoreNextTarget): Record<PropertyKey, any> {
12911291
const hv = heldMaskView(target);
12921292
if (hv !== null) return hv;
12931293
}
1294-
// Signal-parity visibility (core read(): owner-context reads serve
1295-
// _pendingValue, context-free reads serve committed — effects recompute
1296-
// BEFORE commitPendingNodes in the flush, so the pending view must be
1297-
// servable). Drafts (setter window OR projection write-override) and
1298-
// owner-context reads see the pending backing; context-free reads see
1299-
// committed. Node reads apply the same rule, so both homes agree.
1300-
if (
1294+
return pendingBackingVisible(target, false) ? target.pb! : target.v;
1295+
}
1296+
1297+
/** The single pb-vs-committed visibility decision (#3147), shared by per-key
1298+
* backing reads (readSource) and deep()/snapshot composition (snapshotWalk)
1299+
* so the two reader families can never disagree about a HELD landing.
1300+
*
1301+
* Signal-parity visibility (core read(): owner-context reads serve
1302+
* _pendingValue, context-free reads serve committed — effects recompute
1303+
* BEFORE commitPendingNodes in the flush, so the pending view must be
1304+
* servable). Drafts (setter window OR projection write-override) and
1305+
* owner-context reads see the pending backing; context-free reads see
1306+
* committed. Node reads apply the same rule, so all homes agree.
1307+
*
1308+
* `speculative` is deep()/snapshot's posture: an untrack/deep PEEK that sees
1309+
* ordinary pending staging regardless of owner context (the documented
1310+
* divergence from context-free per-key reads) — but never through a hold:
1311+
* held truth stays masked exactly as it is for per-key readers. */
1312+
function pendingBackingVisible(target: StoreNextTarget, speculative: boolean): boolean {
1313+
return (
13011314
target.pb !== null &&
13021315
(inDraft(target) ||
13031316
getWriteOverride() ||
1304-
// Owner-context readers see the pending backing — EXCEPT held truth
1305-
// on an optimistic family (#3164 fold): a live pb on an opt family
1306-
// outside the draft/write-override windows is a staged landing
1307-
// (tentative drafts never outlive their setter), and only the
1308-
// authoritative postures and latest() see it (the backing-level twin
1309-
// of core read()'s A17-for-held-truth arm; ordinary readers keep
1310-
// committed until the transaction's reveal).
1311-
(inOwnerContext() && !heldTruthMasked(target)) ||
1317+
// Owner-context (and speculative-peek) readers see the pending
1318+
// backing — EXCEPT held truth on an optimistic family (#3164 fold):
1319+
// a live pb on an opt family outside the draft/write-override windows
1320+
// is a staged landing (tentative drafts never outlive their setter),
1321+
// and only the authoritative postures and latest() see it (the
1322+
// backing-level twin of core read()'s A17-for-held-truth arm;
1323+
// ordinary readers keep committed until the transaction's reveal).
1324+
((speculative || inOwnerContext()) && !heldTruthMasked(target)) ||
13121325
// A projection's pending backing is authoritative-elect: serve it to
13131326
// context-free readers too UNLESS a transition is holding the node
13141327
// commits (downstream async hold — stale committed is the contract)
13151328
// or the reader is a CHILDREN_FORBIDDEN scope, which never observes
13161329
// its own unsettled write (#3082, signal parity per #3006).
13171330
(target.fam !== null && !heldTruthMasked(target) && !foldHeld(target) && !inForbiddenScope()))
1318-
)
1319-
return target.pb;
1320-
return target.v;
1331+
);
13211332
}
13221333

13231334
/** #3164 fold: HELD truth on an optimistic family — a pending backing
@@ -2108,10 +2119,15 @@ function snapshotWalk(value: any, seen: Map<object, any>, fam: StoreNextFamily |
21082119
if (t === undefined) break;
21092120
if (t.fam !== null) fam = t.fam;
21102121
if (t.fam?.opt === true) (optOwners ??= []).push(t);
2122+
// The shared visibility decision (#3147): the speculative peek serves
2123+
// pending staging, but a HELD landing is masked to committed exactly as
2124+
// it is for per-key readers — the two families must answer alike while
2125+
// a transaction holds store landings.
2126+
const usePB = pendingBackingVisible(t, true);
21112127
// Snapshot runs mid-flush (tracked memos execute before commit), so a
21122128
// pending prototype overlay must present as a REAL merged container.
2113-
if (t.ovl) materializePB(t);
2114-
const backing = t.pb ?? t.v;
2129+
if (usePB && t.ovl) materializePB(t);
2130+
const backing = usePB ? t.pb! : t.v;
21152131
if (backing === src) break;
21162132
src = backing;
21172133
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* #3147 — deep()/snapshot readers and per-key readers must agree while a
3+
* transaction holds store landings.
4+
*
5+
* deep()/snapshot compose from store backings; per-key reads answer through
6+
* node/backing visibility. #3145 removed the accidental capture (a stranger
7+
* transaction adopting a landing), but a LEGITIMATE hold — the #3164 fold of
8+
* a truth landing into the retaining transaction — reopened the seam: the
9+
* per-key channel masked the held landing (committed + optimism) while
10+
* deep() composed over the post-mutation pending backing (staged truth +
11+
* optimism), a frame no timeline contains.
12+
*
13+
* Ruling pinned here: deep()/snapshot stay the SPECULATIVE peek channel
14+
* (they see ordinary pending staging synchronously — the documented
15+
* divergence from context-free committed reads), but the hold decision is
16+
* SHARED with per-key reads: held truth is masked from both until the
17+
* transaction's reveal.
18+
*/
19+
import { describe, expect, it } from "vitest";
20+
import {
21+
action,
22+
createOptimisticStore,
23+
createRenderEffect,
24+
createRoot,
25+
deep,
26+
flush,
27+
snapshot
28+
} from "../../src/index.js";
29+
30+
const settle = async (n = 3) => {
31+
for (let i = 0; i < n; i++) {
32+
await new Promise(r => setTimeout(r, 0));
33+
flush();
34+
}
35+
};
36+
37+
describe("#3147: reader families agree under a held fold", () => {
38+
it("deep() composes the same view as per-key reads through the hold and the reveal", async () => {
39+
let resolveTruth!: () => void;
40+
const truth = new Promise<void>(r => (resolveTruth = r));
41+
let resolveGate!: () => void;
42+
const gate = new Promise<void>(r => (resolveGate = r));
43+
44+
const keyLog: string[] = [];
45+
const deepLog: string[] = [];
46+
const snapLog: string[] = [];
47+
let state!: { list: number[] };
48+
let setState!: (fn: (s: { list: number[] }) => void) => void;
49+
let run!: () => Promise<unknown>;
50+
let dispose!: () => void;
51+
52+
createRoot(d => {
53+
dispose = d;
54+
[state, setState] = createOptimisticStore<{ list: number[] }>(
55+
async function* () {
56+
yield { list: [1, 2, 3] };
57+
await truth;
58+
yield { list: [3, 2, 1] };
59+
},
60+
{ list: [] }
61+
);
62+
run = action(function* () {
63+
// The optimistic edit retains the transaction on the family: the
64+
// truth landing below folds into it instead of revealing.
65+
setState(s => {
66+
s.list.push(1111);
67+
});
68+
yield gate;
69+
});
70+
createRenderEffect(
71+
() => JSON.stringify(state.list),
72+
v => {
73+
keyLog.push(v);
74+
}
75+
);
76+
createRenderEffect(
77+
() => JSON.stringify(deep(state).list),
78+
v => {
79+
deepLog.push(v);
80+
}
81+
);
82+
// snapshot() from the same tracked scope must agree with deep().
83+
createRenderEffect(
84+
() => `${JSON.stringify(deep(state).list)}|${JSON.stringify(snapshot(state).list)}`,
85+
v => {
86+
snapLog.push(v);
87+
}
88+
);
89+
});
90+
flush();
91+
await settle();
92+
expect(keyLog.at(-1)).toBe("[1,2,3]");
93+
expect(deepLog.at(-1)).toBe("[1,2,3]");
94+
95+
const done = run();
96+
flush();
97+
await settle();
98+
// Optimistic frame in both reader families.
99+
expect(keyLog.at(-1)).toBe("[1,2,3,1111]");
100+
expect(deepLog.at(-1)).toBe("[1,2,3,1111]");
101+
102+
// Truth lands mid-action: it folds into the retaining transaction and is
103+
// HELD. Per-key readers keep the pre-hold view; deep()/snapshot must
104+
// agree — not report the post-mutation [3,2,1,1111].
105+
resolveTruth();
106+
await settle();
107+
expect(keyLog.at(-1)).toBe("[1,2,3,1111]");
108+
expect(deepLog.at(-1)).toBe("[1,2,3,1111]");
109+
110+
// Settle: atomic reveal, override reverts, landed truth shows — in both.
111+
resolveGate();
112+
await done;
113+
await settle();
114+
expect(keyLog.at(-1)).toBe("[3,2,1]");
115+
expect(deepLog.at(-1)).toBe("[3,2,1]");
116+
117+
// At no point may the families disagree (dedupe: the deep observer may
118+
// legitimately fire more often).
119+
const dedupe = (log: string[]) => log.filter((v, i) => v !== log[i - 1]);
120+
expect(dedupe(deepLog)).toEqual(dedupe(keyLog));
121+
// deep() and snapshot() from the same scope always agree with each other.
122+
for (const frame of snapLog) {
123+
const [d, s] = frame.split("|");
124+
expect(d).toBe(s);
125+
}
126+
dispose();
127+
flush();
128+
});
129+
});

0 commit comments

Comments
 (0)