Skip to content

Commit 8f1d9d2

Browse files
ryansolidclaude
andauthored
fix(solid): an Errored fallback's own failure goes to the next boundary up; its wait is budgeted (#4316, #4317) (#4318)
* fix(solid): an Errored fallback's own failure goes to the next boundary up; its wait is budgeted (#4316, #4317) #4316: when a <Loading> fails after the shell and its <Errored> renders the fallback as the outcome (renderOutcome), a fallback hole that rejected while the server waited for it was swallowed. The retry ran with no owner, so the hole's error never met a handler and the outcome came back empty. Parked in a pending outer <Loading>, the inner fragment then settled as a blank child, and the client hydrated the blank and halted. A fallback that threw synchronously reached the parent handler instead, but in its no-consumer mode: an outer <Errored> serialized its error record and rendered a fallback nobody sent. Now a failure of the fallback, sync or async, goes to the next boundary up. The fallback owner's handler holds it while the outcome renders, and the retry runs under that owner. The parent is first asked for the outcome at the same position, so an outer <Errored> whose whole content is that position draws its fallback there and serializes its error with it. Otherwise the failure is routed as a content failure, so a pending outer <Loading> fails its fragment. An outer <Errored> reached that way while it is not being pulled lets it pass and does not render. #4317: the wait now shares the <Loading> resume loop's convergence budget (MAX_RESUME_PASSES, internal; hydration.ts uses it in place of the 10000 literal). Past the budget it answers what is available, which is no finished fallback. It also races each wait against the fallback owner's teardown, so an aborted render stops waiting at once. Tests: sync-throw twins for the parked and unparked shapes, with and without an outer <Errored>. The server harness holds the async variant's markup to the twin's. The hydrate spec holds its client outcome, loaded and streamed, to the twin's, and a caught variant must claim the outer fallback. Server specs cover the budget (a fresh pending source every pass) and the abort. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * fix(solid): a fragment failing while parked in a pending fragment goes to the nearest boundary (#4316) A <Loading> that failed after the shell while its fragment was parked in an enclosing <Loading> still pending was settled as a blank region that reported success. The renderer (web/src/server.ts registerFragment) parks a settled child into its pending parent and resolves its `_fr` whether or not it failed. Nothing upstream heard of the failure: the error was routed only to an <Errored> that could draw at exactly that position (errorOutcome). The client then hydrated the blank region as success and halted or drew nothing. Now the failure is the enclosing fragment's content's, as when the same read fails in that fragment's own render pass. Handlers get an escalation mode (ESCALATE, server-internal) for a failure that surfaced after the render passed them: - A <Loading> whose fragment is still pending fails it, the way a hole failure in its own pass does. - An <Errored> whose finished markup sits raw in such a fragment renders its fallback in place of that markup (FragmentContainer.region): the fallback is waited for, its error is serialized once it is finished, and its own failure goes up the same way. An <Errored> still pending shows the fallback on its next pull, and one already showing its fallback absorbs the failure. - A boundary already settled with a failure absorbs it. - Otherwise the failure goes to the next boundary up. The enclosing <Loading> applies the regions to its markup before it settles, outermost first. If a region is not found, or its fallback never finishes, that fragment fails with the region's error. This replaces the `escalating` counter from 7ce3c98: an <Errored> fallback that fails while the server waits for it uses the same mode, so it is drawn one level up too. Not covered: - The late case. If the enclosing fragment settled before the inner read failed, its markup is already out. The inner fragment rejects on its own and the client renders it, and the client's <Errored> meets the failure, unchanged. - An <Errored> inside a nested fragment that settled into the pending one first. Its markup is out of the enclosing fragment's own markup, so the enclosing fragment fails instead of drawing. Tests: parked shapes with an <Errored> between, around the inner <Loading> with a failing fallback, and around everything, each against a twin with no inner <Loading> (the failure met in the enclosing pass). The server harness holds the stream text to the twin's. The hydrate spec, loaded and streamed, holds the client outcome to the twin's and checks that the drawn fallback is claimed. Server specs cover the late case and a nested fragment that settled first. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * fix(solid): an Errored fallback failing in the shell goes to the enclosing boundary An `<Errored>` whose fallback failed while rendering (a hole that rejects, or a synchronous throw) for its content's error was left blank on the server: the boundary's error handler had already marked the error handled before rendering the fallback, so the fallback's own failure was dropped and the pull returned `undefined`. At shell level, with no `<Loading>`, the shell got a blank region with the errors recorded and no markup for them; an enclosing `<Errored>` serialized its record but never drew. The fallback's failure now passes up from the pull: an enclosing `<Errored>` draws its fallback in place of the region, and with none the request fails, the same for the async hole and its sync-throw twin. Server only; client bundles are byte-identical. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent 542ff6f commit 8f1d9d2

40 files changed

Lines changed: 1303 additions & 25 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Server: when an `<Errored>` renders its fallback for a `<Loading>` that failed after the shell, and a hole in that fallback rejects while the server waits for it, the rejection now goes to the next boundary up, as it does when a fallback throws synchronously. Inside a pending outer `<Loading>`, that outer fragment fails; under an outer `<Errored>` whose whole content is that position, the outer fallback is drawn there and the client adopts it. Before, the region streamed blank and the hydrating client halted (#4316). A synchronous throw in that fallback no longer writes an error record for an outer `<Errored>` without sending its fallback.
6+
7+
The server's wait for that fallback now stops after the same pass budget as the `<Loading>` resume loop and then streams what it has. It also stops as soon as the render is torn down, for example when the request is aborted (#4317).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
An `<Errored>` whose fallback fails while rendering (a hole that rejects, or a synchronous throw) now passes that failure to the enclosing boundary on the server, in the shell as well as in a streamed fragment. An outer `<Errored>` draws its fallback in place of the region, and with none the request fails. Before, the region was sent blank, with the errors recorded and no markup for them.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Server: a `<Loading>` that fails after the shell while its fragment waits inside an outer `<Loading>` that is still pending no longer arrives as a blank region reported as a success. The failure now goes to the nearest enclosing boundary, as it does when the same read fails inside the outer `<Loading>`'s own render. An `<Errored>` between them draws its fallback in the outer fragment, the client adopts it, and its error is serialized with it. With no `<Errored>` between them, the outer fragment fails, and an `<Errored>` whose whole content is that outer `<Loading>` draws its fallback there. This also covers an `<Errored>` fallback that fails in that position. Before, the client hydrated the blank region and either halted or rendered nothing there (#4316).

‎packages/solid/src/server/hydration.ts‎

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import {
1515
ownerId,
1616
onCleanup,
1717
inServerComponentScope,
18-
edgeHooks
18+
edgeHooks,
19+
MAX_RESUME_PASSES,
20+
ESCALATE,
21+
fragmentContainer,
22+
setFragmentContainer,
23+
type FragmentContainer
1924
} from "./signals.js";
2025
import { OBSERVE } from "@solidjs/signals";
2126
import { sharedConfig, NoHydrateContext, callerRenderContext } from "./shared.js";
@@ -154,6 +159,21 @@ function ssrLoadingBoundary(
154159
(o as any).id = contentId;
155160

156161
let done: ReturnType<HydrationContext["registerFragment"]> | undefined;
162+
// This boundary's fragment as the boundaries rendered into it see it
163+
// (`FragmentContainer`): open from registration until it settles, and
164+
// the descendant `<Errored>` fallbacks to draw in place of their markup
165+
// when it does (#4316). `discarded` once it settled with a failure: a
166+
// later failure from inside it has nothing left to fail.
167+
const container: FragmentContainer = {
168+
open: false,
169+
region: (from, to, err) => void regions.push({ from, to, err })
170+
};
171+
let regions: {
172+
from: string;
173+
to: string | undefined | (() => Promise<string | undefined>);
174+
err: any;
175+
}[] = [];
176+
let discarded = false;
157177
let handledRenderError: any;
158178
let retryPromise: Promise<any> | undefined;
159179

@@ -405,7 +425,21 @@ function ssrLoadingBoundary(
405425
// errorOutcome): the fragment settles when it lands.
406426
function failFragment(err: any, report: boolean, wait?: boolean): boolean | Promise<boolean> {
407427
const settle = (outcome: Outcome) => {
428+
discarded = true;
408429
outcomeRendered = !!outcome && outcome.value !== undefined && !outcome.escaped;
430+
// Parked in an enclosing fragment still pending, with nothing drawn at
431+
// this position: the failure is that fragment's content's, as a
432+
// failure in its own pass would be (#4316) — the nearest boundary up
433+
// meets it there (an `<Errored>` draws its fallback in place of its
434+
// markup; else the enclosing fragment fails). Settled first, so this
435+
// fragment parks before the enclosing one settles; the boundary that
436+
// meets it reports it.
437+
if (!outcome && parentHandler && fragmentContainer(parent, true)) {
438+
done!(undefined, err);
439+
parentHandler(err, ESCALATE);
440+
outcomeRendered = true;
441+
return true;
442+
}
409443
if (report && !outcomeRendered) {
410444
reportServerError(
411445
err,
@@ -427,6 +461,15 @@ function ssrLoadingBoundary(
427461
o,
428462
render,
429463
(err: any, handler, outcome) => {
464+
// A failure from inside this boundary's content that surfaced after
465+
// its pass (see `ESCALATE`): still pending, the fragment fails with
466+
// it; already failed, there is nothing left to fail; otherwise its
467+
// content is out or rendered into the enclosing output — up.
468+
if (outcome === ESCALATE) {
469+
if (container.open) return failOpen(err);
470+
if (discarded) return true;
471+
return handler ? handler(err, ESCALATE) : undefined;
472+
}
430473
// The outcome question (see errorOutcome) is a `<Loading>`'s to pass
431474
// up: it owns no fallback for an error.
432475
if (outcome !== undefined) return handler ? handler(err, outcome) : undefined;
@@ -463,6 +506,44 @@ function ssrLoadingBoundary(
463506
);
464507
}
465508

509+
// An escalated failure fails the fragment while it is still pending, as a
510+
// failure in its own pass does (the hole path above); the resume loop
511+
// stops at its next wake.
512+
function failOpen(err: any): true {
513+
container.open = false;
514+
discarded = true;
515+
const settling = failFragment(err, true, true);
516+
const settled = (streamed: boolean) => {
517+
if (!outcomeRendered) reportRouted(err, "client");
518+
record("error", streamed, err);
519+
};
520+
settling instanceof Promise ? settling.then(settled) : settled(settling);
521+
return true;
522+
}
523+
524+
// The settled markup with each descendant `<Errored>` fallback drawn in
525+
// place of its markup (`container.region`), waited for until finished.
526+
// Outermost first: a region inside one already replaced has gone with it.
527+
// A region not found, or a fallback that never finished, fails the
528+
// fragment with the region's error instead (thrown to the caller).
529+
async function drawRegions(value: string): Promise<string> {
530+
for (let i = 0; i < regions.length; i++) {
531+
const r = regions[i];
532+
if (typeof r.to === "function") r.to = await r.to();
533+
if (!container.open) return value;
534+
}
535+
const drawn: string[] = [];
536+
for (let i = regions.length - 1; i >= 0; i--) {
537+
const { from, to, err } = regions[i];
538+
const at = value.indexOf(from);
539+
if (at < 0 && drawn.some(d => d.includes(from))) continue;
540+
if (at < 0 || to === undefined) throw err;
541+
value = value.slice(0, at) + to + value.slice(at + from.length);
542+
drawn.push(from);
543+
}
544+
return value;
545+
}
546+
466547
// Only ever called from the async resume loop below — there is nothing on
467548
// the stack to catch a throw from here. The initial (synchronous) discovery
468549
// pass propagates unhandled errors directly out of runLoadingPhase to the
@@ -560,6 +641,7 @@ function ssrLoadingBoundary(
560641
function runDiscovery(): SSRTemplateObject | undefined {
561642
disposeOwner(o, false);
562643
serializeBuffer = [];
644+
regions = [];
563645
retryPromise = undefined;
564646
resolveCount = 0;
565647
passes++;
@@ -671,7 +753,10 @@ function ssrLoadingBoundary(
671753

672754
if (ctx.async) {
673755
const regOpts = revealGroup ? { revealGroup: revealGroup.id } : undefined;
674-
done = ctx.registerFragment(id, regOpts);
756+
const settle = ctx.registerFragment(id, regOpts);
757+
done = (value, error, escaped) => ((container.open = false), settle(value, error, escaped));
758+
container.open = true;
759+
setFragmentContainer(o, container);
675760
let owners = fragmentOwners.get(ctx.registerFragment);
676761
if (!owners) fragmentOwners.set(ctx.registerFragment, (owners = {}));
677762
owners[id] = done;
@@ -682,7 +767,11 @@ function ssrLoadingBoundary(
682767
// registered in the same re-run owns the entry and its reveal slot.
683768
let disposed = false;
684769
let superseded = false;
685-
onCleanup(() => (disposed = true));
770+
onCleanup(() => {
771+
disposed = true;
772+
container.open = false;
773+
setFragmentContainer(o, null);
774+
});
686775
const abandon = () => {
687776
if (owners![id] !== done) return (superseded = true);
688777
delete owners![id];
@@ -717,15 +806,15 @@ function ssrLoadingBoundary(
717806
};
718807
(async () => {
719808
try {
720-
// Convergence budget: each pass should retire at least one async
721-
// slot, so passes are bounded by the boundary's async-slot count in
722-
// any converging render — real trees sit far below this. A shape
809+
// Convergence budget (`MAX_RESUME_PASSES`): each pass should retire
810+
// at least one async slot, so passes are bounded by the boundary's
811+
// async-slot count in any converging render. A shape
723812
// that plants a fresh pending source every pass (an async read whose
724813
// answer is never adoptable at the re-created slot) would otherwise
725814
// loop at microtask speed, serializing a new deferred per pass until
726815
// the process OOMs (#3003). Fail the boundary loudly instead.
727816
const checkBudget = () => {
728-
if (passes <= 10000) return;
817+
if (passes <= MAX_RESUME_PASSES) return;
729818
throw new Error(
730819
`<Loading> boundary discovery did not converge after ${passes} passes — ` +
731820
`an async source produces a new pending answer on every retry. Ensure repeated ` +
@@ -738,6 +827,7 @@ function ssrLoadingBoundary(
738827
if (IS_DEV) waits!.start([retryPromise]);
739828
await retryPromise.catch(() => {});
740829
if (disposed) return abandon();
830+
if (discarded) return;
741831
if (IS_DEV) waits!.landed();
742832
ret = runDiscovery();
743833
}
@@ -749,12 +839,19 @@ function ssrLoadingBoundary(
749839
if (IS_DEV) waits!.start(pending.p);
750840
await Promise.all(pending.p).catch(() => {});
751841
if (disposed) return abandon();
842+
if (discarded) return;
752843
if (IS_DEV) waits!.landed();
753844
passes++;
754845
ret = runLoadingPhase(() => resolveIn(() => ctx.ssr(pending.t, ...pending.h))) as any;
755846
}
756847
flushSerializeBuffer();
757-
const streamed = done!(ret && Array.isArray(ret.t) ? ret.t[0] : ((ret && ret.t) as any));
848+
let value = ret && Array.isArray(ret.t) ? ret.t[0] : ((ret && ret.t) as any);
849+
if (regions.length) {
850+
value = await drawRegions(value);
851+
if (disposed) return abandon();
852+
if (discarded) return;
853+
}
854+
const streamed = done!(value);
758855
record("settled", streamed);
759856
} catch (err) {
760857
// A failure whose outcome is still resolving settles when it lands.

0 commit comments

Comments
 (0)