Skip to content

Commit ebc1b03

Browse files
ryansolidClaude via Cursorcursoragent
committed
fix(signals): a zombie that recomputes stays a zombie (#3543)
`recompute` and `updateIfNecessary` rewrote `_flags` wholesale and dropped REACTIVE_ZOMBIE — the flag that says a node sits on its owner's deferred disposal chain (`_pendingFirstChild`) and that `disposeChildren` keys its parent-chain splice off. Zombies rerun for mainline writes while any transaction is parked (#3463), so an owner that recreates a child each pass — a compiled `<Show when={n() > 0 && n() < 2}>` condition, where the owner runs before its own child (a created child doesn't lift the owner's height) and finds it still DIRTY|IN_HEAP — had that child rerun de-flagged. At the owner's commit the child spliced itself out of the LIVE chain, orphaning the current child, which stayed subscribed and recomputing forever: one leaked node per update until HUGE_FAN_OUT. The flag is position, not per-pass scheduling state: carry it through the three wipes. `queueFor()` picks the right heap for a rerun zombie as a consequence (the #2759 class). Pin change in lane-outside-view.test.ts (#3463): a zombie whose removal a transaction stages no longer holds that transaction's commit after it has rerun — its say was always meant to be moot for the verdict that disposes it; the extra second of hold was this bug (a de-flagged zombie counted as a live reader). Tests: the compiled shape in core primitives (subscriber count, live child count, run-count bound) and GabbeV's playground through real <Show>/JSX. Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent ead76f4 commit ebc1b03

5 files changed

Lines changed: 208 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
A zombie that recomputes stays a zombie (#3543). `recompute` and `updateIfNecessary` rewrote `_flags` wholesale and dropped `REACTIVE_ZOMBIE`, the flag that says a node sits on its owner's deferred-disposal chain. While any transaction was parked, the scheduler reruns zombies for mainline writes (#3463), so an owner that recreates a child each pass — a compiled `<Show when={a() && b()}>` condition — had its previous child rerun de-flagged; at the owner's commit `disposeChildren` then spliced that child out of the _live_ chain instead of the pending one, orphaning the current child. The orphan stayed subscribed and recomputing forever: one leaked node per update, until `HUGE_FAN_OUT`. The flag now survives every per-pass wipe.
6+
7+
A consequence pinned in `lane-outside-view.test.ts`: a zombie whose removal is staged by a transaction no longer holds that transaction's commit after it reruns — its say was always meant to be moot for the verdict that disposes it, and the extra hold was this bug.

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
REACTIVE_REASK,
5454
REACTIVE_RECOMPUTING_DEPS,
5555
REACTIVE_SNAPSHOT_STALE,
56+
REACTIVE_ZOMBIE,
5657
STATUS_ERROR,
5758
STATUS_PENDING,
5859
STATUS_UNINITIALIZED,
@@ -320,7 +321,14 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
320321
context = el;
321322
el._depsTail = null;
322323
el._depGen++;
323-
el._flags = REACTIVE_RECOMPUTING_DEPS;
324+
// REACTIVE_ZOMBIE is position, not scheduling state: it says the node sits
325+
// on its owner's `_pendingFirstChild` chain, and `disposeChildren` keys its
326+
// parent-chain splice off it. A zombie reruns for mainline writes until the
327+
// commit that disposes it (#3463), so the per-pass wipe here — and in the
328+
// finally below and in updateIfNecessary — must carry it (#3543): a
329+
// de-flagged zombie spliced itself out of the LIVE chain at disposal,
330+
// orphaning the owner's current child, which stayed subscribed forever.
331+
el._flags = REACTIVE_RECOMPUTING_DEPS | (el._flags & REACTIVE_ZOMBIE);
324332
el._time = clock;
325333
let value = el._pendingValue === NOT_PENDING ? el._value : el._pendingValue;
326334
let oldHeight = el._height;
@@ -488,7 +496,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
488496
// (markNode(c) in read()) marks the running node as part of ordinary
489497
// bookkeeping, and those marks are correctly discarded here.
490498
missedWake = (el._flags & REACTIVE_MISSED_WAKE) !== 0;
491-
el._flags = REACTIVE_NONE | (create ? el._flags & REACTIVE_SNAPSHOT_STALE : 0);
499+
el._flags = (el._flags & REACTIVE_ZOMBIE) | (create ? el._flags & REACTIVE_SNAPSHOT_STALE : 0);
492500
context = oldcontext;
493501
}
494502
// The cast re-widens: TS narrowed `stagedEntry` to `null` at the reset
@@ -871,7 +879,9 @@ function updateIfNecessary(el: Computed<unknown>): void {
871879
recompute(el);
872880
}
873881

874-
el._flags = el._flags & (REACTIVE_SNAPSHOT_STALE | REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT);
882+
el._flags =
883+
el._flags &
884+
(REACTIVE_SNAPSHOT_STALE | REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT | REACTIVE_ZOMBIE);
875885
}
876886

877887
export function computed<T>(fn: (prev?: T) => T | PromiseLike<T> | AsyncIterable<T>): Computed<T>;

‎packages/signals/tests/lane-outside-view.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,17 @@ describe("a held lane from the outside", () => {
383383
setShow(false);
384384
await settle();
385385
await advanceTo(12000);
386+
// At 5000 the action's body is done and the only pending reader is the
387+
// zombie itself — its refetch of `count=0` is moot for the verdict of the
388+
// transaction that disposes it, so the removal commits at once. (This
389+
// frame read 6000 while a zombie's rerun dropped REACTIVE_ZOMBIE, #3543:
390+
// the 4000 rerun made `Details` a live reader that held the commit for a
391+
// fetch nothing would display.)
386392
expect(frames(log, when)).toEqual([
387393
"0: Show: true | Value: 0",
388394
"1000: Details: 0",
389395
"4000: Details: 1 | Value: 1",
390-
"6000: Details gone | Show: false | Value: 0"
396+
"5000: Details gone | Show: false | Value: 0"
391397
]);
392398
});
393399

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// #3543: a zombie that recomputes must stay a zombie.
2+
//
3+
// A recompute defers the previous pass's children as zombies on the owner's
4+
// `_pendingFirstChild` (REACTIVE_ZOMBIE) until the owner's commit disposes
5+
// them; the replacement children take `_firstChild`. While any transaction is
6+
// parked, the scheduler reruns zombies for mainline writes (#3463: a zombie
7+
// renders until the commit that disposes it), and `recompute` /
8+
// `updateIfNecessary` rewrote `_flags` wholesale — dropping REACTIVE_ZOMBIE.
9+
// `disposeChildren` keys its parent-chain splice off that flag, so the
10+
// de-flagged zombie spliced itself out of the LIVE chain: with `_prevSibling`
11+
// null that is `parent._firstChild = null`, orphaning the replacement, which
12+
// stays subscribed to its sources and recomputes forever. One leaked node per
13+
// owner rerun; the reduced case reached HUGE_FAN_OUT.
14+
//
15+
// Shape (the compiled `<Show when={n() > 0 && n() < 2}>` condition): an
16+
// owner memo that creates a nested memo reading `n` on every pass, a parked
17+
// action writing an unrelated signal a sibling reads, and `n` alternating.
18+
19+
import {
20+
DEV,
21+
action,
22+
createMemo,
23+
createRenderEffect,
24+
createRoot,
25+
createSignal,
26+
flush,
27+
getOwner
28+
} from "../src/index.js";
29+
30+
afterEach(() => flush());
31+
32+
function setup() {
33+
const state = { innerRuns: 0 };
34+
let nNode!: any;
35+
let condOwner!: any;
36+
let setN!: (v: number) => void;
37+
let resolveSave: () => void = () => {};
38+
let save!: () => Promise<void>;
39+
40+
const dispose = createRoot(d => {
41+
const [n, _setN] = createSignal(0);
42+
setN = _setN;
43+
nNode = DEV!.getSignals(getOwner()!)[0];
44+
const [saved, setSaved] = createSignal(false);
45+
// the action writes before it parks, so the write is the transaction's
46+
save = action(function* () {
47+
setSaved(true);
48+
yield new Promise<void>(r => (resolveSave = r));
49+
}) as () => Promise<void>;
50+
// sibling binding reading the action-written signal
51+
createRenderEffect(saved, () => {});
52+
// <Show when={n() > 0 && n() < 2}> compiles the getter to
53+
// `memo(() => n() > 0)() && n() < 2`: Show's condition memo creates a
54+
// nested sync memo on `n` and reads `n` directly too
55+
const conditionValue = createMemo(() => {
56+
condOwner = getOwner();
57+
const inner = createMemo(
58+
() => {
59+
state.innerRuns++;
60+
return n() > 0;
61+
},
62+
{ sync: true }
63+
);
64+
return inner() && n() < 2;
65+
});
66+
const condition = createMemo(conditionValue, { equals: (a, b) => !a === !b, sync: true });
67+
const show = createMemo(() => (condition() ? "Visible" : undefined));
68+
createRenderEffect(show, () => {});
69+
return d;
70+
});
71+
flush();
72+
return {
73+
state,
74+
setN,
75+
save,
76+
resolve: () => resolveSave(),
77+
observers: () => DEV!.getObservers(nNode).length,
78+
liveChildren: () => DEV!.getChildren(condOwner).length,
79+
dispose
80+
};
81+
}
82+
83+
function alternate(setN: (v: number) => void, times: number) {
84+
for (let i = 1; i <= times; i++) {
85+
setN(i % 2);
86+
flush();
87+
}
88+
}
89+
90+
it("keeps the subscriber count bounded while an unrelated action is parked", async () => {
91+
const { state, setN, save, resolve, observers, liveChildren, dispose } = setup();
92+
93+
// control: no transaction — one nested memo replaces the last each pass
94+
// (`&&` short-circuits at n=0, so the count depends on parity: measure it)
95+
const runsBefore = state.innerRuns;
96+
alternate(setN, 40);
97+
const control = observers();
98+
const controlRuns = state.innerRuns - runsBefore;
99+
expect(liveChildren()).toBe(1);
100+
101+
// park an unrelated action, then keep alternating
102+
const p = save();
103+
flush();
104+
const before = state.innerRuns;
105+
alternate(setN, 40);
106+
expect(observers()).toBe(control);
107+
expect(liveChildren()).toBe(1);
108+
// at most one extra run per pass — the zombie rerun the parked transaction
109+
// buys (#3463); an orphan would add a run per pass for every leaked node
110+
expect(state.innerRuns - before).toBeLessThanOrEqual(controlRuns + 40);
111+
112+
// and after the action lands
113+
resolve();
114+
await p;
115+
flush();
116+
alternate(setN, 40);
117+
expect(observers()).toBe(control);
118+
expect(liveChildren()).toBe(1);
119+
120+
dispose();
121+
expect(observers()).toBe(0);
122+
});
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
// #3543: a <Show> condition's nested memo leaks one subscription per update
6+
// while an unrelated action is parked, and the leak survives the action.
7+
import { expect, test } from "vitest";
8+
import { DEV, Show, action, createSignal, flush, getOwner } from "solid-js";
9+
import { render } from "../src/index.js";
10+
11+
test("Show condition subscriptions stay bounded while an unrelated action is parked", async () => {
12+
const div = document.createElement("div");
13+
let setN!: (v: number) => void;
14+
let subscribers!: () => number;
15+
let hold!: () => Promise<void>;
16+
let release: () => void = () => {};
17+
18+
const dispose = render(() => {
19+
const [n, _setN] = createSignal(0, { name: "counter" });
20+
setN = _setN;
21+
const [held, setHeld] = createSignal(false);
22+
const counter = DEV!.getSignals(getOwner()!).find((s: any) => s._name === "counter")!;
23+
subscribers = () => DEV!.getObservers(counter).length;
24+
hold = action(function* () {
25+
setHeld(true);
26+
yield new Promise<void>(r => (release = r));
27+
}) as () => Promise<void>;
28+
return (
29+
<>
30+
<span>{String(held())}</span>
31+
<Show when={n() > 0 && n() < 2}>Visible</Show>
32+
</>
33+
);
34+
}, div);
35+
flush();
36+
37+
const alternate = (times: number) => {
38+
for (let i = 0; i < times; i++) {
39+
setN(i % 2);
40+
flush();
41+
}
42+
};
43+
44+
alternate(40);
45+
const control = subscribers();
46+
47+
const completion = hold();
48+
flush();
49+
alternate(40);
50+
expect(subscribers()).toBe(control);
51+
52+
release();
53+
await completion;
54+
flush();
55+
alternate(40);
56+
expect(subscribers()).toBe(control);
57+
58+
dispose();
59+
});

0 commit comments

Comments
 (0)