diff --git a/.changeset/held-error-recovery-before-commit.md b/.changeset/held-error-recovery-before-commit.md new file mode 100644 index 000000000..ba211681b --- /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 f92e75cef..af0c8313f 100644 --- a/packages/signals/src/core/core.ts +++ b/packages/signals/src/core/core.ts @@ -726,6 +726,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 000000000..a6883132f --- /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(); + } + } +);