From da13eb5de2ed7f925a953a6b1359781a2f522a85 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 22 Aug 2026 11:16:23 -0400 Subject: [PATCH] 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. --- server/onerror.test.ts | 14 ++++++++++++++ server/onerror.ts | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/server/onerror.test.ts b/server/onerror.test.ts index 7e61de929a..faa3dab813 100644 --- a/server/onerror.test.ts +++ b/server/onerror.test.ts @@ -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; diff --git a/server/onerror.ts b/server/onerror.ts index 974c41bc5f..690d4ea22e 100644 --- a/server/onerror.ts +++ b/server/onerror.ts @@ -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)) {