Skip to content

Commit c827758

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(signals): a projection's leaf companions die with the projection; latest() of a dead leaf creates none (spec O5, INV-4)
Disposing a store whose async source was mid-refetch left the latest() shadow of a leaf orphaned: never derived (its compute read through the projection in flight, the backfilled override stood in), its override dropped at the settle, NotReady/uninitialized forever against a leaf whose committed value differed — INV-4 at the next quiescence. Externally coherent (latest() fell back to the committed value), but a companion outliving its source, which is what INV-9's rationale forbids. The disposal snap covered the disposed computed's own companions only; a projection's leaves hang off the firewall, not the child chain. Two parts: - disposeChildren(self) snaps the companions of the firewall's _companionChildren (the set markFirewallChildCompanions maintains, #3038); snapCompanionsToState retires a shadow whose owner's firewall is disposed — a disposed shadow reads as absent, a later read recreates it from the committed view. The isPending signal snaps as the owner's does. - latestRead of a leaf whose firewall is disposed serves the committed value and creates no shadow: a boundary's content re-running after the teardown recreated one that no teardown would ever retire. That recreated shadow was the posture matrix's actual trigger. Surfaced once #3495 stopped leaking parked transactions, which had kept `transitions.size > 0` and masked every __TEST__ quiescence check in the matrix. All 621 matrix cells run with zero invariant violations; the two excluded cells are back. Pin flipped from it.fails and extended with the post-disposal read. +104 B minified (owner.ts, core floor); five brotli caps +13…+43 B. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 810d856 commit c827758

8 files changed

Lines changed: 121 additions & 73 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+
A projection's leaf companions die with the projection. Disposing a store whose async source was mid-refetch left the `latest()` shadow of a leaf orphaned: never derived (its compute read through the projection in flight, the backfilled override stood in), its override dropped at the settle, NotReady forever against a leaf whose committed value differed — the `__TEST__` quiescence invariant INV-4 at the next flush. Externally coherent, but a companion outliving its source. The firewall's teardown now snaps the companions of its companion-bearing leaves (the shadow is retired; a later read recreates it from the committed view), and `latest()` of a leaf whose firewall is already disposed serves the committed value without creating a shadow — a boundary's content re-running after the teardown had recreated one that nothing would retire. Surfaced once #3495 stopped leaking parked transactions, which had masked every quiescence check in the posture matrix; all 621 matrix cells now run with zero invariant violations.

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

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

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ An error escaping every boundary permanently halts the system with `REACTIVITY_H
326326
**Rule:** same-tick adoption is by design (O1); A28 (1)/(2) still govern visibility — nothing is visible before the flush that carries the write, on any channel. The store is right.
327327
**Mechanism (current):** adoption stamps the signal with the transaction (`initTransition`'s pending-node loop); `unflushedValue` reads a stamped node with no `_flushedStaged` stash as a flushed held node and serves `_pendingValue`. The store's selection (`nodeValue` / `serveDataKey`, `flushedStaged`) does not take that path. One rule, two implementations — the fix is making "unflushed" mean the same thing at both sites (a node staged outside a flush and adopted before any flush is unflushed whatever its stamp).
328328

329-
### O5. INV-4 — a projection leaf's `latest()` shadow is stale on the flush right after its root is disposed mid-refetch — violation, open
329+
### O5. INV-4 — a projection leaf's `latest()` shadow is stale on the flush right after its root is disposed mid-refetch — fixed
330330

331-
**Status:** **violation, recorded** 2026-09-16 — surfaced by O3's fix: the posture matrix had been leaking parked transactions (dead reporters never re-judged), which kept `transitions.size > 0` and silenced every quiescence invariant for the rest of the run. With the leak gone, INV-4 fires. Pre-existing on `next` (standalone repro: projection store with a held refetch, `latest()` read of a leaf, `dispose()`, synchronous `flush()`); pinned `it.fails` in `tests/inv4-projection-dispose-shadow.test.ts` (its own file: a live action in a sibling test masks the check). Externally the leaf and its `latest()` agree throughout — the stale pair is an internal companion owner. Transient — the shadow is re-derived a microtask later — but under `__TEST__` a throw from the runtime's own scheduled flush leaves the scheduler mid-flush, so the matrix excludes the two triggering cells (`gatedAway` × the projection states) until fixed.
332-
**Where to look:** the disposal snap (`disposeChildren` → `_snapCompanions`) covers the disposed computed's own companions; a projection's leaves hang off the firewall, not the child chain, and their companions are re-derived only by the scheduled pass that follows.
331+
**Status:** **violation, fixed** 2026-09-16 — surfaced by O3's fix: the posture matrix had been leaking parked transactions (dead reporters never re-judged), which kept `transitions.size > 0` and silenced every quiescence invariant for the rest of the run. With the leak gone, INV-4 fires. Pre-existing on `next` (standalone repro: projection store with a held refetch, `latest()` read of a leaf, `dispose()`, synchronous `flush()`); pinned `it.fails` in `tests/inv4-projection-dispose-shadow.test.ts` (its own file: a live action in a sibling test masks the check). Externally the leaf and its `latest()` agree throughout — the stale pair is an internal companion owner. Transient — the shadow is re-derived a microtask later — but under `__TEST__` a throw from the runtime's own scheduled flush leaves the scheduler mid-flush, so the matrix excludes the two triggering cells (`gatedAway` × the projection states) until fixed.
332+
**Cause:** the leaf's shadow had never derived a value — its compute reads through a projection in flight (NotReady), so the backfilled override stood in — and the settle after disposal dropped the override, leaving a shadow NotReady/uninitialized forever against a leaf whose committed value differs. Not a skipped sync: an orphan. The disposal snap covered the disposed computed's own companions only; a projection's leaves hang off the firewall, not the child chain.
333+
**Mechanism:** `disposeChildren(self)` snaps the companions of the firewall's `_companionChildren` (the set `markFirewallChildCompanions` already maintains, #3038), and `snapCompanionsToState` retires a shadow whose owner's firewall is disposed — `getLatestValueComputed` treats a disposed shadow as absent, so a later read recreates it from the committed view; the isPending signal snaps as the owner's own does. And `latestRead` of a leaf whose firewall is disposed serves the committed value and creates no shadow — a boundary's content re-running after the teardown recreated one (a disposed shadow reads as absent) that no teardown would ever retire; that recreated shadow was the matrix's actual trigger. The matrix's two excluded cells are back; 0 invariant cells across all 621.
333334

334335
## Superseded rules (kept verbatim)
335336

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CONFIG_AUTO_DISPOSE,
3+
CONFIG_CHILD_COMPANIONS,
34
CONFIG_CHILDREN_FORBIDDEN,
45
CONFIG_TRANSPARENT,
56
defaultContext,
@@ -79,6 +80,13 @@ export function disposeChildren(node: Owner, self: boolean = false, zombie?: boo
7980
// false, and notifies subscribers still watching the companion.
8081
const n = node as Computed<unknown>;
8182
if (n._x?._pendingSignal || n._x?._latestValueComputed) GlobalQueue._snapCompanions!(n);
83+
// A firewall's leaves have no lifecycle of their own, so a companion on
84+
// one of them outlives its source the same way (INV-9's rationale). The
85+
// firewall knows which leaves carry companions (CONFIG_CHILD_COMPANIONS,
86+
// #3038): snap them with it — the snap retires a shadow whose firewall is
87+
// disposed (spec O5).
88+
if (n._config & CONFIG_CHILD_COMPANIONS)
89+
n._x!._companionChildren!.forEach(GlobalQueue._snapCompanions! as (leaf: unknown) => void);
8290
// A pending reader parked in a transaction may be the only thing holding
8391
// it (#3372): its death is a completion event the transaction must be
8492
// re-judged for, and nothing else re-enters a parked transaction.

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
} from "./core.js";
4747
import { NotReadyError } from "./error.js";
4848
import { link } from "./graph.js";
49+
import { dispose } from "./owner.js";
4950
import { enqueueSub, insertIntoHeap, markHeap, queueFor } from "./heap.js";
5051
import { devTrackCompanionOwner, InvariantHooks } from "./invariants.js";
5152
import {
@@ -409,6 +410,18 @@ function snapCompanionsToState(owner: Signal<any> | Computed<any>): void {
409410
}
410411
const shadow = owner._x?._latestValueComputed;
411412
if (shadow && !(shadow._flags & REACTIVE_DISPOSED)) {
413+
// A leaf whose firewall is disposed (the projection's teardown snaps its
414+
// companion-bearing leaves): the shadow's compute reads through a
415+
// disposed, possibly still-pending projection and would sit
416+
// NotReady/uninitialized forever — never derived, its backfilled override
417+
// dropped at the settle — against a leaf whose committed value differs
418+
// (INV-4 at the next quiescence; spec O5). It dies with its source;
419+
// getLatestValueComputed treats a disposed shadow as absent, so a later
420+
// read recreates it from the committed view.
421+
if ((owner as FirewallSignal<any>)._firewall?._flags & REACTIVE_DISPOSED) {
422+
dispose(shadow);
423+
return;
424+
}
412425
if (
413426
(shadow._x?._overrideValue === undefined || shadow._x?._overrideValue === NOT_PENDING) &&
414427
shadow._pendingValue === NOT_PENDING &&
@@ -477,6 +490,12 @@ function uninitializedSource(el: Signal<any> | Computed<any>): boolean {
477490
}
478491

479492
function latestRead<T>(el: Signal<T> | Computed<T>): T {
493+
// A leaf of a DISPOSED projection has no flushed world left to mirror: a
494+
// shadow created for it now would read through the dead firewall, sit
495+
// NotReady/uninitialized forever, and no teardown would ever retire it (the
496+
// firewall's already ran — spec O5). Serve the committed value; create
497+
// nothing. (A read of a disposed node freezes at its last commit, #3024.)
498+
if ((el as FirewallSignal<T>)._firewall?._flags & REACTIVE_DISPOSED) return el._value as T;
480499
const pendingComputed = getLatestValueComputed(el);
481500
const prevPending = latestReadActive;
482501
setLatestReadActive(false);
Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
/**
2-
* INV-4 after disposing a projection mid-refetch — VIOLATION, pinned it.fails
3-
* (spec O5; posture matrix S3).
2+
* A projection's leaf companions die with the projection (spec O5).
43
*
5-
* Reproduces on `next`: with a projection store's refetch held and a
6-
* `latest()` companion created on a leaf, the synchronous flush right after
7-
* `dispose()` trips the __TEST__ quiescence invariant INV-4 ("latest() shadow
8-
* holds a stale committed value for a settled node"). Externally the leaf and
9-
* its shadow agree throughout (`s.n` and `latest(() => s.n)` both read the
10-
* committed 0), so the stale pair is an INTERNAL companion owner — the
11-
* projection's firewall node is the candidate — and the window closes a
12-
* microtask later. Under __TEST__ the runtime's own scheduled flush throws in
13-
* that window and leaves the scheduler mid-flush, which is what poisoned the
14-
* posture matrix once #3488 / O3 stopped leaking the parked transactions that
15-
* had kept `transitions.size > 0` and silenced every quiescence check.
4+
* Was: with a projection store's refetch held and a `latest()` companion
5+
* created on a leaf, the synchronous flush right after `dispose()` tripped
6+
* the __TEST__ quiescence invariant INV-4. The leaf's shadow had never
7+
* derived a value — its compute reads through a projection in flight, so the
8+
* backfilled override stood in — and the settle after disposal dropped the
9+
* override, leaving a shadow that is NotReady/uninitialized forever against a
10+
* leaf whose committed value is 0. Externally coherent (`latest()` fell back
11+
* to the committed value), but a companion outliving its source is what
12+
* INV-9's rationale forbids. Now `disposeChildren(self)` retires the
13+
* companions of the firewall's `_companionChildren`: the shadow is disposed
14+
* (a later read recreates it from the committed view), the isPending signal
15+
* snaps. And `latest()` of a leaf whose firewall is already disposed serves
16+
* the committed value without creating a shadow — a boundary's content
17+
* re-running after the teardown recreated one that no teardown would retire.
18+
* Surfaced when #3495 / O3 stopped leaking the parked transactions that had
19+
* masked every quiescence check in the posture matrix.
1620
*
17-
* Own file: a live action in a sibling test would mask the check the same way.
21+
* Own file: a live action in a sibling test would mask the check.
1822
*/
1923
import { expect, it } from "vitest";
2024
import {
@@ -26,37 +30,40 @@ import {
2630
latest
2731
} from "../src/index.js";
2832

29-
it.fails(
30-
"the flush right after disposing a projection with a held refetch passes the quiescence invariants (INV-4)",
31-
async () => {
32-
const [q, setQ] = createSignal(0);
33-
const fetches: Array<() => void> = [];
34-
let s!: { n: number };
35-
const dispose = createRoot(d => {
36-
[s] = createStore<{ n: number }>(
37-
() => {
38-
const v = q();
39-
return new Promise(r => fetches.push(() => r({ n: v * 10 })));
40-
},
41-
{ n: -1 }
42-
);
43-
createRenderEffect(
44-
() => s.n,
45-
() => {}
46-
);
47-
return d;
48-
});
33+
it("the flush right after disposing a projection with a held refetch passes the quiescence invariants (INV-4)", async () => {
34+
const [q, setQ] = createSignal(0);
35+
const fetches: Array<() => void> = [];
36+
let s!: { n: number };
37+
const dispose = createRoot(d => {
38+
[s] = createStore<{ n: number }>(
39+
() => {
40+
const v = q();
41+
return new Promise(r => fetches.push(() => r({ n: v * 10 })));
42+
},
43+
{ n: -1 }
44+
);
45+
createRenderEffect(
46+
() => s.n,
47+
() => {}
48+
);
49+
return d;
50+
});
51+
flush();
52+
fetches.shift()!();
53+
for (let i = 0; i < 3; i++) {
54+
await new Promise(r => setTimeout(r, 0));
4955
flush();
50-
fetches.shift()!();
51-
for (let i = 0; i < 3; i++) {
52-
await new Promise(r => setTimeout(r, 0));
53-
flush();
54-
}
55-
setQ(1); // refetch, never lands
56-
flush();
57-
expect(latest(() => s.n)).toBe(0); // creates the leaf's companion
58-
dispose();
59-
expect(() => flush()).not.toThrow(); // INV-4 here on next
60-
expect(latest(() => s.n)).toBe(s.n); // (externally the two agree — the stale pair is internal)
6156
}
62-
);
57+
setQ(1); // refetch, never lands
58+
flush();
59+
expect(latest(() => s.n)).toBe(0); // creates the leaf's companion
60+
dispose();
61+
expect(() => flush()).not.toThrow(); // INV-4 here before the fix
62+
// A latest() read AFTER the disposal (a boundary's content re-running, a
63+
// stale handler) must not recreate a shadow on the dead leaf — nothing
64+
// would ever retire it. It serves the committed value and creates nothing.
65+
expect(latest(() => s.n)).toBe(s.n);
66+
const [, poke] = createSignal(0);
67+
poke(1);
68+
expect(() => flush()).not.toThrow(); // the recreated shadow tripped INV-4 here
69+
});

‎packages/signals/tests/visibility-oracle-posture.test.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,6 @@ describe("visibility oracle — posture matrix (discovery)", () => {
341341
for (const posture of POSTURES)
342342
for (const reader of READERS) {
343343
if (posture === "gatedAway" && reader !== "memo" && reader !== "effect") continue; // a gate needs a tracked reader
344-
// INV-4 (spec O5, pinned it.fails in posture-store-parity.test.ts): a
345-
// projection leaf's latest() shadow is stale on the flush right after
346-
// its root is disposed. Under __TEST__ the runtime's own scheduled
347-
// flush throws and the scheduler is left mid-flush, poisoning every
348-
// later cell. Excluded until fixed; the pin names the bug.
349-
if (posture === "gatedAway" && state.name.startsWith("derived store (projection)"))
350-
continue;
351344
it(`${state.name} × ${posture} × ${reader}`, async () => {
352345
rows.push(await cell(state, posture, reader));
353346
expect(true).toBe(true);

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,10 @@ module.exports = [
278278
// Reporter-liveness fix rebased over `_parent` mangling (#3495 + #3496,
279279
// 2026-09-16): measured at 9,379 B. The signals floor is unchanged
280280
// minified; the combined property names shift brotli layout.
281-
limit: "9.38 KB",
281+
// A projection's leaf companions die with it; latest() of a dead leaf creates
282+
// none (spec O5, 2026-09-16): 9,393 B (+13 over the cap); +104 B minified in
283+
// owner.ts (core floor), the shadow retirement lives in verdict.ts.
284+
limit: "9.40 KB",
282285
modifyEsbuildConfig
283286
},
284287
{
@@ -523,7 +526,10 @@ module.exports = [
523526
// (+1 over the cap); +100 B minified in the signals floor (24,478 -> 24,578).
524527
// Hold-consistency batch 2 (#3479, 2026-09-15): measured at 16,343 B; the signals
525528
// core delta, see the core floor note.
526-
limit: "16.45 KB",
529+
// A projection's leaf companions die with it; latest() of a dead leaf creates
530+
// none (spec O5, 2026-09-16): 16,481 B (+31 over the cap); +104 B minified in
531+
// owner.ts (core floor), the shadow retirement lives in verdict.ts.
532+
limit: "16.50 KB",
527533
modifyEsbuildConfig
528534
},
529535
{
@@ -659,7 +665,10 @@ module.exports = [
659665
// promote-on-revert, skipped by the body-end supersession and the
660666
// authoritative-blockage census, merged through and suspended on in lanes,
661667
// pending flowing through it in status. Retained here by `latest()`.
662-
limit: "12.05 KB",
668+
// A projection's leaf companions die with it; latest() of a dead leaf creates
669+
// none (spec O5, 2026-09-16): 12,074 B (+24 over the cap); +104 B minified in
670+
// owner.ts (core floor), the shadow retirement lives in verdict.ts.
671+
limit: "12.10 KB",
663672
modifyEsbuildConfig
664673
},
665674
{
@@ -1075,7 +1084,10 @@ module.exports = [
10751084
// write of `onError` onto the root owner is the only prod-floor cost).
10761085
// Reporter-liveness fix rebased over `_parent` mangling (#3495 + #3496,
10771086
// 2026-09-16): measured at 29,903 B; combined brotli layout drift.
1078-
limit: "29.91 KB",
1087+
// A projection's leaf companions die with it; latest() of a dead leaf creates
1088+
// none (spec O5, 2026-09-16): 29,953 B (+43 over the cap); +104 B minified in
1089+
// owner.ts (core floor), the shadow retirement lives in verdict.ts.
1090+
limit: "30.00 KB",
10791091
modifyEsbuildConfig
10801092
},
10811093
{
@@ -1415,7 +1427,10 @@ module.exports = [
14151427
// write of `onError` onto the root owner is the only prod-floor cost).
14161428
// Reporter-liveness fix rebased over `_parent` mangling (#3495 + #3496,
14171429
// 2026-09-16): measured at 28,363 B; combined brotli layout drift.
1418-
limit: "28.37 KB",
1430+
// A projection's leaf companions die with it; latest() of a dead leaf creates
1431+
// none (spec O5, 2026-09-16): 28,392 B (+22 over the cap); +104 B minified in
1432+
// owner.ts (core floor), the shadow retirement lives in verdict.ts.
1433+
limit: "28.40 KB",
14191434
modifyEsbuildConfig: observeEsbuildConfig
14201435
},
14211436
{

0 commit comments

Comments
 (0)