|
| 1 | +/** |
| 2 | + * #3140: a `_transition` stamp must not outlive its transaction. |
| 3 | + * |
| 4 | + * `commitPendingNode` committed the value but left `_transition` pointing at |
| 5 | + * the finished transaction (optimistic stamps were cleared at completion; |
| 6 | + * pending stamps were not). `setSignal` re-opens a node's stamped transaction |
| 7 | + * BEFORE the value-equal bail, so a later no-op write resurrected the corpse: |
| 8 | + * the drain loop saw a live transition again, completed it, and the next |
| 9 | + * finalize pass (a Loading boundary's `_checkSources` rewrites its flag with |
| 10 | + * the same value on every pass) revived it again — the dev build threw the |
| 11 | + * flush loop guard, production hung the tab. |
| 12 | + * |
| 13 | + * The wild stamping race did not reduce to a minimal reproduction (see the |
| 14 | + * issue), so each layer is pinned directly: |
| 15 | + * - commit clears the stamp (symmetry with resolveOptimisticNodes); |
| 16 | + * - a write finding a dead stamp — however it survived — must not |
| 17 | + * re-activate the finished transaction. |
| 18 | + */ |
| 19 | +import { describe, expect, it } from "vitest"; |
| 20 | +import { action, createSignal, flush } from "../src/index.js"; |
| 21 | +import { createRoot } from "../src/index.js"; |
| 22 | +import { setSignal, signal, type Signal } from "../src/core/index.js"; |
| 23 | +import { activeTransition, type Transition } from "../src/core/scheduler.js"; |
| 24 | + |
| 25 | +function deferred<T = void>() { |
| 26 | + let resolve!: (v: T) => void; |
| 27 | + const promise = new Promise<T>(r => (resolve = r)); |
| 28 | + return { promise, resolve }; |
| 29 | +} |
| 30 | + |
| 31 | +/** Runs an action to completion and returns its (now finished) transaction. */ |
| 32 | +async function completedTransition(): Promise<Transition> { |
| 33 | + const gate = deferred(); |
| 34 | + let captured: Transition | null = null; |
| 35 | + let start!: () => Promise<unknown>; |
| 36 | + createRoot(() => { |
| 37 | + start = action(function* () { |
| 38 | + captured = activeTransition; |
| 39 | + yield gate.promise; |
| 40 | + }); |
| 41 | + }); |
| 42 | + const acting = start(); |
| 43 | + flush(); |
| 44 | + gate.resolve(); |
| 45 | + await acting; |
| 46 | + await new Promise(r => setTimeout(r, 0)); |
| 47 | + flush(); |
| 48 | + expect(captured).not.toBeNull(); |
| 49 | + expect((captured as unknown as Transition)._done).toBe(true); |
| 50 | + return captured as unknown as Transition; |
| 51 | +} |
| 52 | + |
| 53 | +describe("#3140: completed-transaction stamps", () => { |
| 54 | + it("commit clears the pending stamp", async () => { |
| 55 | + const gate = deferred(); |
| 56 | + let start!: () => Promise<unknown>; |
| 57 | + createRoot(() => { |
| 58 | + start = action(function* () { |
| 59 | + yield gate.promise; |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + const node = signal(1) as Signal<number>; |
| 64 | + // Stage the write in the ambient batch, then open the transaction in the |
| 65 | + // same unflushed window: initTransition's batch adoption stamps every |
| 66 | + // staged node with it. |
| 67 | + setSignal(node, 2); |
| 68 | + const acting = start(); |
| 69 | + expect(node._transition).not.toBeNull(); |
| 70 | + |
| 71 | + // Park (incomplete: the action is awaiting), then complete. |
| 72 | + flush(); |
| 73 | + gate.resolve(); |
| 74 | + await acting; |
| 75 | + await new Promise(r => setTimeout(r, 0)); |
| 76 | + flush(); |
| 77 | + |
| 78 | + expect(node._value).toBe(2); |
| 79 | + // Pre-fix the stamp survived the commit, pointing at a done transaction. |
| 80 | + expect(node._transition).toBeNull(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("a value-equal write to a dead-stamped node does not resurrect the transaction", async () => { |
| 84 | + const dead = await completedTransition(); |
| 85 | + |
| 86 | + // However a dead stamp survives (the wild race did not minimize), the |
| 87 | + // write path must refuse the corpse rather than re-activate it. |
| 88 | + const node = signal(5) as Signal<number>; |
| 89 | + node._transition = dead; |
| 90 | + |
| 91 | + setSignal(node, 5); // value-equal: bails after the transition re-open ran |
| 92 | + // Pre-fix: activeTransition === dead here, and every drain-loop pass that |
| 93 | + // rewrote any stamped flag re-armed it — the infinite flush loop. |
| 94 | + expect(activeTransition).toBeNull(); |
| 95 | + flush(); |
| 96 | + expect(activeTransition).toBeNull(); |
| 97 | + |
| 98 | + // A real write is routed to the ambient batch, not the corpse. |
| 99 | + const [, setTick] = createSignal(0); |
| 100 | + setSignal(node, 6); |
| 101 | + setTick(1); |
| 102 | + expect(activeTransition).toBeNull(); |
| 103 | + flush(); |
| 104 | + expect(node._value).toBe(6); |
| 105 | + }); |
| 106 | +}); |
0 commit comments