|
| 1 | +/** |
| 2 | + * @jsxImportSource @solidjs/web |
| 3 | + * |
| 4 | + * solidjs/solid#4193 — two `renderToStream` failures bypassed the ambient |
| 5 | + * server error hook (`configureServerErrors`) when the render had no |
| 6 | + * `onError` of its own, and escaped as unhandled rejections: |
| 7 | + * |
| 8 | + * - a value the serializer cannot handle, settling after the shell: the |
| 9 | + * serializer was only given an error callback when the render had a local |
| 10 | + * `onError`, so seroval threw from its promise callback, never reached its |
| 11 | + * `onDone`, and the render stayed pending forever; |
| 12 | + * - an `onCleanup` that throws in the final dispose after a normal |
| 13 | + * completion: only `abandon()` contained a throwing dispose (#4029). |
| 14 | + * |
| 15 | + * Not pinned here: whether the cleanups registered before a throwing one |
| 16 | + * still run. The owner teardown stops at a throw on the server as it does on |
| 17 | + * the client (`disposeOwner` / `runDisposal`); that is owner semantics, not |
| 18 | + * this containment. |
| 19 | + */ |
| 20 | +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; |
| 21 | +import { |
| 22 | + Loading, |
| 23 | + configureServerErrors, |
| 24 | + renderToStream, |
| 25 | + type ServerErrorContext |
| 26 | +} from "@solidjs/web"; |
| 27 | +import { createMemo, onCleanup } from "solid-js"; |
| 28 | + |
| 29 | +const delay = (ms: number) => new Promise(r => setTimeout(r, ms)); |
| 30 | + |
| 31 | +/** Own the process's uncaught channels so an escape fails THIS test. */ |
| 32 | +async function watchEscapes(run: () => Promise<void>) { |
| 33 | + const events = ["uncaughtException", "unhandledRejection"] as const; |
| 34 | + const previous = events.map(e => process.listeners(e)); |
| 35 | + events.forEach(e => process.removeAllListeners(e)); |
| 36 | + const escaped: unknown[] = []; |
| 37 | + const capture = (reason: unknown) => escaped.push(reason); |
| 38 | + events.forEach(e => process.on(e, capture)); |
| 39 | + try { |
| 40 | + await run(); |
| 41 | + for (let turn = 0; turn < 4; turn++) await delay(20); |
| 42 | + return escaped; |
| 43 | + } finally { |
| 44 | + events.forEach((e, i) => { |
| 45 | + process.off(e, capture); |
| 46 | + for (const listener of previous[i]) process.on(e, listener as any); |
| 47 | + }); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/** Resolves "settled" with the HTML, or "pending" if the render never ends. */ |
| 52 | +function settle(p: PromiseLike<string>) { |
| 53 | + return Promise.race([ |
| 54 | + Promise.resolve(p).then(html => ({ state: "settled", html })), |
| 55 | + delay(500).then(() => ({ state: "pending", html: "" })) |
| 56 | + ]); |
| 57 | +} |
| 58 | + |
| 59 | +function pipeSettle(code: () => any, options: Parameters<typeof renderToStream>[1] = {}) { |
| 60 | + return settle( |
| 61 | + new Promise<string>(resolve => { |
| 62 | + const chunks: string[] = []; |
| 63 | + renderToStream(code, options).pipe({ |
| 64 | + write(chunk: string) { |
| 65 | + chunks.push(String(chunk)); |
| 66 | + }, |
| 67 | + end() { |
| 68 | + resolve(chunks.join("")); |
| 69 | + } |
| 70 | + }); |
| 71 | + }) |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +type Heard = { error: unknown; context: ServerErrorContext }; |
| 76 | +let heard: Heard[]; |
| 77 | +let logged: unknown[][]; |
| 78 | +const ambient = (error: unknown, context: ServerErrorContext) => { |
| 79 | + heard.push({ error, context }); |
| 80 | +}; |
| 81 | +const kinds = () => heard.map(h => `${h.context.kind}/${h.context.handling}`); |
| 82 | + |
| 83 | +beforeEach(() => { |
| 84 | + heard = []; |
| 85 | + logged = []; |
| 86 | + vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => { |
| 87 | + logged.push(args); |
| 88 | + }); |
| 89 | +}); |
| 90 | +afterEach(() => { |
| 91 | + configureServerErrors({ onError: undefined }); |
| 92 | + vi.restoreAllMocks(); |
| 93 | +}); |
| 94 | + |
| 95 | +/** A page whose async value settles after the shell to `value`. */ |
| 96 | +function page(value: unknown, extra?: () => void) { |
| 97 | + return () => { |
| 98 | + extra && extra(); |
| 99 | + const data = createMemo(() => Promise.resolve(value)); |
| 100 | + return ( |
| 101 | + <main> |
| 102 | + <Loading fallback="pending"> |
| 103 | + <p>{String(data())}</p> |
| 104 | + </Loading> |
| 105 | + </main> |
| 106 | + ); |
| 107 | + }; |
| 108 | +} |
| 109 | + |
| 110 | +describe("renderToStream: a value the serializer cannot handle (#4193)", () => { |
| 111 | + test("reaches the ambient hook as render/serialize and the render settles", async () => { |
| 112 | + configureServerErrors({ onError: ambient }); |
| 113 | + let completed = false; |
| 114 | + let result: { state: string; html: string } | undefined; |
| 115 | + const escaped = await watchEscapes(async () => { |
| 116 | + result = await settle( |
| 117 | + renderToStream( |
| 118 | + page(() => "fn"), |
| 119 | + { |
| 120 | + onCompleteAll() { |
| 121 | + completed = true; |
| 122 | + } |
| 123 | + } |
| 124 | + ) |
| 125 | + ); |
| 126 | + }); |
| 127 | + expect(escaped).toEqual([]); |
| 128 | + expect(result!.state).toBe("settled"); |
| 129 | + expect(completed).toBe(true); |
| 130 | + expect(kinds()).toEqual(["render/serialize"]); |
| 131 | + }); |
| 132 | + |
| 133 | + test("the piped render settles too", async () => { |
| 134 | + configureServerErrors({ onError: ambient }); |
| 135 | + let result: { state: string; html: string } | undefined; |
| 136 | + const escaped = await watchEscapes(async () => { |
| 137 | + result = await pipeSettle(page(() => "fn")); |
| 138 | + }); |
| 139 | + expect(escaped).toEqual([]); |
| 140 | + expect(result!.state).toBe("settled"); |
| 141 | + expect(kinds()).toEqual(["render/serialize"]); |
| 142 | + }); |
| 143 | + |
| 144 | + test("the render's own onError still wins over the ambient hook", async () => { |
| 145 | + configureServerErrors({ onError: ambient }); |
| 146 | + const local: Heard[] = []; |
| 147 | + let result: { state: string; html: string } | undefined; |
| 148 | + const escaped = await watchEscapes(async () => { |
| 149 | + result = await settle( |
| 150 | + renderToStream( |
| 151 | + page(() => "fn"), |
| 152 | + { onError: (error, context) => void local.push({ error, context }) } |
| 153 | + ) |
| 154 | + ); |
| 155 | + }); |
| 156 | + expect(escaped).toEqual([]); |
| 157 | + expect(result!.state).toBe("settled"); |
| 158 | + expect(heard).toEqual([]); |
| 159 | + expect(local.map(h => h.context.handling)).toEqual(["serialize"]); |
| 160 | + }); |
| 161 | + |
| 162 | + test("without a hook anywhere the render settles and the failure reaches console.error", async () => { |
| 163 | + let result: { state: string; html: string } | undefined; |
| 164 | + const escaped = await watchEscapes(async () => { |
| 165 | + result = await settle(renderToStream(page(() => "fn"))); |
| 166 | + }); |
| 167 | + expect(escaped).toEqual([]); |
| 168 | + expect(result!.state).toBe("settled"); |
| 169 | + expect(logged.length).toBe(1); |
| 170 | + }); |
| 171 | +}); |
| 172 | + |
| 173 | +describe("renderToStream: an onCleanup that throws in the final dispose (#4193)", () => { |
| 174 | + const cleanupBoom = (ran: string[]) => () => { |
| 175 | + onCleanup(() => ran.push("first")); |
| 176 | + onCleanup(() => { |
| 177 | + throw new Error("cleanup threw"); |
| 178 | + }); |
| 179 | + }; |
| 180 | + |
| 181 | + test("reaches the ambient hook as render/failed when awaited", async () => { |
| 182 | + configureServerErrors({ onError: ambient }); |
| 183 | + const ran: string[] = []; |
| 184 | + let completed = false; |
| 185 | + let result: { state: string; html: string } | undefined; |
| 186 | + const escaped = await watchEscapes(async () => { |
| 187 | + result = await settle( |
| 188 | + renderToStream(page("resolved", cleanupBoom(ran)), { |
| 189 | + onCompleteAll() { |
| 190 | + completed = true; |
| 191 | + } |
| 192 | + }) |
| 193 | + ); |
| 194 | + }); |
| 195 | + expect(escaped).toEqual([]); |
| 196 | + expect(result!.state).toBe("settled"); |
| 197 | + expect(result!.html).toContain("resolved"); |
| 198 | + expect(completed).toBe(true); |
| 199 | + expect(kinds()).toEqual(["render/failed"]); |
| 200 | + expect((heard[0].error as Error).message).toBe("cleanup threw"); |
| 201 | + }); |
| 202 | + |
| 203 | + test("reaches the ambient hook as render/failed when piped", async () => { |
| 204 | + configureServerErrors({ onError: ambient }); |
| 205 | + const ran: string[] = []; |
| 206 | + let result: { state: string; html: string } | undefined; |
| 207 | + const escaped = await watchEscapes(async () => { |
| 208 | + result = await pipeSettle(page("resolved", cleanupBoom(ran))); |
| 209 | + }); |
| 210 | + expect(escaped).toEqual([]); |
| 211 | + expect(result!.state).toBe("settled"); |
| 212 | + expect(kinds()).toEqual(["render/failed"]); |
| 213 | + }); |
| 214 | + |
| 215 | + test("the render's own onError hears it ahead of the ambient hook", async () => { |
| 216 | + configureServerErrors({ onError: ambient }); |
| 217 | + const local: Heard[] = []; |
| 218 | + const escaped = await watchEscapes(async () => { |
| 219 | + await settle( |
| 220 | + renderToStream(page("resolved", cleanupBoom([])), { |
| 221 | + onError: (error, context) => void local.push({ error, context }) |
| 222 | + }) |
| 223 | + ); |
| 224 | + }); |
| 225 | + expect(escaped).toEqual([]); |
| 226 | + expect(heard).toEqual([]); |
| 227 | + expect(local.map(h => `${h.context.kind}/${h.context.handling}`)).toEqual(["render/failed"]); |
| 228 | + }); |
| 229 | + |
| 230 | + test("without a hook anywhere it reaches console.error", async () => { |
| 231 | + const escaped = await watchEscapes(async () => { |
| 232 | + await settle(renderToStream(page("resolved", cleanupBoom([])))); |
| 233 | + }); |
| 234 | + expect(escaped).toEqual([]); |
| 235 | + expect(logged.some(args => (args[0] as Error)?.message === "cleanup threw")).toBe(true); |
| 236 | + }); |
| 237 | +}); |
0 commit comments