fix: Treat unreadable request stream as a client closed request (#13524)

raw-body throws a 500 'stream is not readable' when the request stream is
already destroyed at parse time, which means the client went away. Map it to
ClientClosedRequestError so it reports 499 and is not sent to the bug tracker.
This commit is contained in:
Tom Moor authored and GitHub committed 2026-08-22 15:16:23 +00:00
1 parent 41616f73cc
commit da13eb5de2
2 files changed
+20 -1

No files matched your search

+14
View File
@@ -127,6 +127,20 @@ describe("onerror", () => {
expect(ctx.body).toContain("request_timeout");
});
it("should convert an unreadable request stream into a client closed request", () => {
const error = new Error("stream is not readable") as ReportableError & {
type?: string;
};
error.status = 500;
error.type = "stream.not.readable";
app.context.onerror.call(ctx, error);
expect(requestErrorHandler).not.toHaveBeenCalled();
expect(ctx.status).toBe(499);
expect(ctx.body).toContain("client_closed_request");
});
it("should set the response status code when the response is not writable", () => {
ctx.writable = false;
+6 -1
View File
@@ -32,9 +32,14 @@ export default function onerror(app: Koa) {
err = ClientClosedRequestError();
}
} else if (
// The connection ended part way through the request message.
err.code === "HPE_INVALID_EOF_STATE" ||
// The client closed the connection before the response was sent.
err.code === "ECONNRESET" ||
err.code === "EPIPE"
// The client closed the connection while the response was written.
err.code === "EPIPE" ||
// Raised by the body parser when the request stream was already destroyed.
err.type === "stream.not.readable"
) {
err = ClientClosedRequestError();
} else if (isQueryCanceledError(err)) {