Skip to content

Commit 9338c00

Browse files
ryansolidClaude via Cursorcursoragent
authored
fix(web): createSSRResponse tears the render down on body cancel and pre-flush redirect (#3768) (#3772)
The body's cancel() and the pre-flush redirect branch only set a local `closed` flag, so writes were dropped silently: nothing threw, guardSink never reached abandon("sink"), and the render kept resolving fragments and pulling serialized async iterators for nobody. pipe() now hands the render's teardown to createSSRResponse's sink through a module-private symbol (the reverse of SHELL_ABANDONED). The body's cancel() calls it as a "consumer" disconnect, the same reason and finding as cancelling the readable view; a cancel after end() (queued chunks draining) is a no-op. A pre-flush redirect tears down quietly, with no SSR_STREAM_ABANDONED finding, because the response discarded the page and nobody left. Both settle the render record as "abandoned". Co-authored-by: Claude via Cursor <[email protected]> Co-authored-by: Cursor <[email protected]>
1 parent 4738c6d commit 9338c00

5 files changed

Lines changed: 338 additions & 8 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/web": patch
3+
---
4+
5+
`createSSRResponse` over a stream result now tears the render down when nobody will read it (#3768). Cancelling the resolved body (the client went away, a HEAD request whose body the host cancels) disposes the render through `renderToStream`'s disconnect path, as cancelling its `readable` does: every async source still being pulled is returned, `onCleanup` runs, and nothing more is written. This emits `SSR_STREAM_ABANDONED` with `data.reason: "consumer"`. A pre-flush `Location` (a bodyless redirect) tears the render down the same way, as the JSDoc already said, but without the finding, because nobody left. Previously both only dropped writes, and the render ran on until its sources happened to end. The render's observe record settles as `"abandoned"` in both cases.

‎documentation/solid-2.0/08-dev-diagnostics.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ Finding (`warn`, observe + dev). A fragment failed while descendants of it were
589589

590590
**Message:** "[SSR_STREAM_ABANDONED] The response stream was abandoned mid-render (consumer) with 4 fragment(s) still pending; the render was torn down."
591591

592-
Finding (`warn`, observe + dev; no `ownerPath` — a stream event). The consumer cancelled (`data.reason: "consumer"`, a `pipeTo` cancellation — usually the browser navigating away), the sink failed on write (`"sink"`), or the request's `signal` aborted (`"signal"` — `renderToStream`'s `signal` option; how a frame-stream response, whose render never touches the document writable, learns its reader is gone, from its body's `cancel` or the request's abort) while fragments were pending, and the render was torn down. `data.shellFlushed` says whether the shell had gone out; `data.pendingFragments` counts what never shipped. Not an error in the app; at volume it is the request cost of renders nobody waited for.
592+
Finding (`warn`, observe + dev; no `ownerPath` — a stream event). The consumer cancelled (`data.reason: "consumer"`, a `pipeTo` cancellation or a cancelled `createSSRResponse` body — usually the browser navigating away), the sink failed on write (`"sink"`), or the request's `signal` aborted (`"signal"` — `renderToStream`'s `signal` option; how a frame-stream response, whose render never touches the document writable, learns its reader is gone, from its body's `cancel` or the request's abort) while fragments were pending, and the render was torn down. A pre-flush redirect through `createSSRResponse` tears the render down too, without this finding: the page was discarded, nobody left. `data.shellFlushed` says whether the shell had gone out; `data.pendingFragments` counts what never shipped. Not an error in the app; at volume it is the request cost of renders nobody waited for.
593593

594594
#### `LATE_HEADER_WRITE`
595595

@@ -837,7 +837,7 @@ const off = OBSERVE.records.subscribe("render", (event, live) => {
837837
});
838838
```
839839

840-
One record per render, delivered when it **ends**: the document returned, the stream's last fragment written, or the render torn down. `at` is `performance.now()` at the render's start; `shellMs` runs start → the shell complete — for a stream, the head and shell handed to the sink (the head is frozen from there; a fragment can no longer add to it), for a string, the document assembled (the whole render) — and is absent when the render ended before its shell; `durationMs` runs start → the end. `boundaries` counts the `<Loading>` boundaries the shell **waited on** — each also a `"boundary"` record with `streamed: false` — and not the ones that streamed after it; it is counted from the boundary records the render filed, so in an observe build it needs a `"boundary"` listener too (`0` with a `"render"` listener alone). `outcome` is `"complete"` for a render that ran to its end, `"abandoned"` when the consumer left mid-stream (the `SSR_STREAM_ABANDONED` finding is that request's account), `"error"` when the render failed (a string render threw; a stream's uncontained failure wound it down). `live.event` is the request the render served, absent for a render outside a request scope; `live.trace` is the render's trace context (what `getTraceContext()` answers during it). `solid-shell` on the response's `Server-Timing` is this record's `shellMs`, read off the same object at head commit (below, [Chrome Performance panel](#chrome-performance-panel-solidjswebperformance-tracks)). The cost is paid only with a listener installed (or in dev): a render nobody observes reads no clock. `route` is the route the render resolved to, as the router declared it while building its context under this render (`OBSERVE.attribution.withOrigin` with an `initial` ref — the same call that opens the client's first `"navigation"` record): `name` the matched pattern (`/users/:id`), `to` the concrete path, `params` what it bound; read from the router's ref when the render settles, so a match refined during the render is what lands, and absent when no router declared one. It is the name a consumer gives the request (`http.route`), where the URL would scatter one page across as many names as it has parameters.
840+
One record per render, delivered when it **ends**: the document returned, the stream's last fragment written, or the render torn down. `at` is `performance.now()` at the render's start; `shellMs` runs start → the shell complete — for a stream, the head and shell handed to the sink (the head is frozen from there; a fragment can no longer add to it), for a string, the document assembled (the whole render) — and is absent when the render ended before its shell; `durationMs` runs start → the end. `boundaries` counts the `<Loading>` boundaries the shell **waited on** — each also a `"boundary"` record with `streamed: false` — and not the ones that streamed after it; it is counted from the boundary records the render filed, so in an observe build it needs a `"boundary"` listener too (`0` with a `"render"` listener alone). `outcome` is `"complete"` for a render that ran to its end, `"abandoned"` when the consumer left mid-stream (the `SSR_STREAM_ABANDONED` finding is that request's account) or `createSSRResponse` discarded the page for a pre-flush redirect (no finding), `"error"` when the render failed (a string render threw; a stream's uncontained failure wound it down). `live.event` is the request the render served, absent for a render outside a request scope; `live.trace` is the render's trace context (what `getTraceContext()` answers during it). `solid-shell` on the response's `Server-Timing` is this record's `shellMs`, read off the same object at head commit (below, [Chrome Performance panel](#chrome-performance-panel-solidjswebperformance-tracks)). The cost is paid only with a listener installed (or in dev): a render nobody observes reads no clock. `route` is the route the render resolved to, as the router declared it while building its context under this render (`OBSERVE.attribution.withOrigin` with an `initial` ref — the same call that opens the client's first `"navigation"` record): `name` the matched pattern (`/users/:id`), `to` the concrete path, `params` what it bound; read from the router's ref when the render settles, so a match refined during the render is what lands, and absent when no router declared one. It is the name a consumer gives the request (`http.route`), where the URL would scatter one page across as many names as it has parameters.
841841

842842
The **`"call"` record** (from `@solidjs/web`, client) is one server-function call made from the browser — the invocation's twin, seen from the caller's end:
843843

‎packages/web/src/observe.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ export interface RenderEvent {
171171
* `"complete"` — the render ran to its end; `"abandoned"` — the consumer
172172
* left mid-stream (the sink threw, the readable was cancelled — the
173173
* `SSR_STREAM_ABANDONED` finding is that request's account) and the
174-
* render was torn down; `"error"` — the render failed: a string render
175-
* threw, a stream's uncontained failure wound it down through `onError`.
174+
* render was torn down, or `createSSRResponse` discarded the page for a
175+
* pre-flush redirect (no finding); `"error"` — the render failed: a
176+
* string render threw, a stream's uncontained failure wound it down
177+
* through `onError`.
176178
*/
177179
outcome: "complete" | "abandoned" | "error";
178180
/**

‎packages/web/src/server.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,6 +1870,10 @@ export function renderToString(code, options = {}) {
18701870
// is a successful empty document, so the two need separate roads; every
18711871
// other `pipe` sink keeps the plain `end()` (failure) or silence (abort).
18721872
const SHELL_ABANDONED = Symbol();
1873+
// The reverse handle, set on that same sink by `pipe`: the render's
1874+
// teardown, for the response that knows nobody will read the rest — its
1875+
// body cancelled, or a pre-flush redirect that discards the page.
1876+
const ABANDON_RENDER = Symbol();
18731877

18741878
export function renderToStream<T>(
18751879
fn: () => T,
@@ -2034,9 +2038,11 @@ export function renderToStream(code, options = {}) {
20342038
}
20352039
: complete;
20362040
};
2037-
const abandon = disconnect => {
2041+
// `quiet` tears down as a disconnect without the finding: the response
2042+
// chose to discard the page (a pre-flush redirect) — nobody left.
2043+
const abandon = (disconnect, quiet) => {
20382044
if (dead) return;
2039-
if ("_SOLID_OBSERVE_" && disconnect)
2045+
if ("_SOLID_OBSERVE_" && disconnect && !quiet)
20402046
emitFinding(
20412047
{
20422048
code: "SSR_STREAM_ABANDONED",
@@ -3309,6 +3315,7 @@ export function renderToStream(code, options = {}) {
33093315
} catch (_) {}
33103316
});
33113317
if (abandoned) {
3318+
w[ABANDON_RENDER] = abandon;
33123319
if (disconnected) abandoned();
33133320
else onDisconnectedBeforeShell = abandoned;
33143321
}
@@ -6820,7 +6827,9 @@ export function createSSRResponse(
68206827
* header writes fail loudly — see `commitResponseStub`), its
68216828
* status/headers merged over `options.responseInit`, and a pre-flush
68226829
* `Location` short-circuits to a redirect with no body (the render is
6823-
* abandoned). A `Location` set after the flush
6830+
* torn down, without an `SSR_STREAM_ABANDONED` finding — nobody left).
6831+
* Cancelling the resolved body tears the render down as a disconnect
6832+
* (`data.reason: "consumer"`). A `Location` set after the flush
68246833
* can only be honored client-side, so stream completion appends
68256834
* `<script>window.location=...</script>` for relative or HTTP(S) targets
68266835
* (carrying `options.nonce` for strict `script-src` CSPs) before closing.
@@ -6883,6 +6892,7 @@ export function createSSRResponse(result, event, options = {}) {
68836892
resolve(
68846893
new Response(null, { status: getExpectedRedirectStatus(stub), headers: head.headers })
68856894
);
6895+
sink[ABANDON_RENDER]("redirect", true);
68866896
return;
68876897
}
68886898
if (!head.headers.has("content-type")) {
@@ -6894,8 +6904,12 @@ export function createSSRResponse(result, event, options = {}) {
68946904
start(c) {
68956905
controller = c;
68966906
},
6907+
// A cancel while chunks queued after `end()` drain finds
6908+
// the render already over (`closed`): nothing to tear down.
68976909
cancel() {
6910+
if (closed) return;
68986911
closed = true;
6912+
sink[ABANDON_RENDER]("consumer");
68996913
}
69006914
}),
69016915
{ status: head.status, statusText: head.statusText, headers: head.headers }
@@ -6935,7 +6949,10 @@ export function createSSRResponse(result, event, options = {}) {
69356949
// otherwise a status set before the failure does not describe it.
69366950
const status = stub && stub.headers.get("Location") ? getExpectedRedirectStatus(stub) : 500;
69376951
resolve(new Response(null, { status, headers: head.headers }));
6938-
}
6952+
},
6953+
// Replaced by `renderToStream`'s `pipe`; any other `pipe` result has
6954+
// no render to tear down.
6955+
[ABANDON_RENDER]() {}
69396956
};
69406957
result.pipe(sink);
69416958
});

0 commit comments

Comments
 (0)