Skip to content

Commit 4b62bc2

Browse files
ryansolidClaude via Cursor
andauthored
fix(signals): recompute preserves REACTIVE_DISPOSED set during the run (#3621) (#3625)
A memo that disposed its own root during its recompute came back alive: recompute's finally rebuilt _flags from the ZOMBIE bit alone, dropping the REACTIVE_DISPOSED that disposeChildren set reentrantly. isDisposed() read false, refresh() re-ran it, and the reads after the dispose() call had re-linked the dead node to its sources. - recompute's finally mask (and updateIfNecessary's) carries REACTIVE_DISPOSED. - A pass whose node died is void: clearDeps, retire the flight, close the attribution frame, return before the value publishes (a dead node freezes at its last committed value, #3024). - Tests: recompute-preserves-disposed-3621.test.ts (memo/root, deps unlinked, value not committed, outside effect not fired, effect twin, parent-owner twin, non-reentrant control). - Size: +72 B minified in the core floor (treeshake cap 26,230 -> 26,300); brotli caps ratcheted with notes for render+one signal (12.60 KB), hydrating+stores (31.30 KB), observe+attribution (29.05 KB). Fixes #3621 Co-authored-by: Claude via Cursor <[email protected]>
1 parent 75ed5e9 commit 4b62bc2

6 files changed

Lines changed: 329 additions & 8 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 computation that disposes its own owner during its recompute (a memo calling its root's `dispose()`) stays disposed: `recompute`'s `finally` now carries `REACTIVE_DISPOSED` alongside `REACTIVE_ZOMBIE` instead of dropping it, and the pass is void — dependencies re-linked after the `dispose()` call are unlinked, a flight it started is retired, and its value is neither committed nor propagated (a dead node freezes at its last committed value). `isDisposed()` reads true and `refresh()` no longer re-runs it into a torn-down tree (#3621).

‎packages/signals/src/core/attribution-hooks.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export interface AttributionHooks {
5454
flushEnd(): void;
5555
/**
5656
* A recompute is starting; `el._deps` still holds the previous run's links.
57-
* Always paired with `recomputeEnd` (recompute has no early returns).
57+
* Always paired with `recomputeEnd` (recompute's one early return — a node
58+
* disposed during its own pass, #3621 — fires it before leaving).
5859
*/
5960
recomputeStart(el: Computed<any>, create: boolean): void;
6061
/**

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import {
6161
type Refreshable
6262
} from "./constants.js";
6363
import { NotReadyError } from "./error.js";
64-
import { dormantNodes, link, trimStaleDeps } from "./graph.js";
64+
import { clearDeps, dormantNodes, link, trimStaleDeps } from "./graph.js";
6565
import {
6666
deleteFromHeap,
6767
enqueueSub,
@@ -514,7 +514,14 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
514514
// (markNode(c) in read()) marks the running node as part of ordinary
515515
// bookkeeping, and those marks are correctly discarded here.
516516
missedWake = (el._flags & REACTIVE_MISSED_WAKE) !== 0;
517-
el._flags = (el._flags & REACTIVE_ZOMBIE) | (create ? el._flags & REACTIVE_SNAPSHOT_STALE : 0);
517+
// REACTIVE_DISPOSED survives too (#3621): the pass may have disposed its
518+
// own owner (a memo calling its root's `dispose()`, a cleanup doing so
519+
// #3601/#3606), and `disposeChildren` set the flag on this node
520+
// reentrantly. Dropped, the node read as live — `refresh()` re-ran it
521+
// and `isDisposed()` lied.
522+
el._flags =
523+
(el._flags & (REACTIVE_ZOMBIE | REACTIVE_DISPOSED)) |
524+
(create ? el._flags & REACTIVE_SNAPSHOT_STALE : 0);
518525
context = oldcontext;
519526
}
520527
// The cast re-widens: TS narrowed `stagedEntry` to `null` at the reset
@@ -523,6 +530,26 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
523530
const bornHeld = stagedEntry as Transition | null;
524531
stagedEntry = prevStagedEntry;
525532

533+
// A node that died during its own pass (#3621) is dead at the end of it,
534+
// and the pass is void. Its owner's teardown already unlinked its deps,
535+
// removed it from the heap and ran its cleanups; what remains is what the
536+
// body did AFTER the `dispose()` call: reads that re-linked the dead node
537+
// to its sources (unlinked here — the leak that kept it re-running in a
538+
// torn-down tree), a flight it may have started (retired: the landing
539+
// checks `_inFlight` identity), and the value it returned. That value is
540+
// NOT published: a dead node freezes at its last committed value (#3024),
541+
// so nothing is staged, committed, or propagated to subscribers, and an
542+
// effect's run is not enqueued (runEffect would refuse it anyway). The
543+
// attribution frame opened at the top is still closed.
544+
if (el._flags & REACTIVE_DISPOSED) {
545+
clearDeps(el);
546+
if (el._x !== null) el._x._inFlight = null;
547+
if (__OBSERVE__ && attrHooks !== null)
548+
attrHooks.recomputeEnd(el, create, false, false, false, false);
549+
currentOptimisticLane = prevLane;
550+
return;
551+
}
552+
526553
if (!el._x?._error) {
527554
// Observe-tier fan-in (HUGE_FAN_IN): the validated prefix [_deps.._depsTail]
528555
// IS this pass's distinct sources — count it here rather than per link. A
@@ -914,9 +941,15 @@ function updateIfNecessary(el: Computed<unknown>): void {
914941
recompute(el);
915942
}
916943

944+
// The guard above refused an already-disposed node; the recompute it just
945+
// ran may have disposed it (#3621) — carry the flag, or it comes back alive.
917946
el._flags =
918947
el._flags &
919-
(REACTIVE_SNAPSHOT_STALE | REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT | REACTIVE_ZOMBIE);
948+
(REACTIVE_SNAPSHOT_STALE |
949+
REACTIVE_IN_HEAP |
950+
REACTIVE_IN_HEAP_HEIGHT |
951+
REACTIVE_ZOMBIE |
952+
REACTIVE_DISPOSED);
920953
}
921954

922955
export function computed<T>(fn: (prev?: T) => T | PromiseLike<T> | AsyncIterable<T>): Computed<T>;
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
// #3621: a memo that disposed its own root during its recompute came back
2+
// alive. `disposeChildren` set REACTIVE_DISPOSED on it reentrantly, and
3+
// `recompute`'s `finally` then rebuilt `_flags` from the ZOMBIE bit alone,
4+
// dropping it. `isDisposed()` read false, `refresh()` re-ran the memo, and the
5+
// reads that followed the `dispose()` call inside the pass had re-linked the
6+
// dead node to its sources — it kept re-running in a torn-down tree.
7+
//
8+
// Sibling of #3601 / #3606 (cleanups disposing the root mid-run). The ruling
9+
// here is the disposal path's: a node that dies during its own pass is dead
10+
// at the end of it, and the pass is void — no value commits or propagates
11+
// (#3024: a dead node freezes at its last committed value), no dependencies
12+
// survive it.
13+
14+
import {
15+
$REFRESH,
16+
createEffect,
17+
createMemo,
18+
createOwner,
19+
createRoot,
20+
createSignal,
21+
flush,
22+
isDisposed,
23+
refresh,
24+
runWithOwner
25+
} from "../src/index.js";
26+
27+
const node = (accessor: () => unknown) => (accessor as any)[$REFRESH];
28+
29+
afterEach(() => flush());
30+
31+
describe("#3621 a memo disposing its own root mid-recompute stays disposed", () => {
32+
it("issue repro: isDisposed is true and refresh() does not re-run it", () => {
33+
const [n, setN] = createSignal(0);
34+
let memo!: () => number;
35+
let runs = 0;
36+
37+
createRoot(dispose => {
38+
memo = createMemo(() => {
39+
runs++;
40+
if (n() === 1) dispose();
41+
return n();
42+
});
43+
createEffect(memo, () => {});
44+
});
45+
46+
flush();
47+
expect(runs).toBe(1);
48+
setN(1);
49+
flush();
50+
expect(runs).toBe(2);
51+
52+
expect(isDisposed(node(memo))).toBe(true);
53+
refresh(memo);
54+
flush();
55+
expect(runs).toBe(2);
56+
});
57+
58+
it("the reads after dispose() inside the pass leave no subscription behind", () => {
59+
const [n, setN] = createSignal(0);
60+
let memo!: () => number;
61+
let runs = 0;
62+
63+
createRoot(dispose => {
64+
memo = createMemo(() => {
65+
runs++;
66+
if (n() === 1) dispose();
67+
// Read AFTER the dispose: this link was created on a dead node.
68+
return n();
69+
});
70+
createEffect(memo, () => {});
71+
});
72+
73+
flush();
74+
setN(1);
75+
flush();
76+
expect(runs).toBe(2);
77+
78+
// No subscriber on the source, no dependency on the memo.
79+
expect(node(n)._subs).toBeNull();
80+
expect(node(memo)._deps).toBeNull();
81+
82+
// And a write to the source does not reach it.
83+
setN(2);
84+
flush();
85+
expect(runs).toBe(2);
86+
});
87+
88+
it("the value produced by the dying pass is not committed: the memo freezes at its last committed value (#3024)", () => {
89+
const [n, setN] = createSignal(0);
90+
let memo!: () => number;
91+
92+
createRoot(dispose => {
93+
memo = createMemo(() => {
94+
if (n() === 1) dispose();
95+
return n();
96+
});
97+
createEffect(memo, () => {});
98+
});
99+
100+
flush();
101+
expect(memo()).toBe(0);
102+
setN(1);
103+
flush();
104+
expect(memo()).toBe(0);
105+
});
106+
107+
it("an effect downstream of the memo (outside the disposed root) does not fire from the mid-compute value", () => {
108+
const [n, setN] = createSignal(0);
109+
let memo!: () => number;
110+
const seen: number[] = [];
111+
112+
createRoot(dispose => {
113+
memo = createMemo(() => {
114+
if (n() === 1) dispose();
115+
return n();
116+
});
117+
});
118+
createRoot(() => {
119+
createEffect(memo, v => {
120+
seen.push(v);
121+
});
122+
});
123+
124+
flush();
125+
expect(seen).toEqual([0]);
126+
setN(1);
127+
flush();
128+
expect(seen).toEqual([0]);
129+
setN(2);
130+
flush();
131+
expect(seen).toEqual([0]);
132+
});
133+
134+
it("effect twin: an effect whose compute phase disposes its root does not run its effect phase", () => {
135+
const [n, setN] = createSignal(0);
136+
const seen: number[] = [];
137+
let computes = 0;
138+
139+
createRoot(dispose => {
140+
createEffect(
141+
() => {
142+
computes++;
143+
if (n() === 1) dispose();
144+
return n();
145+
},
146+
v => {
147+
seen.push(v);
148+
}
149+
);
150+
});
151+
152+
flush();
153+
expect(seen).toEqual([0]);
154+
expect(computes).toBe(1);
155+
setN(1);
156+
flush();
157+
expect(computes).toBe(2);
158+
expect(seen).toEqual([0]);
159+
// Dead: no subscription survived the pass.
160+
expect(node(n)._subs).toBeNull();
161+
setN(2);
162+
flush();
163+
expect(computes).toBe(2);
164+
expect(seen).toEqual([0]);
165+
});
166+
167+
it("a memo disposing a parent owner (not its root) mid-compute stays disposed; the root survives", () => {
168+
const [n, setN] = createSignal(0);
169+
let memo!: () => number;
170+
let sibling!: () => number;
171+
let runs = 0;
172+
let siblingRuns = 0;
173+
174+
createRoot(() => {
175+
const owner = createOwner();
176+
runWithOwner(owner, () => {
177+
memo = createMemo(() => {
178+
runs++;
179+
if (n() === 1) owner.dispose();
180+
return n();
181+
});
182+
createEffect(memo, () => {});
183+
});
184+
// Owned by the root directly: must keep running after the inner owner dies.
185+
sibling = createMemo(() => {
186+
siblingRuns++;
187+
return n() * 10;
188+
});
189+
createEffect(sibling, () => {});
190+
});
191+
192+
flush();
193+
expect(runs).toBe(1);
194+
expect(siblingRuns).toBe(1);
195+
setN(1);
196+
flush();
197+
expect(runs).toBe(2);
198+
expect(siblingRuns).toBe(2);
199+
200+
expect(isDisposed(node(memo))).toBe(true);
201+
expect(isDisposed(node(sibling))).toBe(false);
202+
expect(node(memo)._deps).toBeNull();
203+
refresh(memo);
204+
flush();
205+
expect(runs).toBe(2);
206+
207+
setN(2);
208+
flush();
209+
expect(runs).toBe(2);
210+
expect(siblingRuns).toBe(3);
211+
expect(sibling()).toBe(20);
212+
});
213+
214+
it("control: disposing after the flush (non-reentrant) is unchanged", () => {
215+
const [n, setN] = createSignal(0);
216+
let memo!: () => number;
217+
let runs = 0;
218+
let dispose!: () => void;
219+
220+
createRoot(d => {
221+
dispose = d;
222+
memo = createMemo(() => {
223+
runs++;
224+
return n();
225+
});
226+
createEffect(memo, () => {});
227+
});
228+
229+
flush();
230+
setN(1);
231+
flush();
232+
expect(runs).toBe(2);
233+
expect(memo()).toBe(1);
234+
235+
dispose();
236+
expect(isDisposed(node(memo))).toBe(true);
237+
expect(node(n)._subs).toBeNull();
238+
refresh(memo);
239+
flush();
240+
expect(runs).toBe(2);
241+
setN(2);
242+
flush();
243+
expect(runs).toBe(2);
244+
// Frozen at the last committed value (#3024).
245+
expect(memo()).toBe(1);
246+
});
247+
});

‎packages/signals/tests/treeshake.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,13 @@ describe("pay-for-use tree-shaking (#2883)", () => {
410410
// frame parked as zombies, #3404) before setting REACTIVE_DISPOSED, since
411411
// the commit's drain returns on that flag. Gated on `self` and not
412412
// `zombie`: a rerun's `disposeChildren(el)` leaves the frame rendering.
413-
expect(minifiedBytes).toBeLessThan(26_230);
413+
// A node disposed during its own pass stays disposed and the pass is void
414+
// (#3621, 2026-09-23): +72 B core-retained (26,193 -> 26,265) — recompute's
415+
// `finally` mask (and updateIfNecessary's) carries REACTIVE_DISPOSED, and
416+
// recompute returns on it after the body: `clearDeps` (the reads after the
417+
// `dispose()` call re-linked the dead node), the flight retire, and the
418+
// lane restore. The mask bits are free; the bytes are the void-pass arm.
419+
expect(minifiedBytes).toBeLessThan(26_300);
414420
});
415421

416422
it("plain stores shed the verdict layer, affects, boundaries, and map", async () => {

0 commit comments

Comments
 (0)