Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Remote fetch: an answer let go of unread throws nothing uncaught

Claude9cd07fb1f358
diff --git a/src/server/remote-fetch.ts b/src/server/remote-fetch.ts
index bdee2886..daa0874d 100644
--- a/src/server/remote-fetch.ts
+++ b/src/server/remote-fetch.ts
@@ -417,6 +417,12 @@ export async function fetchPublic(url: string, init: RequestInit = {}, o: Public
}
cb(null, chunk);
},
+ // Let go of (discardBody) or failed with a chunk still on its way: Node 22's Readable.toWeb would put that
+ // chunk into the closed web stream, a "Controller is already closed" nothing catches. Nobody reads it now.
+ destroy(this: Transform, err, cb) {
+ this.removeAllListeners("data");
+ cb(err);
+ },
});
const stream = counted;
res.on("error", (e) => stream.destroy(e));
diff --git a/tests/remote-fetch-drop.test.ts b/tests/remote-fetch-drop.test.ts
new file mode 100644
index 00000000..0c639fe1
--- /dev/null
+++ b/tests/remote-fetch-drop.test.ts
@@ -0,0 +1,42 @@
+import http from "node:http";
+import { afterAll, beforeAll, describe, expect, it } from "vitest";
+import { discardBody, fetchPublic } from "@/server/remote-fetch";
+
+// An error answer the caller lets go of unread (discardBody): its body is often already buffered, and Node 22's
+// Readable.toWeb would still put it into the cancelled web stream, an uncaught "Controller is already closed".
+describe("fetchPublic: an answer let go of unread", () => {
+ const body = JSON.stringify({ error: "not here", detail: "x".repeat(200) });
+ let server: http.Server;
+ let url = "";
+ beforeAll(async () => {
+ server = http.createServer((_req, res) => {
+ res.writeHead(404, { "content-type": "application/json" });
+ res.end(body);
+ });
+ await new Promise<void>((r) => server.listen(0, "127.0.0.1", () => r()));
+ url = `http://127.0.0.1:${(server.address() as { port: number }).port}/history/x`;
+ });
+ afterAll(async () => {
+ server.closeAllConnections?.();
+ await new Promise<void>((r) => server.close(() => r()));
+ });
+
+ it("throws nothing where nobody catches it, and a body read to its end still arrives whole", async () => {
+ const thrown: unknown[] = [];
+ const onError = (e: unknown) => thrown.push(e);
+ process.on("uncaughtException", onError);
+ try {
+ for (let i = 0; i < 20; i++) {
+ const res = await fetchPublic(url, {}, { blocked: () => false });
+ expect(res.status).toBe(404);
+ discardBody(res);
+ }
+ await new Promise((r) => setTimeout(r, 200));
+ expect(thrown.map(String)).toEqual([]);
+ const res = await fetchPublic(url, {}, { blocked: () => false });
+ expect(await res.text()).toBe(body);
+ } finally {
+ process.off("uncaughtException", onError);
+ }
+ });
+});