From a179725af7880d914443477347f3f066e95d6ada Mon Sep 17 00:00:00 2001 From: drakeo338 Date: Mon, 19 Oct 2026 05:41:44 +0000 Subject: [PATCH] fix(signals): a value that replaces a held error is not thrown at the reveal A memo that throws while an async sibling holds the frame stages its error (D-41): _pendingValue carries it and CONFIG_STAGED_ERROR marks it. A successful pass in the same hold replaced the staging with its value but left the flag set, so readers at the reveal threw the value as an error and halted the reactive system. The pass that stages a value now clears the flag. Fixes #4145 --- .../held-error-recovery-before-commit.md | 5 ++ packages/signals/src/core/core.ts | 3 + .../tests/held-error-recovery-4145.test.ts | 76 +++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 .changeset/held-error-recovery-before-commit.md create mode 100644 packages/signals/tests/held-error-recovery-4145.test.ts diff --git a/.changeset/held-error-recovery-before-commit.md b/.changeset/held-error-recovery-before-commit.md new file mode 100644 index 0000000000..ba211681bd --- /dev/null +++ b/.changeset/held-error-recovery-before-commit.md @@ -0,0 +1,5 @@ +--- +"@solidjs/signals": patch +--- + +A memo that recovers before its held error commits shows the recovered value (#4145). D-41 stages an error found while a sibling holds the frame, marking the staging with `CONFIG_STAGED_ERROR`. A later successful pass in the same hold replaced the staged error with its value but left the flag set, so the reveal threw the successful value as an error and halted the reactive system. The pass that stages a value now clears the flag. diff --git a/packages/signals/src/core/core.ts b/packages/signals/src/core/core.ts index 5e610220f1..f6e462f017 100644 --- a/packages/signals/src/core/core.ts +++ b/packages/signals/src/core/core.ts @@ -705,6 +705,9 @@ export function recompute(el: Computed, create: boolean = false): void { el._value = value; } else { el._pendingValue = value; + // The value replaces an error staged earlier in this hold (D-41, + // #4145): the staging is an answer again, not one to throw. + el._config &= ~CONFIG_STAGED_ERROR; // Born held: in the transaction from birth, so a read of it this // tick is a read of its world (the mount-during-a-hold case), not // only after the seam parks it. diff --git a/packages/signals/tests/held-error-recovery-4145.test.ts b/packages/signals/tests/held-error-recovery-4145.test.ts new file mode 100644 index 0000000000..a6883132f7 --- /dev/null +++ b/packages/signals/tests/held-error-recovery-4145.test.ts @@ -0,0 +1,76 @@ +/** + * #4145: a memo throws while an async sibling holds the frame (D-41 stages + * the error), then recomputes successfully before that frame commits. The + * success replaces the proposal: when the sibling lands, the frame shows the + * recovered value — it is not thrown as the error it replaced. + */ +import { afterEach, expect, it } from "vitest"; +import { + createEffect, + createMemo, + createRenderEffect, + createRoot, + createSignal, + flush, + resetErrorHalt +} from "../src/index.js"; + +function deferred() { + let resolve!: (value: T) => void; + const promise = new Promise(res => (resolve = res)); + return { promise, resolve }; +} +// Before the fix the reveal halts the reactive system; keep the cases apart. +afterEach(() => resetErrorHalt()); + +const tick = async () => { + await new Promise(r => setTimeout(r, 0)); + flush(); +}; + +it.each([1, 7])( + "a held error replaced by a value (%s) before its frame commits shows the value", + async recovered => { + const request = deferred(); + const boom = new Error("boom"); + const [step, setStep] = createSignal(0); + const seen: unknown[] = []; + const errors: unknown[] = []; + let value!: () => number; + const dispose = createRoot(d => { + const slow = createMemo(() => (step() === 0 ? 1 : request.promise)); + value = createMemo(() => { + const s = step(); + if (s === 1) throw boom; + return s === 0 ? 1 : recovered; + }); + const sum = createMemo(() => slow() + value()); + createRenderEffect(sum, v => void seen.push(v), { schedule: true }); + createEffect(sum, { effect: () => {}, error: e => void errors.push(e) }); + return d; + }); + try { + flush(); + expect(seen).toEqual([2]); + + // `slow` holds the frame; `value`'s error is staged behind it. + setStep(1); + flush(); + expect(seen).toEqual([2]); + + // `value` recovers while the request is still pending. + setStep(2); + flush(); + expect(seen).toEqual([2]); + + request.resolve(2); + await tick(); + expect(errors).toEqual([]); + expect(seen).toEqual([2, 2 + recovered]); + expect(value()).toBe(recovered); + } finally { + dispose(); + flush(); + } + } +);