diff --git a/.changeset/nested-errored-async-rethrow.md b/.changeset/nested-errored-async-rethrow.md new file mode 100644 index 000000000..2c09edc0b --- /dev/null +++ b/.changeset/nested-errored-async-rethrow.md @@ -0,0 +1,5 @@ +--- +"@solidjs/signals": patch +--- + +Do not keep an inner error boundary's pending reader attached to an outer error boundary after an async rejection is caught by the inner boundary (#5261). diff --git a/packages/signals/docs/RULES-INDEX.md b/packages/signals/docs/RULES-INDEX.md index 04e116acf..fa69744dc 100644 --- a/packages/signals/docs/RULES-INDEX.md +++ b/packages/signals/docs/RULES-INDEX.md @@ -61,7 +61,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul | F-9 | live | `docs/RULES-FUNDAMENTALS.md:101` | boundaries.ts×2 async.ts×6 constants.ts×1 core.ts×10 effect.ts×1 lanes.ts×1 scheduler.ts×1 seat.ts×4 types.ts×4 verdict.ts×17 deferred.ts×2 signals.ts×1 thinProjection.ts×1 | action-refresh-ispending-loop-4349.test.ts×1 answered-then-reasked-released-reader.test.ts×1 answered-then-reasked-same-tick.test.ts×2 async-reads-ahead-second-write.test.ts×1 derived-store-derive-settle.test.ts×2 derived-store-first-landing-verdict.test.ts×1 first-load-superseded-answer-4569.test.ts×1 latest-first-one-pass-4402.test.ts×2 latest-ispending-refetch-loop.test.ts×1 loading-on-held-filter-refresh-4379.test.ts×1 midflight-memo-over-reask-4504.test.ts×1 optimistic-question-change-4336.test.ts×2 refresh-overlapping-poll-5094.test.ts×1 settle-diamond-revisit.test.ts×1 staged-error-netted-write.test.ts×1 staged-error-through-async-memo-4640.test.ts×1 two-sources-answered-reasked-4495.test.ts×2 verdict-memo-flight-no-reask.test.ts×9 verdict-probe-committed-4648.test.ts×2 | `isPending` is about a new question; a re-ask is quiet; a first load is loading, not pending | | F-10 | live | `docs/RULES-FUNDAMENTALS.md:109` | async.ts×1 constants.ts×1 core.ts×8 guesses.ts×1 scheduler.ts×1 seat.ts×6 types.ts×1 verdict.ts×4 thin.ts×1 | createProjection.draft-lifetime-3585.test.ts×1 latest-held-till-flush.test.ts×2 latest-memo-inline-parity.test.ts×1 optimistic-lane-seam-loop-4643.test.ts×1 createProjection.async.test.ts×7 effect-callback-store-data.test.ts×2 latest-read-after-write-3576.test.ts×5 proto-key-removal-4672.test.ts×1 reconcile-keyed-row-proxy-5220.test.ts×2 shallow.test.ts×1 store-reconcile-pending-reader-5005.test.ts×1 two-sources-answered-reasked-4495.test.ts×2 verdict-memo-held-branch-4544.test.ts×1 verdict-probe-committed-4648.test.ts×2 | A write becomes visible at flush — to every channel | | F-11 | live | `docs/RULES-FUNDAMENTALS.md:117` | action.ts×1 async.ts×1 constants.ts×1 core.ts×4 guesses.ts×3 lanes.ts×5 scheduler.ts×1 seat.ts×1 truth.ts×2 types.ts×1 verdict.ts×1 index.ts×1 thin.ts×1 thinOptimistic.ts×1 | guess-memo-reask-other-list-5090.test.ts×1 lane-correction-staged-error-4748.test.ts×1 optimistic-guess-beside-reask-4824.test.ts×2 optimistic-guess-over-held-slot.test.ts×2 optimistic-guess-over-staged-error-4751.test.ts×1 optimistic-held-error-untracked-read-4489.test.ts×1 optimistic-held-write-leak.test.ts×1 optimistic-lane-seam-loop-4643.test.ts×1 optimistic-layered-filter-4336.test.ts×1 optimistic-list-push-filter-push-4642.test.ts×5 optimistic-live-echo-3898.test.ts×1 optimistic-mount-nested-gate-3835.test.ts×1 optimistic-outside-reask-4690.test.ts×7 optimistic-path-guess-moved-row-4902.test.ts×1 optimistic-path-guess-row-leaves-and-returns-4907.test.ts×1 optimistic-reask-over-held-write-4887.test.ts×1 optimistic-second-guess-held-staging-4825.test.ts×1 optimistic-second-log-guess-4666.test.ts×4 projection-chain-lane-4762.test.ts×1 optimistic-landing-model.test.ts×1 optimistic-shallow-guess-source-5027.test.ts×1 reconcile-keyed-row-proxy-5220.test.ts×1 | An optimistic value is a guess over a hold: the displayed value until that hold commits or its own source answers differently | -| F-12 | live | `docs/RULES-FUNDAMENTALS.md:125` | boundaries.ts×2 thinProjection.ts×1 | derived-store-derive-settle.test.ts×1 derived-store-recovery-wake.test.ts×1 errored-heal-disposed-reader-4373.test.ts×1 errored-heal-memo-reader-4644.test.ts×1 errored-held-mount-staged-error-4480.test.ts×1 errored-memo-over-output-held-reject-4640.test.ts×1 errored-reader-4486.test.ts×1 errored-staged-source-4364.test.ts×1 lean.test.ts×1 loading-reveal-held-outside-4593.test.ts×1 optimistic-derive-memo-error-4437.test.ts×1 try-catch-reactive-read.test.ts×2 | An error is a boundary returning to its fallback; pending is not an error | +| F-12 | live | `docs/RULES-FUNDAMENTALS.md:125` | boundaries.ts×3 thinProjection.ts×1 | derived-store-derive-settle.test.ts×1 derived-store-recovery-wake.test.ts×1 errored-heal-disposed-reader-4373.test.ts×1 errored-heal-memo-reader-4644.test.ts×1 errored-held-mount-staged-error-4480.test.ts×1 errored-memo-over-output-held-reject-4640.test.ts×1 errored-reader-4486.test.ts×1 errored-staged-source-4364.test.ts×1 lean.test.ts×1 loading-reveal-held-outside-4593.test.ts×1 nested-errored-async-5261.test.ts×1 optimistic-derive-memo-error-4437.test.ts×1 try-catch-reactive-read.test.ts×2 | An error is a boundary returning to its fallback; pending is not an error | ## D — derived rules | id | status | defined | cited in src | cited in tests | statement (at definition) | @@ -101,7 +101,7 @@ Status legend: **live** stated and standing · **ruled** carries an explicit rul | D-35 | live | `docs/RULES-DERIVED.md:233` | lanes.ts×1 | optimistic-guess-split-bindings-4536.test.ts×1 optimistic-layered-filter-4336.test.ts×2 optimistic-list-push-filter-push-4642.test.ts×6 optimistic-outside-reask-4690.test.ts×1 optimistic-second-guess-reask-4629.test.ts×1 optimistic-second-log-guess-4666.test.ts×1 optimistic-removal-rebase-new-question-4482.test.ts×1 | Provenance: an older question's answer never corrects a newer guess; the body's end corrects what nothing authoritative is answering | | D-36 | live | `docs/RULES-DERIVED.md:237` | — | — | A resting optimistic node is a plain memo | | D-37 | live | `docs/RULES-DERIVED.md:241` | lanes.ts×2 scheduler.ts×2 | display-ahead-plain-write-content.test.ts×1 lane-mount-effects-4557.test.ts×1 optimistic-held-write-leak.test.ts×1 optimistic-lane-seam-loop-4643.test.ts×5 optimistic-list-push-filter-push-4642.test.ts×1 | A lane sees the screen plus its own guesses and never holds a sync write | -| D-38 | live | `docs/RULES-DERIVED.md:247` | boundaries.ts×4 core.ts×1 | errored-reader-4486.test.ts×1 | Effect error phases; a comparator throw is a compute error; an escaping error halts | +| D-38 | live | `docs/RULES-DERIVED.md:247` | boundaries.ts×5 core.ts×1 | errored-reader-4486.test.ts×1 nested-errored-async-5261.test.ts×1 | Effect error phases; a comparator throw is a compute error; an escaping error halts | | D-39 | live | `docs/RULES-DERIVED.md:255` | lanes.ts×1 scheduler.ts×3 | derived-store-recovery-wake.test.ts×1 error-boundary-superseded-staged-error-4527.test.ts×1 error-is-an-answer.test.ts×2 error-staged-recovery.test.ts×1 errored-heal-memo-reader-4644.test.ts×2 errored-list-recovery-held-4526.test.ts×1 errored-memo-over-output-held-reject-4640.test.ts×1 errored-over-rearmed-loading-4384.test.ts×1 errored-reader-4486.test.ts×3 errored-recovery-fresh-loading-4627.test.ts×2 errored-recovery-stale-memo-4592.test.ts×2 errored-recovery-wedge-4403-4404-4405.test.ts×3 latest-memo-inline-parity.test.ts×2 latest-uninitialized-over-latest-4362.test.ts×1 lean.test.ts×2 createProjection.async.test.ts×1 derived-store-heal-4451.test.ts×1 | An error fallback is the display; it stays until a new answer replaces the error | | D-40 | live | `docs/RULES-DERIVED.md:299` | constants.ts×1 core.ts×1 deferred.ts×2 signals.ts×1 | create-deferred-3710.test.ts×1 create-deferred.test.ts×1 | A deferred source lags its readers and never leads them | | D-41 | live | `docs/RULES-DERIVED.md:259` | boundaries.ts×15 async.ts×6 constants.ts×1 core.ts×13 lanes.ts×1 scheduler.ts×5 seat.ts×10 types.ts×2 verdict.ts×4 signals.ts×1 thinProjection.ts×3 | derived-store-error-delivered-once.test.ts×2 derived-store-recovery-wake.test.ts×1 error-boundary-superseded-staged-error-4527.test.ts×3 error-is-an-answer.test.ts×3 error-staged-recovery.test.ts×1 errored-held-mount-staged-error-4480.test.ts×3 errored-latest-loading-reask-4870.test.ts×1 errored-latest-obsolete-flight-4568.test.ts×1 errored-list-recovery-held-4526.test.ts×2 errored-memo-over-output-held-reject-4640.test.ts×1 errored-reader-4486.test.ts×1 errored-recovery-fresh-loading-4627.test.ts×4 errored-recovery-wedge-4403-4404-4405.test.ts×7 errored-staged-source-4364.test.ts×1 held-sync-throw-4109.test.ts×2 hold-recovers-landing-reruns-4469.test.ts×3 lane-correction-staged-error-4748.test.ts×1 lane-hold-staged-error-4841.test.ts×1 latest-beside-staged-read-4365.test.ts×1 latest-derived-held-error-4106.test.ts×1 latest-derived-held-error-guess-lane-4106.test.ts×1 latest-derived-sync-error-4377.test.ts×2 lean.test.ts×2 loading-reveal-held-outside-4593.test.ts×1 optimistic-guess-over-staged-error-4751.test.ts×1 optimistic-held-error-untracked-read-4489.test.ts×3 settle-diamond-revisit.test.ts×1 staged-error-derivation.test.ts×1 staged-error-rerun-4109.test.ts×1 staged-error-through-async-memo-4640.test.ts×1 writable-memo-write-clears-error-4110.test.ts×1 | An error is an answer: it ends the wait like a value, and it shows when a value would | diff --git a/packages/signals/src/boundaries.ts b/packages/signals/src/boundaries.ts index 89040aa50..2f479417a 100644 --- a/packages/signals/src/boundaries.ts +++ b/packages/signals/src/boundaries.ts @@ -182,18 +182,28 @@ const isCollecting = (b: Boundary): boolean => const freshAbove = (b: Boundary | null): boolean => !!b && (b._type & STATUS_PENDING ? isCollecting(b) : freshAbove(b._parent)); -/** The statuses that keep reader `r` of `b`: `b`'s own and pending — and, - * for a loading boundary behind an error fallback, the error of a reader - * directly under it (an error boundary between them shows that error +/** The nearest error boundary at or above `b`, if any. */ +const erroredOf = (b: Boundary | null | undefined): Boundary | null | undefined => + b && b._type !== STATUS_ERROR ? erroredOf(b._parent) : b; + +/** The statuses that keep reader `r` of `b`: pending, `b`'s own — for an error + * boundary, only when no inner error boundary handles `r`'s errors (D-38, F-12) + * — and, for a loading boundary behind an error fallback, the error of a + * reader directly under it (an error boundary between them shows that error * itself; a held signal it waits on has no context): there is nothing to * reveal, the error shows above. Revealing re-derived the content, whose * re-read retried the error's source: pending again, caught again, without * end (#4870). The error's recovery answers the reader, which reveals it * with the recovered content. */ const keeps = (b: Boundary, r: Computed): number => - b._type | STATUS_PENDING | - (behindError(b._parent) && r._context?.[BOUNDARY] === b ? STATUS_ERROR : 0); + (( + b._type === STATUS_ERROR + ? erroredOf(r._context?.[BOUNDARY] as Boundary | undefined) === b + : r._context?.[BOUNDARY] === b && behindError(b._parent) + ) + ? STATUS_ERROR + : 0); /** A reader `b` waits on: recorded for its `on`, and `b` visited at the seam. */ function record(b: Boundary, node: Computed, source = node): void { diff --git a/packages/signals/tests/nested-errored-async-5261.test.ts b/packages/signals/tests/nested-errored-async-5261.test.ts new file mode 100644 index 000000000..b140d719a --- /dev/null +++ b/packages/signals/tests/nested-errored-async-5261.test.ts @@ -0,0 +1,244 @@ +// #5261 (D-38, F-12): an outer error boundary recording a nested pending reader +// behind its fallback must not keep waiting once the inner source rejects with +// an error handled by the inner error boundary. +import { + createErrorBoundary, + createMemo, + createRenderEffect, + createRoot, + createSignal, + flush +} from "../src/index.js"; + +class NotFound extends Error {} +class TooLong extends Error {} +class Fatal extends Error {} + +const wait = (ms = 30) => new Promise(r => setTimeout(r, ms)); + +type Host = string | { child: Host | string }; +function textOf(h: Host): string { + return typeof h === "string" ? h : textOf(h.child); +} + +it("sync: outer recovers to inner fallback when inner handles TooLong after rethrowing NotFound", () => { + const [id, setId] = createSignal("1"); + let screen: Host = ""; + + const dispose = createRoot(d => { + const user = createMemo(() => { + const cur = id(); + if (cur === "missing") throw new NotFound(); + if (cur === "long") throw new TooLong(); + return "user " + cur; + }); + + const outer = createErrorBoundary( + () => { + const inner = createErrorBoundary( + () => { + const p = { child: "" }; + createRenderEffect(user, v => { + p.child = v; + }); + return p; + }, + err => { + const e = err(); + if (!(e instanceof TooLong)) throw e; + return "inner:TooLong"; + } + ); + const wrapper: Host = { child: "" }; + createRenderEffect(inner, v => { + wrapper.child = v; + }); + return wrapper; + }, + err => "outer:" + (err() as Error).constructor.name + ); + + createRenderEffect(outer, v => { + screen = v; + }); + return d; + }); + + flush(); + expect(textOf(screen)).toBe("user 1"); + + setId("missing"); + flush(); + expect(textOf(screen)).toBe("outer:NotFound"); + + setId("long"); + flush(); + expect(textOf(screen)).toBe("inner:TooLong"); + + setId("2"); + flush(); + expect(textOf(screen)).toBe("user 2"); + + dispose(); +}); + +it("async: outer recovers to inner fallback across rethrow -> handled -> rethrow -> recovery", async () => { + const [id, setId] = createSignal("1"); + let screen: Host = ""; + + const dispose = createRoot(d => { + const user = createMemo( + () => + new Promise((res, rej) => { + const cur = id(); + setTimeout(() => { + if (cur === "missing") rej(new NotFound()); + else if (cur === "fatal") rej(new Fatal()); + else if (cur === "long") rej(new TooLong()); + else res("user " + cur); + }, 5); + }) + ); + + const outer = createErrorBoundary( + () => { + const inner = createErrorBoundary( + () => { + const p = { child: "" }; + createRenderEffect(user, v => { + p.child = v; + }); + return p; + }, + err => { + const e = err(); + if (!(e instanceof TooLong)) throw e; + return "inner:TooLong"; + } + ); + const wrapper: Host = { child: "" }; + createRenderEffect(inner, v => { + wrapper.child = v; + }); + return wrapper; + }, + err => "outer:" + (err() as Error).constructor.name + ); + + createRenderEffect(outer, v => { + screen = v; + }); + return d; + }); + + flush(); + await wait(); + expect(textOf(screen)).toBe("user 1"); + + // Inner rethrows NotFound -> outer catches it + setId("missing"); + flush(); + await wait(); + expect(textOf(screen)).toBe("outer:NotFound"); + + // Async rejection with TooLong (handled by inner) -> outer clears, inner shows + setId("long"); + flush(); + await wait(); + expect(textOf(screen)).toBe("inner:TooLong"); + + // Async switch back to unthrown error -> outer catches again + setId("fatal"); + flush(); + await wait(); + expect(textOf(screen)).toBe("outer:Fatal"); + + // Back to handled error, then full recovery + setId("long"); + flush(); + await wait(); + expect(textOf(screen)).toBe("inner:TooLong"); + + setId("2"); + flush(); + await wait(); + expect(textOf(screen)).toBe("user 2"); + + dispose(); +}); + +it("async: outer waits while sibling under outer is pending, then reveals inner fallback once sibling settles", async () => { + const flights = new Map void; reject: (e: Error) => void }>(); + const ask = (key: string) => + new Promise((resolve, reject) => flights.set(key, { resolve, reject })); + const [step, setStep] = createSignal(0); + let screen: Host = ""; + let sibText = ""; + + const dispose = createRoot(d => { + const user = createMemo(() => ask("u:" + step())); + const sibling = createMemo(() => ask("s:" + step())); + + const outer = createErrorBoundary( + () => { + const inner = createErrorBoundary( + () => { + const p = { child: "" }; + createRenderEffect(user, v => { + p.child = v; + }); + return p; + }, + err => { + const e = err(); + if (!(e instanceof TooLong)) throw e; + return "inner:TooLong"; + } + ); + createRenderEffect(sibling, v => { + sibText = v; + }); + const wrapper: Host = { child: "" }; + createRenderEffect(inner, v => { + wrapper.child = v; + }); + return wrapper; + }, + err => "outer:" + (err() as Error).constructor.name + ); + + createRenderEffect(outer, v => { + screen = v; + }); + return d; + }); + + flush(); + flights.get("u:0")!.resolve("ok0"); + flights.get("s:0")!.resolve("sib0"); + await wait(10); + expect(textOf(screen)).toBe("ok0"); + + // u:1 rethrows NotFound -> outer shows fallback + setStep(1); + flush(); + flights.get("s:1")!.resolve("sib1"); + flights.get("u:1")!.reject(new NotFound()); + await wait(10); + expect(textOf(screen)).toBe("outer:NotFound"); + + // step=2: both pending -> u:2 handles TooLong while s:2 still pending (#4592 keeps outer fallback) + setStep(2); + flush(); + flights.get("u:2")!.reject(new TooLong()); + await wait(10); + expect(textOf(screen)).toBe("outer:NotFound"); + + // s:2 resolves -> outer reveals inner:TooLong + flights.get("s:2")!.resolve("sib2"); + await wait(10); + expect(textOf(screen)).toBe("inner:TooLong"); + expect(sibText).toBe("sib2"); + + dispose(); +}); diff --git a/packages/solid/test/errored-nested-async-5261.spec.ts b/packages/solid/test/errored-nested-async-5261.spec.ts new file mode 100644 index 000000000..91095ab58 --- /dev/null +++ b/packages/solid/test/errored-nested-async-5261.spec.ts @@ -0,0 +1,101 @@ +/** + * #5261 (D-38, F-12): nested `` boundaries over async work recover to + * the inner fallback when the inner boundary handles a subsequent error after + * previously rethrowing to the outer boundary. + */ +import { expect, test } from "vitest"; +import { + createMemo, + createRenderEffect, + createRoot, + createSignal, + Errored, + flush +} from "../src/index.js"; + +class NotFound extends Error {} +class TooLong extends Error {} +class Fatal extends Error {} + +const wait = (ms = 30) => new Promise(r => setTimeout(r, ms)); + +type Host = { child: unknown }; +function textOf(node: unknown): string { + return typeof node === "string" ? node : textOf((node as Host).child); +} + +test("nested Errored boundaries recover to inner fallback across async errors (#5261)", async () => { + const [id, setId] = createSignal("1"); + let screen: unknown = ""; + + const dispose = createRoot(d => { + const user = createMemo( + () => + new Promise((res, rej) => { + const cur = id(); + setTimeout(() => { + if (cur === "missing") rej(new NotFound()); + else if (cur === "fatal") rej(new Fatal()); + else if (cur === "long") rej(new TooLong()); + else res("user " + cur); + }, 5); + }) + ); + + const outer = Errored({ + get children() { + const inner = Errored({ + get children() { + const p: Host = { child: "" }; + createRenderEffect(user, v => { + p.child = v; + }); + return p as any; + }, + fallback: err => { + const e = err(); + if (!(e instanceof TooLong)) throw e; + return "inner:TooLong"; + } + }); + const wrapper: Host = { child: "" }; + createRenderEffect(inner as unknown as () => unknown, v => { + wrapper.child = v; + }); + return wrapper as any; + }, + fallback: err => "outer:" + (err() as Error).constructor.name + }); + + createRenderEffect(outer as unknown as () => unknown, v => { + screen = v; + }); + return d; + }); + + flush(); + await wait(); + expect(textOf(screen)).toBe("user 1"); + + setId("missing"); + flush(); + await wait(); + expect(textOf(screen)).toBe("outer:NotFound"); + + setId("long"); + flush(); + await wait(); + expect(textOf(screen)).toBe("inner:TooLong"); + + setId("fatal"); + flush(); + await wait(); + expect(textOf(screen)).toBe("outer:Fatal"); + + setId("2"); + flush(); + await wait(); + expect(textOf(screen)).toBe("user 2"); + + dispose(); +});