fix: close streaming responses after upstream errors

This commit is contained in:
tdurieux
2026-09-06 09:19:18 +02:00
parent 38a09188ad
commit b1cb9093bb
3 changed files with 20 additions and 2 deletions
+2 -2
View File
@@ -172,10 +172,10 @@ async function webView(req: express.Request, res: express.Response) {
const html = `<!DOCTYPE html><html><head><title>Content</title></head><link rel="stylesheet" href="/css/all.min.css" /><body><div class="container p-3 file-content markdown-body">${body}</div></body></html>`;
res.contentType("text/html").send(html);
} catch {
f.send(res);
await f.send(res);
}
} else {
f.send(res);
await f.send(res);
}
} catch (error) {
handleError(error, res, req);
+3
View File
@@ -39,6 +39,7 @@ router.post(
);
} catch (error) {
handleError(error, res);
if (res.headersSent && !res.writableEnded) res.destroy();
}
}
);
@@ -111,6 +112,8 @@ router.post("/", async (req: express.Request, res: express.Response) => {
content.destroy();
}
handleError(error, res);
if (res.headersSent && !res.writableEnded) res.destroy();
anonymizer.destroy();
}
content
.on("error", handleStreamError)
+15
View File
@@ -232,6 +232,21 @@ describe("production regressions", function () {
expect(res.headers["Content-Security-Policy"]).to.include("sandbox");
expect(res.headers["Content-Security-Policy"]).not.to.include("allow-same-origin");
});
it("terminates the streamer response after a late upstream error", async function () {
const input = new (require("stream").PassThrough)();
stub(GitHubStream.prototype, "getFileContentCache", async () => input);
stub(require("../src/server/routes/route-utils"), "handleError", () => {});
const res = new (require("stream").PassThrough)();
res.header = res.contentType = () => res;
res.headersSent = true;
res.resume();
const handler = require("../src/streamer/route").default.stack.find(x => x.route.path === "/").route.stack[0].handle;
await handler({ body: { token: "token", repoFullName: "owner/repo", repoId: "repo", filePath: "file.txt", anonymizerOptions: { filePath: "file.txt", image: true, link: true } } }, res);
input.write("partial data");
input.destroy(new Error("upstream broke"));
await new Promise(resolve => setImmediate(resolve));
expect(res.destroyed).to.equal(true);
});
it("keeps a conference eligible for retry when repository expiration fails", async function () {
const Conference = require("../src/core/Conference").default;
const model = { status: "ready", save: async () => {} };