Skip to content

Commit 788277a

Browse files
brenelzclaude
andcommitted
fix(signals): a zombie keeps REACTIVE_ZOMBIE through its re-run (#3543)
recompute() and updateIfNecessary() rewrote `_flags` wholesale, dropping REACTIVE_ZOMBIE from a deferred child that re-ran while its owner's commit waited. disposeChildren() then took the ordinary parent-chain splice for a node sitting on `_pendingFirstChild` and wrote `parent._firstChild = next`, detaching the replacement child: still subscribed, unreachable by cleanup. Re-pins the #3463 frame (6000 -> 5000): it relied on the re-run zombie being judged as a live reader. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ead76f4 commit 788277a

4 files changed

Lines changed: 106 additions & 4 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 deferred child keeps `REACTIVE_ZOMBIE` when it re-runs (#3543). `recompute()` and `updateIfNecessary()` rewrote `_flags` wholesale, so a zombie that re-ran while its owner's commit waited (an unrelated parked action is enough) was later disposed as an ordinary child: the parent-chain splice wrote `parent._firstChild = next` and detached the replacement, which stayed subscribed where no owner cleanup could reach it. A compiled `<Show when={n() > 0 && n() < 2}>` leaked one nested memo per update and eventually tripped `HUGE_FAN_OUT`. The flag also routes heap queues and the transaction verdict, so a re-run zombie is again judged as a zombie: the #3463 pin's removal commits when its action ends (5000) rather than waiting on a refetch only the zombie reads (6000).

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

Lines changed: 10 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,11 @@ 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 survives every `_flags` rewrite (#3543): a zombie that
325+
// re-runs while its owner's commit waits is still on the owner's
326+
// `_pendingFirstChild`, and disposal, heap routing and the verdict all key
327+
// off the flag. Dropped, its dispose spliced the owner's live chain.
328+
el._flags = REACTIVE_RECOMPUTING_DEPS | (el._flags & REACTIVE_ZOMBIE);
324329
el._time = clock;
325330
let value = el._pendingValue === NOT_PENDING ? el._value : el._pendingValue;
326331
let oldHeight = el._height;
@@ -488,7 +493,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
488493
// (markNode(c) in read()) marks the running node as part of ordinary
489494
// bookkeeping, and those marks are correctly discarded here.
490495
missedWake = (el._flags & REACTIVE_MISSED_WAKE) !== 0;
491-
el._flags = REACTIVE_NONE | (create ? el._flags & REACTIVE_SNAPSHOT_STALE : 0);
496+
el._flags = el._flags & (create ? REACTIVE_SNAPSHOT_STALE : REACTIVE_ZOMBIE);
492497
context = oldcontext;
493498
}
494499
// The cast re-widens: TS narrowed `stagedEntry` to `null` at the reset
@@ -871,7 +876,9 @@ function updateIfNecessary(el: Computed<unknown>): void {
871876
recompute(el);
872877
}
873878

874-
el._flags = el._flags & (REACTIVE_SNAPSHOT_STALE | REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT);
879+
el._flags =
880+
el._flags &
881+
(REACTIVE_SNAPSHOT_STALE | REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT | REACTIVE_ZOMBIE);
875882
}
876883

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

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ describe("a held lane from the outside", () => {
344344
// cannot commit while the action runs, so it stays on screen. It was treated
345345
// as dead for the hold and the lane revealed `Value: 1` beside its `Details:
346346
// 0`. A zombie blocks unless the judgment IS the commit that disposes it.
347+
// The action's end (5000) is that judgment: the refetch its `setCount(0)`
348+
// starts has no reader but the zombie, so the removal commits there. (The
349+
// pin read 6000 while a zombie's re-run dropped REACTIVE_ZOMBIE and it was
350+
// judged as a live reader, #3543.)
347351
it("#3463 a reader whose removal is staged keeps holding the lane while it is visible", async () => {
348352
reset();
349353
const log: string[] = [];
@@ -387,7 +391,7 @@ describe("a held lane from the outside", () => {
387391
"0: Show: true | Value: 0",
388392
"1000: Details: 0",
389393
"4000: Details: 1 | Value: 1",
390-
"6000: Details gone | Show: false | Value: 0"
394+
"5000: Details gone | Show: false | Value: 0"
391395
]);
392396
});
393397

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* #3543: a deferred (zombie) child that recomputes while an unrelated action
3+
* is parked loses REACTIVE_ZOMBIE — recompute()/updateIfNecessary() rewrite
4+
* `_flags` wholesale. At the owner's commit `disposeChildren(child, true)`
5+
* then takes the ordinary parent-chain splice and, with no `_prevSibling`,
6+
* writes `parent._firstChild = next`, detaching the REPLACEMENT child. The
7+
* replacement stays subscribed but no owner cleanup can reach it again.
8+
*
9+
* Shape is the compiler's `<Show when={n() > 0 && n() < 2}>`: a condition
10+
* memo whose getter creates a nested memo on every run.
11+
*/
12+
import { describe, expect, it } from "vitest";
13+
import {
14+
action,
15+
createMemo,
16+
createRenderEffect,
17+
createRoot,
18+
createSignal,
19+
flush
20+
} from "../src/index.js";
21+
22+
function setup() {
23+
const [n, setN] = createSignal(0);
24+
const [a, setA] = createSignal(0);
25+
let release!: () => void;
26+
const gate = new Promise<void>(r => (release = r));
27+
const run = action(async function* () {
28+
setA(v => v + 1);
29+
yield gate;
30+
});
31+
let disposeRoot!: () => void;
32+
// runs of the nested memo: every live copy subscribed to `n` runs per write
33+
let runs = 0;
34+
createRoot(d => {
35+
disposeRoot = d;
36+
const cond = createMemo(() => {
37+
const c = createMemo(() => (runs++, n() > 0));
38+
return c() && n() < 2;
39+
});
40+
createRenderEffect(
41+
() => a(),
42+
() => {}
43+
);
44+
createRenderEffect(
45+
() => cond(),
46+
() => {}
47+
);
48+
});
49+
flush();
50+
const toggle = (times: number) => {
51+
for (let i = 0; i < times; i++) {
52+
setN(i % 2 ? 0 : 1);
53+
flush();
54+
}
55+
};
56+
// nested-memo runs caused by one more write
57+
const runsPerWrite = () => {
58+
const before = runs;
59+
setN(v => (v ? 0 : 1));
60+
flush();
61+
return runs - before;
62+
};
63+
return { toggle, runsPerWrite, run, release, disposeRoot };
64+
}
65+
66+
describe("#3543 zombie child recompute keeps its zombie flag", () => {
67+
it("control: no pending action keeps subscribers bounded", () => {
68+
const { toggle, runsPerWrite } = setup();
69+
toggle(40);
70+
expect(runsPerWrite()).toBeLessThanOrEqual(2);
71+
});
72+
73+
it("pending unrelated action keeps subscribers bounded, and after it finishes", async () => {
74+
const { toggle, runsPerWrite, run, release, disposeRoot } = setup();
75+
const done = run();
76+
flush();
77+
toggle(40);
78+
expect(runsPerWrite()).toBeLessThanOrEqual(2);
79+
release();
80+
await done;
81+
flush();
82+
expect(runsPerWrite()).toBeLessThanOrEqual(2);
83+
disposeRoot();
84+
expect(runsPerWrite()).toBe(0);
85+
});
86+
});

0 commit comments

Comments
 (0)