|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { createMemo, createRenderEffect, createRoot, createSignal, flush } from "../src/index.js"; |
| 3 | +import { REACTIVE_IN_HEAP, REACTIVE_IN_HEAP_HEIGHT } from "../src/core/constants.js"; |
| 4 | +import { dirtyQueue, zombieQueue } from "../src/core/scheduler.js"; |
| 5 | +import type { Computed } from "../src/core/types.js"; |
| 6 | + |
| 7 | +/** A chain of pass-through memos, standing in for a deep derived-data pipeline. */ |
| 8 | +function chain<T>(src: () => T, n: number): () => T { |
| 9 | + let cur = src; |
| 10 | + for (let i = 0; i < n; i++) { |
| 11 | + const prev = cur; |
| 12 | + cur = createMemo(() => prev()); |
| 13 | + } |
| 14 | + return cur; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Every node physically linked in a heap must carry an in-heap flag, because |
| 19 | + * `runHeap` makes no progress on a bucket head that `deleteFromHeap` refuses |
| 20 | + * to unlink (it early-returns on the flag check), and every `deleteFromHeap` |
| 21 | + * call site picks the queue from the node's flags. |
| 22 | + */ |
| 23 | +function entriesWithoutInHeapFlags(heap: typeof dirtyQueue): number[] { |
| 24 | + const corrupted: number[] = []; |
| 25 | + for (let height = 0; height < heap._heap.length; height++) { |
| 26 | + for ( |
| 27 | + let el: Computed<unknown> | undefined = heap._heap[height]; |
| 28 | + el !== undefined; |
| 29 | + el = el._nextHeap |
| 30 | + ) { |
| 31 | + if (!(el._flags & (REACTIVE_IN_HEAP | REACTIVE_IN_HEAP_HEIGHT))) { |
| 32 | + corrupted.push(el._flags); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + return corrupted; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * @see https://github.com/solidjs/solid/issues/2759 |
| 41 | + */ |
| 42 | +it("disposing a subtree with a stale height-adjust entry does not corrupt the dirty queue", () => { |
| 43 | + const [open, setOpen] = createSignal(true); |
| 44 | + let setRows!: (v: number[] | null) => void; |
| 45 | + let view: () => unknown = () => null; |
| 46 | + let dispose!: () => void; |
| 47 | + |
| 48 | + createRoot(d => { |
| 49 | + dispose = d; |
| 50 | + // keeps the unmount recompute at a low height, so the flush that disposes |
| 51 | + // the subtree never advances the cursor up to the stale entry's bucket |
| 52 | + const gate = chain(() => open(), 4); |
| 53 | + const appView = createMemo(() => { |
| 54 | + if (!gate()) return null; |
| 55 | + |
| 56 | + // ~<Inner>: an async data source that lands after mount |
| 57 | + const [rows, set] = createSignal<number[] | null>(null); |
| 58 | + setRows = set; |
| 59 | + |
| 60 | + // parks the "condition value" memo at a height above everything the |
| 61 | + // unmount flush will visit |
| 62 | + const tall = chain( |
| 63 | + createMemo(() => 1), |
| 64 | + 12 |
| 65 | + ); |
| 66 | + const deepA = chain( |
| 67 | + createMemo(() => 1), |
| 68 | + 20 |
| 69 | + ); |
| 70 | + const deepB = chain(() => rows(), 45); |
| 71 | + |
| 72 | + // mirrors <Show when={expr}>: the compiler wraps the `when` expression |
| 73 | + // in a memo that is created lazily inside (and owned by) Show's |
| 74 | + // "condition value" memo, so the parent's height never tracks the |
| 75 | + // child's height |
| 76 | + let expr: (() => unknown) | undefined; |
| 77 | + const conditionValue = createMemo(() => { |
| 78 | + tall(); |
| 79 | + expr ??= createMemo(() => (rows() === null ? deepA() : deepB()) && true); |
| 80 | + return expr(); |
| 81 | + }); |
| 82 | + const condition = createMemo(() => conditionValue(), { |
| 83 | + equals: (a, b) => !a === !b |
| 84 | + }); |
| 85 | + const value = createMemo(() => (condition() ? "inner ready" : null)); |
| 86 | + return value(); |
| 87 | + }); |
| 88 | + view = appView; |
| 89 | + createRenderEffect(appView, () => undefined); |
| 90 | + }); |
| 91 | + |
| 92 | + flush(); |
| 93 | + expect(view()).toBe("inner ready"); |
| 94 | + |
| 95 | + // The async data lands: the condition expression memo recomputes, its |
| 96 | + // height grows past the already-advanced cursor while its value stays the |
| 97 | + // same, so its subscriber is re-inserted for height adjustment at a stale |
| 98 | + // height the cursor has already passed and survives the flush linked in |
| 99 | + // `dirtyQueue` with only `REACTIVE_IN_HEAP_HEIGHT` set. |
| 100 | + setRows([1, 2, 3]); |
| 101 | + flush(); |
| 102 | + |
| 103 | + // Unmount: `markDisposal` zombifies the subtree and the pending disposal |
| 104 | + // commits. The height-only entry must migrate queues together with its |
| 105 | + // zombie flag, otherwise the commit deletes it from the queue its flag |
| 106 | + // names (`zombieQueue`) while it is physically linked in `dirtyQueue`. |
| 107 | + setOpen(false); |
| 108 | + flush(); |
| 109 | + |
| 110 | + expect(entriesWithoutInHeapFlags(dirtyQueue)).toEqual([]); |
| 111 | + expect(entriesWithoutInHeapFlags(zombieQueue)).toEqual([]); |
| 112 | + |
| 113 | + // Remount: on corrupted state this flush livelocks in `runHeap`, pinning |
| 114 | + // the thread forever. |
| 115 | + setOpen(true); |
| 116 | + flush(); |
| 117 | + setRows([1, 2, 3]); |
| 118 | + flush(); |
| 119 | + expect(view()).toBe("inner ready"); |
| 120 | + |
| 121 | + dispose(); |
| 122 | + flush(); |
| 123 | +}); |
0 commit comments