From de8952d80bcf1f8fb0c80f15b87b03ce495fd92f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 2 Nov 2026 08:42:01 -0500 Subject: [PATCH] fix: Do not report writes to a closing websocket (#14446) Fixes OUTLINE-CLOUD-DAD --- server/onupgrade.test.ts | 15 +++++++++++++++ server/onupgrade.ts | 17 ++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/server/onupgrade.test.ts b/server/onupgrade.test.ts index 40b1064d7f..d380d22a3d 100644 --- a/server/onupgrade.test.ts +++ b/server/onupgrade.test.ts @@ -28,6 +28,21 @@ describe("onupgrade", () => { errorSpy.mockRestore(); }); + it("should swallow writes to a socket that has already ended", async () => { + const errorSpy = vi.spyOn(Logger, "error"); + const server = http.createServer(); + const socket = new PassThrough(); + onupgrade(server); + + server.emit("upgrade", request(), socket); + socket.end(); + socket.write("message"); + await new Promise((resolve) => setImmediate(resolve)); + + expect(errorSpy).not.toHaveBeenCalled(); + errorSpy.mockRestore(); + }); + it("should report unexpected errors", () => { const errorSpy = vi.spyOn(Logger, "error").mockImplementation(() => {}); const server = http.createServer(); diff --git a/server/onupgrade.ts b/server/onupgrade.ts index 64e8d25b71..a29832d40e 100644 --- a/server/onupgrade.ts +++ b/server/onupgrade.ts @@ -63,4 +63,19 @@ export function rejectUpgrade(socket: Duplex) { * Socket errors that are the result of a client, or an intermediary such as a * reverse proxy, going away. They are expected and not worth reporting. */ -const expectedErrorCodes = ["ECONNRESET", "ECONNABORTED", "EPIPE", "ETIMEDOUT"]; +const expectedErrorCodes = [ + // The client closed the connection abruptly. + "ECONNRESET", + + // The connection was aborted before it was established. + "ECONNABORTED", + + // A write was attempted after the client closed the connection. + "EPIPE", + + // The client stopped responding. + "ETIMEDOUT", + + // A message was broadcast to a websocket that was already closing. + "ERR_STREAM_WRITE_AFTER_END", +];