diff --git a/.changeset/held-equal-rewrite-no-flush.md b/.changeset/held-equal-rewrite-no-flush.md new file mode 100644 index 000000000..fcfb36a6b --- /dev/null +++ b/.changeset/held-equal-rewrite-no-flush.md @@ -0,0 +1,5 @@ +--- +"@solidjs/signals": patch +--- + +Rewriting a held signal with its current value outside a flush no longer schedules a flush. An observer that rewrote its state after every flush (such as `DevToolbar` during an action that calls `refresh`) kept the microtask queue busy, so the action's pending request never resolved and the tab stopped responding (#5038). diff --git a/packages/signals/docs/RULES-INDEX.md b/packages/signals/docs/RULES-INDEX.md index 799d96c23..ab620b9f0 100644 --- a/packages/signals/docs/RULES-INDEX.md +++ b/packages/signals/docs/RULES-INDEX.md @@ -69,10 +69,10 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul | D-1 | live | `docs/RULES-DERIVED.md:13` | async.ts×1 core.ts×7 scheduler.ts×1 seat.ts×10 | fallback-born-creator-4428.test.ts×1 | Reading a held value joins its hold | | D-2 | live | `docs/RULES-DERIVED.md:21` | lanes.ts×1 | adoption-lazy-child-pairing-3712.test.ts×1 boundary-not-born-held-3540.test.ts×1 fallback-fn-latest-runaway-4467.test.ts×1 latest-unread-identity-memo-4474.test.ts×1 midflight-memo-over-reask-4504.test.ts×2 | A new memo joins a hold by reading it, not by being created | | D-3 | live | `docs/RULES-DERIVED.md:25` | constants.ts×2 scheduler.ts×1 seat.ts×2 | boundary-mounts-separate-flushes-4283.test.ts×1 derived-store-latest-seed-4355.test.ts×1 errored-recovery-wedge-4403-4404-4405.test.ts×2 | A render effect is a leaf: it shows the screen and re-derives at the commit | -| D-4 | live | `docs/RULES-DERIVED.md:33` | constants.ts×1 seat.ts×3 | boundary-mounts-separate-flushes-4283.test.ts×1 verdict-seat-merged-lane-4427.test.ts×1 | Membership in a hold is the tick's | +| D-4 | live | `docs/RULES-DERIVED.md:33` | constants.ts×1 scheduler.ts×1 seat.ts×3 | boundary-mounts-separate-flushes-4283.test.ts×1 held-equal-rewrite-5038.test.ts×1 verdict-seat-merged-lane-4427.test.ts×1 | Membership in a hold is the tick's | | D-6 | live | `docs/RULES-DERIVED.md:41` | boundaries.ts×1 | direction-rule-probe.test.ts×1 errored-memo-over-output-held-reject-4640.test.ts×1 first-load-superseded-answer-4569.test.ts×2 | A first load starts no hold of its own; uncaught, it holds the change that mounted it, and the root attach | | D-7 | live | `docs/RULES-DERIVED.md:49` | lanes.ts×1 verdict.ts×1 | optimistic-held-write-leak.test.ts×1 optimistic-outside-reask-4690.test.ts×3 | Actions are holds; `yield` is the suspension point; the same tick's earlier writes are adopted | -| D-8 | live | `docs/RULES-DERIVED.md:59` | constants.ts×1 core.ts×10 guesses.ts×1 scheduler.ts×2 seat.ts×2 verdict.ts×2 store.ts×2 target.ts×1 | action-outside-write-ispending-4367.test.ts×3 errored-recovery-wedge-4403-4404-4405.test.ts×2 lane-mount-effects-4557.test.ts×1 lean.test.ts×2 netted-equals-4483.test.ts×2 netted-verdict-reader-4485.test.ts×1 staged-error-netted-write.test.ts×2 | A write is a proposal | +| D-8 | live | `docs/RULES-DERIVED.md:59` | constants.ts×1 core.ts×10 guesses.ts×1 scheduler.ts×3 seat.ts×2 verdict.ts×2 store.ts×2 target.ts×1 | action-outside-write-ispending-4367.test.ts×3 errored-recovery-wedge-4403-4404-4405.test.ts×2 held-equal-rewrite-5038.test.ts×1 lane-mount-effects-4557.test.ts×1 lean.test.ts×2 netted-equals-4483.test.ts×2 netted-verdict-reader-4485.test.ts×1 staged-error-netted-write.test.ts×2 | A write is a proposal | | D-9 | live | `docs/RULES-DERIVED.md:67` | async.ts×2 core.ts×2 effect.ts×1 scheduler.ts×2 seat.ts×4 | hold-recovers-landing-reruns-4469.test.ts×2 ispending-async-derivation-4677.test.ts×1 latest-between-plain-reads-4777.test.ts×1 optimistic-guess-entangled-async-4577.test.ts×2 optimistic-refresh-late-shared-memo-4576.test.ts×1 | Entanglement is graph-driven; what shares an on-screen async derivation settles as one, and a hold ends when its last on-screen reader leaves | | D-10 | live | `docs/RULES-DERIVED.md:75` | attribution.ts×1 constants.ts×1 core.ts×1 effect.ts×1 seat.ts×1 | — | The committed frame keeps its inputs until it is replaced | | D-11 | live | `docs/RULES-DERIVED.md:83` | lanes.ts×2 | lane-dissolve-disposed-member-4613.test.ts×2 optimistic-refresh-late-shared-memo-4576.test.ts×1 | A hold ends when its last on-screen reader leaves | diff --git a/packages/signals/src/core/core.ts b/packages/signals/src/core/core.ts index f0365a1bf..ae86796d7 100644 --- a/packages/signals/src/core/core.ts +++ b/packages/signals/src/core/core.ts @@ -2312,9 +2312,6 @@ export function setSignal(el: Signal | Computed, v: T | ((prev: T) => T // A write landing on a guess is its truth (F-11); one on a lane's // derivation is the lane's staging. if (laneWriteHook && el._config & CONFIG_OVERRIDE) return laneWriteHook(el, v); - // A write to a held slot is a second proposal: the writer's tick joins the - // hold, before the equality gate (D-8). - if (el._config & CONFIG_HELD) joinFuture(txOf(el)); // The first commit has no previous value: the comparator never sees // `undefined`. A write over a shown error is a change (D-41). `custom` is // taken once for the gate and the netting so the default `===` runs inline @@ -2325,6 +2322,9 @@ export function setSignal(el: Signal | Computed, v: T | ((prev: T) => T !!((el as Computed)._statusFlags & STATUS_UNINITIALIZED) || !!el._x?._shownError || (custom === false ? currentValue !== v : !isSame(el, currentValue, v)); + // A write to a held slot is a second proposal: the writer's tick joins the + // hold, equal or not (D-8). + if (el._config & CONFIG_HELD) joinFuture(txOf(el), !valueChanged); if (!valueChanged) return v; // Observe: a committed write begins a re-run chain; a landing reports itself. diff --git a/packages/signals/src/core/scheduler.ts b/packages/signals/src/core/scheduler.ts index 599c4c05e..273e53ced 100644 --- a/packages/signals/src/core/scheduler.ts +++ b/packages/signals/src/core/scheduler.ts @@ -499,14 +499,24 @@ export function dropFlushTx(): void { function unappend(into: unknown[] | null, own: unknown[] | null): void { if (own) for (let j = own.length; j--; ) into!.splice(into!.lastIndexOf(own[j]) >>> 0, 1); } -export function joinFuture(t: Transaction | null): void { +export function joinFuture(t: Transaction | null, unchanged?: boolean): void { // A lane is never the flush's transaction (D-37, #4440). if (laneStageHook && t?._lane) return; const f = flushTransaction; if (f === null) { flushTransaction = t === null ? newTransaction(false) : resolveTx(t); } else if (t !== null) merge(resolveTx(t), f); - if (!globalQueue._running) schedule(); + if (globalQueue._running) return; + // An equal rewrite has nothing to flush; its join ends with the tick (D-4, D-8). + if (unchanged && !scheduled) { + if (quietJoin === null) queueMicrotask(dropQuietJoin); + quietJoin = flushTransaction; + } else schedule(); +} +let quietJoin: Transaction | null = null; +function dropQuietJoin(): void { + if (!scheduled && flushTransaction === quietJoin) flushTransaction = null; + quietJoin = null; } /** `t` merges into `f`: the work that touched both entangles them (A15). * A lane never merges: it is a sub-frame, not a peer — its work is routed @@ -1830,6 +1840,7 @@ export function flush(fn?: () => T): T | void { } return; } + if (quietJoin !== null) dropQuietJoin(); if (halted) return; let count = 0; // Attribution: whether this call drained anything, so `flushEnd` fires once diff --git a/packages/signals/tests/held-equal-rewrite-5038.test.ts b/packages/signals/tests/held-equal-rewrite-5038.test.ts new file mode 100644 index 000000000..51fae195f --- /dev/null +++ b/packages/signals/tests/held-equal-rewrite-5038.test.ts @@ -0,0 +1,148 @@ +// #5038 (D-8): an equal rewrite of a held slot joins the hold but has nothing to flush. +import { afterEach, describe, expect, it } from "vitest"; +import { + action, + createEffect, + createRoot, + createSignal, + DEV, + flush, + resetErrorHalt +} from "../src/index.js"; + +afterEach(() => { + DEV!.hooks.onUpdate = undefined; + resetErrorHalt(); +}); + +describe("#5038: an equal rewrite of a held slot schedules no flush", () => { + it("does not flush for a rewrite with the staged value", async () => { + const entries: number[] = []; + const [s, setS] = createSignal(entries); + const seen: number[][] = []; + const dispose = createRoot(d => { + createEffect(s, v => void seen.push(v)); + return d; + }); + flush(); + let resolve!: () => void; + const next = [1]; + const act = action(function* () { + setS(next); + yield new Promise(r => (resolve = r)); + }); + act(); + flush(); + await Promise.resolve(); + flush(); + + let flushes = 0; + DEV!.hooks.onUpdate = () => void flushes++; + setS(next); + await Promise.resolve(); + await Promise.resolve(); + expect(flushes).toBe(0); + + DEV!.hooks.onUpdate = undefined; + resolve(); + await new Promise(r => setTimeout(r, 0)); + flush(); + expect(seen).toEqual([entries, next]); + dispose(); + }); + + it("still joins the tick's other writes to the hold", async () => { + const [s, setS] = createSignal(0); + const [t, setT] = createSignal(0); + const seen: number[] = []; + const dispose = createRoot(d => { + createEffect(s, () => {}); + createEffect(t, v => void seen.push(v)); + return d; + }); + flush(); + let resolve!: () => void; + const act = action(function* () { + setS(1); + yield new Promise(r => (resolve = r)); + }); + act(); + flush(); + await Promise.resolve(); + + setS(1); + setT(5); + await new Promise(r => setTimeout(r, 0)); + flush(); + expect(seen).toEqual([0]); + + resolve(); + await new Promise(r => setTimeout(r, 0)); + flush(); + expect(seen).toEqual([0, 5]); + dispose(); + }); + + it("a manual flush after the rewrite ends the tick (D-4)", async () => { + const [s, setS] = createSignal(0); + const [t, setT] = createSignal(0); + const seen: number[] = []; + const dispose = createRoot(d => { + createEffect(s, () => {}); + createEffect(t, v => void seen.push(v)); + return d; + }); + flush(); + let resolve!: () => void; + const act = action(function* () { + setS(1); + yield new Promise(r => (resolve = r)); + }); + act(); + flush(); + await Promise.resolve(); + + flush(() => setS(1)); + setT(5); + flush(); + expect(seen).toEqual([0, 5]); + + resolve(); + await new Promise(r => setTimeout(r, 0)); + flush(); + dispose(); + }); + + it("an observer rewriting after every flush lets timers run", async () => { + const [s, setS] = createSignal([]); + const dispose = createRoot(d => { + createEffect(s, () => {}); + return d; + }); + flush(); + const next = [1]; + const act = action(function* () { + setS(next); + yield new Promise(r => setTimeout(r, 0)); + }); + act(); + flush(); + + let rewrites = 0; + let queued = false; + DEV!.hooks.onUpdate = () => { + if (queued || rewrites > 10_000) return; + queued = true; + queueMicrotask(() => { + queued = false; + rewrites++; + setS(s => s); + }); + }; + setS(s => s); + await new Promise(r => setTimeout(r, 0)); + flush(); + expect(rewrites).toBeLessThan(10); + dispose(); + }); +});