|
| 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 | +}); |
0 commit comments