Skip to content

Commit c0c8270

Browse files
ryansolidClaude via Cursor
andcommitted
fix(signals): a cleanup that disposes its root runs exactly once (#3601)
`runDisposal` iterated `node._disposal` / `_pendingDisposal` and nulled it only after the loop. A cleanup that calls the root's `dispose()` re-enters the node through the death walk (#3592's parked-frame drain) and ran the whole list a second time. The same shape latched a throwing cleanup: the list survived the throw and every later flush re-threw it. Detach the list before running it, as `_cleanup` already does (#2813). Mirror in the server `disposeOwner`. Fixes #3601 Co-authored-by: Claude via Cursor <[email protected]>
1 parent cf61b8e commit c0c8270

5 files changed

Lines changed: 251 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@solidjs/signals": patch
3+
"solid-js": patch
4+
---
5+
6+
A cleanup that disposes its own root (or throws) runs exactly once: the disposal list is detached before it runs (#3601).

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,16 @@ export function linkChild(parent: Owner, node: Owner): void {
192192
}
193193

194194
function runDisposal(node: Owner, zombie?: boolean): void {
195-
let disposal = zombie ? node._x?._pendingDisposal : node._disposal;
195+
// Detach the list BEFORE running it (#3601), as `_cleanup` is (#2813). A
196+
// cleanup that disposes an ancestor re-enters this node through the death
197+
// walk while the loop is still running; with the list still attached, that
198+
// walk ran every entry a second time. Detached, the re-entrant drain finds
199+
// nothing. The same shape latched a throwing cleanup: the list survived the
200+
// throw and every later drain re-ran and re-threw it.
201+
const disposal = zombie ? node._x?._pendingDisposal : node._disposal;
196202
if (!disposal) return;
203+
if (zombie) node._x!._pendingDisposal = null;
204+
else node._disposal = null;
197205

198206
if (Array.isArray(disposal)) {
199207
// Unwind order (#3572, restores 1.x #1562): later registrations run
@@ -205,11 +213,8 @@ function runDisposal(node: Owner, zombie?: boolean): void {
205213
callable.call(callable);
206214
}
207215
} else {
208-
(disposal as Disposable).call(disposal);
216+
disposal.call(disposal);
209217
}
210-
if (zombie) {
211-
if (node._x !== null) node._x._pendingDisposal = null;
212-
} else node._disposal = null;
213218
}
214219

215220
function childId(owner: Owner, consume: boolean): string {
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// #3601: an `onCleanup` callback ran twice when it disposed its root during a
2+
// memo's recomputation.
3+
//
4+
// A rerun's `disposeChildren(el)` → `runDisposal(el)` iterates `el._disposal`
5+
// and only nulls it AFTER the loop. A cleanup that calls the root's `dispose()`
6+
// re-enters `disposeChildren(root, true)`, whose walk reaches `el` again while
7+
// the same array is still attached — and runs every entry a second time.
8+
9+
import {
10+
action,
11+
createEffect,
12+
createMemo,
13+
createRoot,
14+
createSignal,
15+
flush,
16+
onCleanup,
17+
resetErrorHalt
18+
} from "../src/index.js";
19+
20+
afterEach(() => {
21+
flush();
22+
resetErrorHalt();
23+
});
24+
25+
describe("#3601 onCleanup disposing the root during memo recomputation", () => {
26+
it("runs the cleanup exactly once (issue repro)", () => {
27+
const [version, setVersion] = createSignal(0);
28+
let cleanups = 0;
29+
30+
createRoot(dispose => {
31+
const memo = createMemo(() => {
32+
const current = version();
33+
if (current === 0) {
34+
onCleanup(() => {
35+
cleanups++;
36+
dispose();
37+
});
38+
}
39+
return current;
40+
});
41+
createEffect(memo, () => {});
42+
});
43+
44+
flush();
45+
setVersion(1);
46+
flush();
47+
expect(cleanups).toBe(1);
48+
});
49+
50+
it("with several registered cleanups, each runs once, in unwind order (#3572)", () => {
51+
const [version, setVersion] = createSignal(0);
52+
const log: string[] = [];
53+
54+
createRoot(dispose => {
55+
const memo = createMemo(() => {
56+
const current = version();
57+
if (current === 0) {
58+
onCleanup(() => log.push("a"));
59+
onCleanup(() => {
60+
log.push("b");
61+
dispose();
62+
});
63+
onCleanup(() => log.push("c"));
64+
}
65+
return current;
66+
});
67+
createEffect(memo, () => {});
68+
onCleanup(() => log.push("root"));
69+
});
70+
71+
flush();
72+
setVersion(1);
73+
flush();
74+
expect(log).toEqual(["c", "b", "root", "a"]);
75+
});
76+
77+
it("effect twin: a compute-phase cleanup disposing the root runs once", () => {
78+
const [version, setVersion] = createSignal(0);
79+
let cleanups = 0;
80+
81+
createRoot(dispose => {
82+
createEffect(
83+
() => {
84+
const current = version();
85+
if (current === 0) {
86+
onCleanup(() => {
87+
cleanups++;
88+
dispose();
89+
});
90+
}
91+
return current;
92+
},
93+
() => {}
94+
);
95+
});
96+
97+
flush();
98+
setVersion(1);
99+
flush();
100+
expect(cleanups).toBe(1);
101+
});
102+
103+
it("held-children twin: a rerun under a hold drains `_disposal` directly — still once", async () => {
104+
// An uncommitted pass's registrations (CONFIG_HELD_CHILDREN) die
105+
// immediately on the next rerun via `disposeChildren(el)` — the same
106+
// cleared-after-the-loop shape on the `_disposal` slot instead of the
107+
// parked frame's `_pendingDisposal`. This path predates #3592.
108+
let setVisible!: (v: boolean) => void;
109+
let setTick!: (v: number) => void;
110+
let cleanups = 0;
111+
const pending = Promise.withResolvers<void>();
112+
113+
createRoot(dispose => {
114+
const [visible, sv] = createSignal(true);
115+
const [tick, st] = createSignal(0);
116+
setVisible = sv;
117+
setTick = st;
118+
const branch = createMemo(() => {
119+
tick();
120+
if (visible()) return true;
121+
// registered by the held pass, drained by the next held rerun
122+
onCleanup(() => {
123+
cleanups++;
124+
dispose();
125+
});
126+
return false;
127+
});
128+
createEffect(branch, () => {});
129+
});
130+
flush();
131+
132+
const changed = action(function* () {
133+
setVisible(false);
134+
yield Promise.resolve();
135+
setTick(1);
136+
yield pending.promise;
137+
})();
138+
flush();
139+
for (let i = 0; i < 10; i++) await Promise.resolve();
140+
flush();
141+
expect(cleanups).toBe(1);
142+
143+
pending.resolve();
144+
await changed;
145+
flush();
146+
expect(cleanups).toBe(1);
147+
});
148+
149+
it("a throwing compute-phase cleanup does not latch: later flushes are clean", () => {
150+
// Same shape as #2813's `_cleanup` ruling, on the `_disposal` list: the
151+
// thrower is detached before it runs, so it fires once and never again.
152+
vi.spyOn(console, "error").mockImplementation(() => {});
153+
const [version, setVersion] = createSignal(0);
154+
let runs = 0;
155+
156+
createRoot(() => {
157+
const memo = createMemo(() => {
158+
const current = version();
159+
if (current === 0) {
160+
onCleanup(() => {
161+
runs++;
162+
throw new Error("cleanup boom");
163+
});
164+
}
165+
return current;
166+
});
167+
createEffect(memo, () => {});
168+
});
169+
170+
flush();
171+
expect(() => {
172+
setVersion(1);
173+
flush();
174+
}).toThrow("cleanup boom");
175+
expect(runs).toBe(1);
176+
177+
// Unpatched, the list survived the throw and every later flush re-ran
178+
// and re-threw it. The entry must be gone.
179+
resetErrorHalt();
180+
setVersion(2);
181+
flush();
182+
setVersion(3);
183+
flush();
184+
expect(runs).toBe(1);
185+
});
186+
});

‎packages/solid/src/server/signals.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ export function disposeOwner(owner: Owner, self: boolean = true): void {
439439
}
440440
node._firstChild = null;
441441
node._childCount = 0;
442+
// Detached before it runs, mirroring the client `runDisposal` (#3601): a
443+
// cleanup that re-enters this owner's disposal must find nothing to re-run.
442444
const d = node._disposal;
445+
node._disposal = null;
443446
if (d) {
444447
if (Array.isArray(d)) {
445448
// Unwind order, mirroring the client `runDisposal` (#3572): later
@@ -448,7 +451,6 @@ export function disposeOwner(owner: Owner, self: boolean = true): void {
448451
} else {
449452
d();
450453
}
451-
node._disposal = null;
452454
}
453455
if (self) unlinkOwner(node);
454456
// Recycle the disposed owner. Skip the root case (`self=false`) and the

‎packages/solid/test/server/signals.spec.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
import {
4040
createErrorBoundary,
4141
createLoadingBoundary,
42+
resetOwnerForRerun,
4243
NotReadyError as NotReadyErrorClass,
4344
type Owner
4445
} from "../../src/server/signals.js";
@@ -657,6 +658,51 @@ describe("Server owner tree", () => {
657658
expect(order).toEqual(["C", "B", "A"]);
658659
});
659660

661+
test("a cleanup that disposes its own root during a rerun reset runs once (#3601)", () => {
662+
// Twin of the client `runDisposal` ruling: `disposeOwner` detaches the
663+
// list before running it. A rerun reset (`self=false`, the server's
664+
// recompute-disposal) doesn't mark the owner disposed, so a cleanup that
665+
// calls the root's `dispose()` re-enters the same owner mid-loop — with
666+
// the list still attached it ran every entry again.
667+
const log: string[] = [];
668+
let dispose!: () => void;
669+
let owner!: Owner;
670+
createRoot(
671+
d => {
672+
dispose = d;
673+
owner = getOwner()!;
674+
onCleanup(() => log.push("a"));
675+
onCleanup(() => {
676+
log.push("b");
677+
dispose();
678+
});
679+
onCleanup(() => log.push("c"));
680+
},
681+
{ id: "test" }
682+
);
683+
resetOwnerForRerun(owner);
684+
expect(log).toEqual(["c", "b", "a"]);
685+
});
686+
687+
test("a child cleanup that disposes the root runs once (#3601)", () => {
688+
let cleanups = 0;
689+
let dispose!: () => void;
690+
createRoot(
691+
d => {
692+
dispose = d;
693+
createRoot(() =>
694+
onCleanup(() => {
695+
cleanups++;
696+
dispose();
697+
})
698+
);
699+
},
700+
{ id: "test" }
701+
);
702+
dispose();
703+
expect(cleanups).toBe(1);
704+
});
705+
660706
test("disposing a parent does not dispose an owner recycled into another root", () => {
661707
let disposeParent!: () => void;
662708
let disposeChild!: () => void;

0 commit comments

Comments
 (0)