Skip to content

Commit fbabcc4

Browse files
everton-dgnclaude
authored andcommitted
fix(web): reject render thenables on signal cancellation
1 parent b120f3a commit fbabcc4

4 files changed

Lines changed: 141 additions & 11 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+
Reject `renderToStream` thenable consumers with `signal.reason` when its `signal` aborts, including consumers registered after cancellation. Render failures still resolve with the available HTML.

‎packages/web/src/server-mock.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export function renderToStream<T>(
265265
* "signal"`); the one teardown handle for a host whose transport cannot
266266
* report a dead consumer through the sink or the readable view — pass
267267
* `request.signal`.
268+
* Awaiting the render rejects with `signal.reason` when this signal aborts.
268269
*/
269270
signal?: AbortSignal;
270271
}
@@ -273,7 +274,7 @@ export function renderToStream<T>(
273274
* Awaiting the stream resolves with the complete HTML once every boundary
274275
* settles — the fully-settled-string form of the render. Render errors
275276
* route through `onError` and the promise resolves with whatever HTML the
276-
* render produced; it never rejects.
277+
* render produced. Signal cancellation rejects with `signal.reason`.
277278
*/
278279
then<TResult1 = string, TResult2 = never>(
279280
onfulfilled?: ((html: string) => TResult1 | PromiseLike<TResult1>) | null,

‎packages/web/src/server.ts‎

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,7 @@ export function renderToStream<T>(
20172017
* consumed by a proxy — this is the one teardown handle; pass
20182018
* `request.signal`. Idempotent with the sink and consumer paths: whichever
20192019
* fires first tears down, the rest are no-ops.
2020+
* Awaiting the render rejects with `signal.reason` when this signal aborts.
20202021
*/
20212022
signal?: AbortSignal;
20222023
}
@@ -2025,9 +2026,8 @@ export function renderToStream<T>(
20252026
* Awaiting the stream resolves with the complete HTML once every boundary
20262027
* settles — the fully-settled-string form of the render (`const html =
20272028
* await renderToStream(...)`). Render errors route through `onError` and
2028-
* the promise resolves with whatever HTML the render produced; it never
2029-
* rejects. The same holds when the `signal` aborts: the promise resolves
2030-
* with what was produced, as does `pipeTo`'s.
2029+
* the promise resolves with whatever HTML the render produced. Signal
2030+
* cancellation rejects with `signal.reason`.
20312031
*
20322032
* Completion is this path's head-freeze point: the request event's
20332033
* `response` head (the request scope the render was started in) is
@@ -3151,8 +3151,15 @@ export function renderToStream(code, options = {}) {
31513151
// frame stream ending a plain response at its streaming bound (see
31523152
// frame-sink's `frameStream`) — not a client that left: no abandonment
31533153
// finding for it.
3154+
let cancelled = false;
3155+
let onCancelled;
31543156
const onAbort = signal
3155-
? () => abandon("signal", !!(signal.reason && signal.reason.quiet === true))
3157+
? () => {
3158+
if (dead) return;
3159+
cancelled = true;
3160+
abandon("signal", !!(signal.reason && signal.reason.quiet === true));
3161+
onCancelled && onCancelled();
3162+
}
31563163
: undefined;
31573164
// Dependency edges: the server's signal-naming hooks are installed while
31583165
// an edges render is in flight; this one releases them at its final
@@ -3497,8 +3504,8 @@ export function renderToStream(code, options = {}) {
34973504
// Proper thenable: `await renderToStream(...)` resolves with the full
34983505
// HTML once every boundary settles — the replacement for the removed
34993506
// renderToStringAsync. Render errors route through `onError` (the
3500-
// promise resolves with whatever HTML the render produced; it never
3501-
// rejects), matching the pipe/pipeTo contract.
3507+
// promise resolves with whatever HTML the render produced), matching
3508+
// the pipe/pipeTo contract. Signal cancellation rejects with its reason.
35023509
//
35033510
// Head-freeze point: completion. The pipe paths freeze the request's
35043511
// response head when the shell reaches the sink (`createSSRResponse`
@@ -3522,7 +3529,13 @@ export function renderToStream(code, options = {}) {
35223529
if (requestEvent && requestEvent.response)
35233530
commitResponseStub(requestEvent.response, { event: requestEvent });
35243531
};
3525-
const p = new Promise(resolve => {
3532+
const p = new Promise((resolve, reject) => {
3533+
if (cancelled) return reject(signal.reason);
3534+
const previousCancelled = onCancelled;
3535+
onCancelled = () => {
3536+
previousCancelled && previousCancelled();
3537+
reject(signal.reason);
3538+
};
35263539
function complete() {
35273540
freezeHead();
35283541
dispose();
@@ -3538,9 +3551,9 @@ export function renderToStream(code, options = {}) {
35383551
// A render failure (see `abandon`) — the renderer's own retry pass
35393552
// below, or a boundary's resume loop through `failRender` — resolves
35403553
// with whatever HTML the render produced (nothing, pre-shell): the
3541-
// thenable never rejects, `onError` already heard the failure. A
3542-
// disconnect (the `signal` aborting) resolves the same way (#4646). The
3543-
// head is deliberately NOT frozen: the render died, its teardown
3554+
// thenable only rejects on signal cancellation; `onError` already
3555+
// heard the render failure. The head is deliberately NOT frozen:
3556+
// the render died, its teardown
35443557
// retracts the declarations as it always did, and the consumer
35453558
// keeps a writable head for whatever error response it builds
35463559
// around the partial HTML.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { expect, test, vi } from "vitest";
2+
import { createMemo, onCleanup } from "solid-js";
3+
import { renderToStream, ssr, escape } from "@solidjs/web";
4+
import { watchEscapes } from "./stream-settle-harness.js";
5+
6+
for (const timing of ["before render", "before then", "after then"] as const) {
7+
test(`rejects thenable consumers when aborted ${timing}`, async () => {
8+
const controller = new AbortController();
9+
const reason = { message: "request disconnected" };
10+
const onCompleteAll = vi.fn();
11+
if (timing === "before render") controller.abort(reason);
12+
const stream = renderToStream(() => "hello", {
13+
signal: controller.signal,
14+
onCompleteAll
15+
});
16+
if (timing === "before then") controller.abort(reason);
17+
const results: unknown[] = [];
18+
const fulfilled = vi.fn();
19+
stream.then(fulfilled, error => results.push(error));
20+
stream.then(fulfilled, error => results.push(error));
21+
if (timing === "after then") controller.abort(reason);
22+
await Promise.resolve();
23+
expect(results).toEqual([reason, reason]);
24+
expect(results[0]).toBe(reason);
25+
expect(results[1]).toBe(reason);
26+
expect(fulfilled).not.toHaveBeenCalled();
27+
expect(onCompleteAll).not.toHaveBeenCalled();
28+
});
29+
}
30+
31+
test("rejects with the default abort reason", async () => {
32+
const controller = new AbortController();
33+
const stream = renderToStream(() => "hello", { signal: controller.signal });
34+
const rejected = vi.fn();
35+
stream.then(undefined, rejected);
36+
controller.abort();
37+
await Promise.resolve();
38+
expect(rejected).toHaveBeenCalledExactlyOnceWith(controller.signal.reason);
39+
});
40+
41+
test("an abort after completion preserves the fulfilled result", async () => {
42+
const controller = new AbortController();
43+
const onCompleteAll = vi.fn();
44+
const stream = renderToStream(() => "hello", { signal: controller.signal, onCompleteAll });
45+
const result = Promise.resolve(stream);
46+
expect(await result).toBe("hello");
47+
controller.abort();
48+
expect(await result).toBe("hello");
49+
expect(onCompleteAll).toHaveBeenCalledTimes(1);
50+
});
51+
52+
test("rejects while an asynchronous render is pending", async () => {
53+
const controller = new AbortController();
54+
const reason = new Error("client disconnected");
55+
let resolve!: (value: string) => void;
56+
const pending = new Promise<string>(done => (resolve = done));
57+
const onCompleteAll = vi.fn();
58+
const stream = renderToStream(
59+
() => {
60+
const message = createMemo(() => pending);
61+
return ssr(["<p>", "</p>"], () => escape(message()));
62+
},
63+
{ signal: controller.signal, onCompleteAll }
64+
);
65+
const rejected = vi.fn();
66+
stream.then(undefined, rejected);
67+
controller.abort(reason);
68+
await Promise.resolve();
69+
expect(rejected).toHaveBeenCalledExactlyOnceWith(reason);
70+
resolve("loaded");
71+
await Promise.resolve();
72+
expect(onCompleteAll).not.toHaveBeenCalled();
73+
});
74+
75+
test("rejects both consumers with the abort reason when cleanup throws without escaping", async () => {
76+
const controller = new AbortController();
77+
const reason = new Error("client disconnected");
78+
const cleanupError = new Error("cleanup threw");
79+
const onCompleteAll = vi.fn();
80+
const onError = vi.fn();
81+
const fulfilled = vi.fn();
82+
const rejected = [vi.fn(), vi.fn()];
83+
84+
const escaped = await watchEscapes(async () => {
85+
const stream = renderToStream(
86+
() => {
87+
onCleanup(() => {
88+
throw cleanupError;
89+
});
90+
return "hello";
91+
},
92+
{ signal: controller.signal, onCompleteAll, onError }
93+
);
94+
stream.then(fulfilled, rejected[0]);
95+
stream.then(fulfilled, rejected[1]);
96+
expect(() => controller.abort(reason)).not.toThrow();
97+
await Promise.resolve();
98+
for (const consumer of rejected) {
99+
expect(consumer).toHaveBeenCalledTimes(1);
100+
expect(consumer.mock.calls[0][0]).toBe(controller.signal.reason);
101+
expect(consumer.mock.calls[0][0]).toBe(reason);
102+
}
103+
});
104+
105+
expect(escaped).toEqual([]);
106+
expect(fulfilled).not.toHaveBeenCalled();
107+
expect(onCompleteAll).not.toHaveBeenCalled();
108+
expect(onError).toHaveBeenCalledTimes(1);
109+
expect(onError.mock.calls[0][0]).toBe(cleanupError);
110+
expect(onError.mock.calls[0][1]).toMatchObject({ kind: "render", handling: "failed" });
111+
});

0 commit comments

Comments
 (0)