Skip to content

Commit 43fae6e

Browse files
ryansolidClaude via Cursor
andauthored
fix(signals): cleanups run in unwind order — later registrations first, children before owner (#3572) (#3591)
`runDisposal` (client) and `disposeOwner` (server facade) now iterate an owner's `_disposal` list in reverse registration order. Children were already disposed before the owner's own cleanups; this restores the 1.x LIFO rule from #1562 (8d0877e) that 2.0's forward walk regressed. In production a component body shares its enclosing owner (no per-component owner), so with LIFO a parent that registers `onCleanup` before rendering its children now tears down after them — matching the dev tier's per-component owner (#3572). Documented divergences that remain: a parent registering `onCleanup` after creating its children, and a parent effect-returned cleanup vs a child body's `onCleanup`. Tests: new unwind-order pins in signals (source + all three dist tiers), the server facade, and the flattened-vs-wrapped component shapes in solid; two tests that pinned the old FIFO order incidentally are updated. Co-authored-by: Claude via Cursor <[email protected]>
1 parent d2d7bd4 commit 43fae6e

11 files changed

Lines changed: 180 additions & 13 deletions

File tree

‎.changeset/cleanup-unwind-order.md‎

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+
`onCleanup` callbacks on one owner now run in reverse registration order (unwind), restoring the 1.x #1562 semantics; in production, component bodies share the enclosing owner, so a parent that registers cleanup before rendering its children now tears down after them, matching dev (#3572).

‎documentation/solid-2.0/02-signals-derived-ownership.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function Widget() {
3030
// When Widget is disposed, the nested root is disposed too.
3131
```
3232

33+
#### Disposal order is unwind
34+
35+
Cleanups run in unwind order: an owner's children are disposed before its own cleanups, and within one owner later registrations run before earlier ones (the same LIFO rule 1.x had). In production a component body shares its enclosing owner rather than getting one of its own, so a parent that registers `onCleanup` before rendering its children tears down after them — the same order the dev build's per-component owner gives. Register cleanup before creating children when the order between them matters; a parent that registers `onCleanup` *after* creating its children runs before them in production (and after them in dev).
36+
3337
#### Detaching is explicit: `runWithOwner(null, ...)`
3438

3539
If you really want “no owner” (module singletons, external integrations), detach explicitly:

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ function runDisposal(node: Owner, zombie?: boolean): void {
184184
if (!disposal) return;
185185

186186
if (Array.isArray(disposal)) {
187-
for (let i = 0; i < disposal.length; i++) {
187+
// Unwind order (#3572, restores 1.x #1562): later registrations run
188+
// before earlier ones. Children have already been disposed by the caller,
189+
// so with LIFO a body that registers cleanup before creating its children
190+
// tears down after them — the same order a per-component owner gives.
191+
for (let i = disposal.length - 1; i >= 0; i--) {
188192
const callable = disposal[i];
189193
callable.call(callable);
190194
}

‎packages/signals/src/signals.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ import {
6565
* Cannot be used inside `createTrackedEffect` or `onSettled` — return a
6666
* cleanup function from the callback body instead.
6767
*
68+
* Cleanups run in unwind order: an owner's children are disposed before its
69+
* own cleanups, and within one owner later registrations run before earlier
70+
* ones. In production a component body shares its enclosing owner, so
71+
* register cleanup before creating children when the order between them
72+
* matters.
73+
*
6874
* @example
6975
* ```ts
7076
* // Library shape: thread a resource's disposal into a *captured* owner

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,12 @@ describe("final returned cleanup runs at the effect's own disposal", () => {
648648
flush();
649649
dispose();
650650

651-
// Children unwind newest-first at their positions, then the owner's own
652-
// disposal list runs FIFO. Under the old parent-registration scheme the
653-
// effect cleanups landed in that FIFO list instead (before, A, B, after).
654-
expect(order).toEqual(["effect:B", "effect:A", "onCleanup:before", "onCleanup:after"]);
651+
// Full unwind (#3572): children (the effect nodes) unwind newest-first at
652+
// their positions, then the owner's own disposal list unwinds too —
653+
// later registrations before earlier ones. Under the old
654+
// parent-registration scheme the effect cleanups landed in the owner's
655+
// list instead, and that list ran FIFO (before, A, B, after).
656+
expect(order).toEqual(["effect:B", "effect:A", "onCleanup:after", "onCleanup:before"]);
655657
});
656658

657659
it("early individual disposal fires the cleanup once; outer disposal does not re-run it", () => {

‎packages/signals/tests/dist-artifacts.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,38 @@ describe("@solidjs/signals artifacts", () => {
168168
});
169169
});
170170

171+
describe("@solidjs/signals cleanup order per tier", () => {
172+
// #3572: cleanup order is unwind — children before the owner's own list,
173+
// and within one owner later registrations before earlier ones. The tiers
174+
// differ only in wiring and checks; the disposal walk is the same code,
175+
// and this pins that against each built artifact (the suite otherwise
176+
// sees only source).
177+
const cores: Record<keyof typeof TIERS, () => Promise<any>> = {
178+
prod: () => import("../dist/prod/index.js"),
179+
observe: () => import("../dist/observe/index.js"),
180+
dev: () => import("../dist/dev.js")
181+
};
182+
for (const tier of Object.keys(TIERS) as (keyof typeof TIERS)[]) {
183+
test(`${tier}: children first, then the owner's cleanups later-registered first`, async () => {
184+
const { createRoot, createEffect, onCleanup, flush } = await cores[tier]();
185+
const order: string[] = [];
186+
const dispose = createRoot((dispose: () => void) => {
187+
onCleanup(() => order.push("A"));
188+
onCleanup(() => order.push("B"));
189+
createRoot(() => onCleanup(() => order.push("C")));
190+
createEffect(
191+
() => {},
192+
() => () => order.push("D")
193+
);
194+
return dispose;
195+
});
196+
flush();
197+
dispose();
198+
expect(order).toEqual(["D", "C", "B", "A"]);
199+
});
200+
}
201+
});
202+
171203
describe("@solidjs/signals node literals per tier", () => {
172204
// Each node factory has two object literals — prod, and observe = prod plus
173205
// its diagnostic slots (`_name`; `_owner` on signals) — selected at build

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,33 @@ it("should clean up in reverse order", () => {
9898
expect(disposeParent).toHaveBeenCalledWith(3);
9999
});
100100

101+
it("cleanup order is unwind: children before owner, later registrations before earlier", () => {
102+
// #3572 / #1562. Disposal unwinds: an owner's children are torn down
103+
// before the owner's own cleanups, and within one owner the cleanups run
104+
// in reverse registration order (LIFO). Production component bodies share
105+
// their enclosing owner, so this is what makes a parent that registers
106+
// cleanup before rendering its children tear down after them — the same
107+
// order the dev tier's per-component owner gives.
108+
const order: string[] = [];
109+
110+
const dispose = createRoot(dispose => {
111+
onCleanup(() => order.push("A"));
112+
onCleanup(() => order.push("B"));
113+
createRoot(() => onCleanup(() => order.push("C")));
114+
createEffect(
115+
() => {},
116+
() => () => order.push("D")
117+
);
118+
return dispose;
119+
});
120+
flush();
121+
dispose();
122+
123+
// Children first, in their chain order (newest child first), then the
124+
// owner's own list unwound: B (registered last) before A.
125+
expect(order).toEqual(["D", "C", "B", "A"]);
126+
});
127+
101128
it("should dispose all roots", () => {
102129
const disposals: string[] = [];
103130

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ export function isDisposed(owner: Owner): boolean {
329329
return (owner as unknown as SSROwner)._disposed;
330330
}
331331

332+
/**
333+
* Server mirror of `onCleanup`. Cleanups run in unwind order: an owner's
334+
* children are disposed before its own cleanups, and within one owner later
335+
* registrations run before earlier ones (see `disposeOwner`).
336+
*/
332337
export function onCleanup(fn: Disposable): Disposable {
333338
const o = currentOwner;
334339
if (!o) return fn;
@@ -390,7 +395,8 @@ function unlinkOwner(node: SSROwner): void {
390395
* Tears down `owner` (optionally) and all of its descendants. Walks the
391396
* forward-only `_firstChild` -> `_nextSibling` chain, recursively disposing
392397
* each child with `self=true`, then runs the owner's own `_disposal` queue
393-
* and resets `_firstChild` / `_childCount`.
398+
* in unwind order (later registrations first) and resets `_firstChild` /
399+
* `_childCount`.
394400
*
395401
* `self=false` keeps `owner` itself alive (its `_disposed` flag stays clear,
396402
* future `runWithOwner(owner, ...)` keeps working) but tears down its
@@ -436,7 +442,9 @@ export function disposeOwner(owner: Owner, self: boolean = true): void {
436442
const d = node._disposal;
437443
if (d) {
438444
if (Array.isArray(d)) {
439-
for (let i = 0, len = d.length; i < len; i++) d[i]();
445+
// Unwind order, mirroring the client `runDisposal` (#3572): later
446+
// registrations run before earlier ones.
447+
for (let i = d.length - 1; i >= 0; i--) d[i]();
440448
} else {
441449
d();
442450
}

‎packages/solid/test/dev.spec.ts‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
createMemo,
88
createEffect,
99
getOwner,
10+
onCleanup,
1011
flush,
1112
DEV,
1213
$DEVCOMP,
@@ -114,10 +115,16 @@ describe("effect cleanup ordering through the dev component wrapper", () => {
114115
// (unwind order). The transparent observedComponent root adds a nesting level at
115116
// the same position, so DFS unwind order relative to siblings is identical
116117
// with and without the wrapper — dev matches prod for the idiomatic 2.0
117-
// cleanup form. (Raw onCleanup in a component body still lands on the
118-
// wrapper and is a known dev divergence — see #1561/#2710 assessment.)
119-
// Disposal order is not a documented guarantee; this pins implementation
120-
// behavior so changes to it are deliberate.
118+
// cleanup form.
119+
//
120+
// Disposal order IS documented (#3572): children before the owner's own
121+
// cleanups, and within one owner later registrations before earlier ones.
122+
// Raw `onCleanup` in a component body lands on the wrapper in dev and on
123+
// the enclosing owner in prod; the unwind rule makes the two agree when
124+
// the parent registers BEFORE creating its children (the tests below).
125+
// Two shapes remain documented divergences: a parent registering
126+
// `onCleanup` AFTER creating its children, and a parent effect-returned
127+
// cleanup vs a child body's `onCleanup` — see #1561/#2710.
121128
test("effect-returned cleanups order the same with and without the wrapper", () => {
122129
const run = (wrap: boolean) => {
123130
const order: string[] = [];
@@ -156,6 +163,58 @@ describe("effect cleanup ordering through the dev component wrapper", () => {
156163
// Children unwind newest-first at their structural positions.
157164
expect(dev).toEqual(["sibling:after", "child:B", "child:A", "sibling:before"]);
158165
});
166+
167+
// The #3572 shapes: raw `onCleanup` in component bodies where the parent
168+
// registers before rendering its children. `wrap=true` is the dev tier
169+
// (each component gets its own transparent owner); `wrap=false` is the
170+
// flattened prod shape (every body shares the enclosing owner). Both must
171+
// unwind children-first.
172+
const unwind = (build: (call: (C: () => any) => any, order: string[]) => void) => {
173+
const run = (wrap: boolean) => {
174+
const order: string[] = [];
175+
const call = (C: () => any) => (wrap ? createComponent(C, {}) : C());
176+
createRoot(dispose => {
177+
build(call, order);
178+
flush();
179+
dispose();
180+
});
181+
flush();
182+
return order;
183+
};
184+
const wrapped = run(true);
185+
const flattened = run(false);
186+
expect(flattened).toEqual(wrapped);
187+
return wrapped;
188+
};
189+
190+
test("parent registers onCleanup then renders a child: child tears down first (wrapped and flattened)", () => {
191+
const order = unwind((call, order) => {
192+
const Child = () => (onCleanup(() => order.push("child")), null);
193+
const Parent = () => (onCleanup(() => order.push("parent")), call(Child));
194+
call(Parent);
195+
});
196+
expect(order).toEqual(["child", "parent"]);
197+
});
198+
199+
test("parent registers onCleanup then renders siblings: siblings unwind, then parent", () => {
200+
const order = unwind((call, order) => {
201+
const A = () => (onCleanup(() => order.push("A")), null);
202+
const B = () => (onCleanup(() => order.push("B")), null);
203+
const Parent = () => (onCleanup(() => order.push("parent")), [call(A), call(B)]);
204+
call(Parent);
205+
});
206+
expect(order).toEqual(["B", "A", "parent"]);
207+
});
208+
209+
test("3-deep chain, each level registering before its child: innermost first", () => {
210+
const order = unwind((call, order) => {
211+
const Leaf = () => (onCleanup(() => order.push("leaf")), null);
212+
const Mid = () => (onCleanup(() => order.push("mid")), call(Leaf));
213+
const Top = () => (onCleanup(() => order.push("top")), call(Mid));
214+
call(Top);
215+
});
216+
expect(order).toEqual(["leaf", "mid", "top"]);
217+
});
159218
});
160219

161220
describe("$DEVCOMP marking", () => {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,24 @@ describe("Server owner tree", () => {
639639
);
640640
});
641641

642+
test("cleanup order is unwind: children before owner, later registrations before earlier (#3572)", () => {
643+
// Mirrors the client ruling: `disposeOwner` tears down the child chain,
644+
// then the owner's own list in reverse registration order.
645+
const order: string[] = [];
646+
let dispose!: () => void;
647+
createRoot(
648+
d => {
649+
dispose = d;
650+
onCleanup(() => order.push("A"));
651+
onCleanup(() => order.push("B"));
652+
createRoot(() => onCleanup(() => order.push("C")));
653+
},
654+
{ id: "test" }
655+
);
656+
dispose();
657+
expect(order).toEqual(["C", "B", "A"]);
658+
});
659+
642660
test("disposing a parent does not dispose an owner recycled into another root", () => {
643661
let disposeParent!: () => void;
644662
let disposeChild!: () => void;

0 commit comments

Comments
 (0)