From 8dcfe564b98894d5f7880fa09f95ef0513203145 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Sat, 7 Nov 2026 22:07:32 +0200 Subject: [PATCH 1/2] [perf] Do not retain the read buffer via the close reason The close reason is emitted with `'conclude'` as a view into the buffer the stream delivered, and the `WebSocket` keeps it as `_closeMessage`. So a closed `WebSocket` that is still referenced keeps that whole buffer, commonly 64 KiB, alive. Copy the reason when it is assigned to `_closeMessage`. A pooled copy would pin the `Buffer` pool the same way, since `Buffer.poolSize` is 64 KiB on recent Node.js versions. Co-Authored-By: Claude Opus 5.5 --- lib/websocket.js | 12 +++++++++++- test/websocket.test.js | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/websocket.js b/lib/websocket.js index 7175b2ec0..953cb903a 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -1191,7 +1191,17 @@ function receiverOnConclude(code, reason) { const websocket = this[kWebSocket]; websocket._closeFrameReceived = true; - websocket._closeMessage = reason; + // + // The reason is a view into the buffer the socket delivered. Copy it so that + // the `WebSocket` does not keep that buffer alive. The copy is not pooled + // because a pooled one would keep the pool alive in the same way. + // + if (reason.length === 0) { + websocket._closeMessage = EMPTY_BUFFER; + } else { + websocket._closeMessage = Buffer.allocUnsafeSlow(reason.length); + reason.copy(websocket._closeMessage); + } websocket._closeCode = code; if (websocket._socket[kWebSocket] === undefined) return; diff --git a/test/websocket.test.js b/test/websocket.test.js index f169292f6..c91876558 100644 --- a/test/websocket.test.js +++ b/test/websocket.test.js @@ -3506,6 +3506,23 @@ describe('WebSocket', () => { }); }); + it('does not retain the read buffer via the close reason', (done) => { + const wss = new WebSocket.Server({ port: 0 }, () => { + const ws = new WebSocket(`ws://localhost:${wss.address().port}`); + + ws.on('close', (code, reason) => { + assert.strictEqual(code, 1000); + assert.deepStrictEqual(reason, Buffer.from('bye')); + assert.strictEqual(reason.buffer.byteLength, reason.length); + wss.close(done); + }); + }); + + wss.on('connection', (ws) => { + ws.close(1000, 'bye'); + }); + }); + it('sets a timer for the closing handshake to complete', (done) => { const wss = new WebSocket.Server({ port: 0 }, () => { const ws = new WebSocket(`ws://localhost:${wss.address().port}`); From c04f7ad2b72d2e0707362ef576c40da63024ae6f Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Sun, 8 Nov 2026 14:55:35 +0100 Subject: [PATCH 2/2] fixup! [perf] Do not retain the read buffer via the close reason --- lib/websocket.js | 5 +++-- test/websocket.test.js | 34 +++++++++++++++++----------------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/websocket.js b/lib/websocket.js index 953cb903a..9760e1ec9 100644 --- a/lib/websocket.js +++ b/lib/websocket.js @@ -1191,6 +1191,8 @@ function receiverOnConclude(code, reason) { const websocket = this[kWebSocket]; websocket._closeFrameReceived = true; + websocket._closeCode = code; + // // The reason is a view into the buffer the socket delivered. Copy it so that // the `WebSocket` does not keep that buffer alive. The copy is not pooled @@ -1200,9 +1202,8 @@ function receiverOnConclude(code, reason) { websocket._closeMessage = EMPTY_BUFFER; } else { websocket._closeMessage = Buffer.allocUnsafeSlow(reason.length); - reason.copy(websocket._closeMessage); + websocket._closeMessage.set(reason); } - websocket._closeCode = code; if (websocket._socket[kWebSocket] === undefined) return; diff --git a/test/websocket.test.js b/test/websocket.test.js index c91876558..99f973fa2 100644 --- a/test/websocket.test.js +++ b/test/websocket.test.js @@ -3506,23 +3506,6 @@ describe('WebSocket', () => { }); }); - it('does not retain the read buffer via the close reason', (done) => { - const wss = new WebSocket.Server({ port: 0 }, () => { - const ws = new WebSocket(`ws://localhost:${wss.address().port}`); - - ws.on('close', (code, reason) => { - assert.strictEqual(code, 1000); - assert.deepStrictEqual(reason, Buffer.from('bye')); - assert.strictEqual(reason.buffer.byteLength, reason.length); - wss.close(done); - }); - }); - - wss.on('connection', (ws) => { - ws.close(1000, 'bye'); - }); - }); - it('sets a timer for the closing handshake to complete', (done) => { const wss = new WebSocket.Server({ port: 0 }, () => { const ws = new WebSocket(`ws://localhost:${wss.address().port}`); @@ -5223,5 +5206,22 @@ describe('WebSocket', () => { ws._socket.push(Buffer.concat(list)); }); }); + + it('does not retain the read buffer via the close reason', (done) => { + const wss = new WebSocket.Server({ port: 0 }, () => { + const ws = new WebSocket(`ws://localhost:${wss.address().port}`); + + ws.on('close', (code, reason) => { + assert.strictEqual(code, 1000); + assert.deepStrictEqual(reason, Buffer.from('bye')); + assert.strictEqual(reason.buffer.byteLength, reason.length); + wss.close(done); + }); + }); + + wss.on('connection', (ws) => { + ws.close(1000, 'bye'); + }); + }); }); });