From 5de25195839c0da59b0d43d29ec8e81a9479e7db Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 02:47:53 +0000 Subject: [PATCH 1/6] test(node:http): pin the payload in the two pin-release tests and assert the pin first `a client disconnect while a large write is draining releases the pin` and `a second write before drain releases the pin on the first buffer` wrote a plain Buffer. A pending res.write() does not pin a plain Buffer, so transfer() detached it at any time and both tests passed with the unpin removed. Both tests now write a Buffer over an ArrayBuffer, assert that the pending write pins it, and then assert the release. Both skip Windows, where a write leaves no pinned tail. --- .../node/http/node-http-pinned-write.test.ts | 42 ++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/test/js/node/http/node-http-pinned-write.test.ts b/test/js/node/http/node-http-pinned-write.test.ts index 49efd38b538a..09c942a14626 100644 --- a/test/js/node/http/node-http-pinned-write.test.ts +++ b/test/js/node/http/node-http-pinned-write.test.ts @@ -247,7 +247,8 @@ describe("node:http large Buffer writes are sent zero-copy", () => { expect(sha1(body)).toBe(expectedHash); }); - test("a client disconnect while a large write is draining releases the pin", async () => { + // The pinned-tail state is never reached on Windows (see the first test), so there is no pin to release. + test.skipIf(isWindows)("a client disconnect while a large write is draining releases the pin", async () => { // Run in a child so the pin release on the close path is observed in // isolation: after 'close' fires the backing store is unpinned, so // transfer() detaches again. @@ -261,14 +262,20 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const { once } = require("node:events"); const CHUNK_SIZE = ${CHUNK_SIZE}; const PATTERN = Buffer.from(Array.from({ length: 256 }, (_, i) => i)); + let detachedWhilePending; let detachedAfterClose; const server = http.createServer((req, res) => { - const payload = Buffer.alloc(CHUNK_SIZE, PATTERN); + // A pending write pins only a Buffer that already has an ArrayBuffer behind it. + const payload = Buffer.from(new ArrayBuffer(CHUNK_SIZE)).fill(PATTERN); res.writeHead(200, { "Content-Type": "application/octet-stream" }); res.write(payload); + // The tail is pending, so the ArrayBuffer is pinned and transfer() copies. + payload.buffer.transfer(); + detachedWhilePending = payload.buffer.detached; res.once("close", () => { // The close path unpinned the backing store, so transfer() detaches. - payload.buffer.transfer(); + // It throws if the first transfer() already detached the buffer. + if (!detachedWhilePending) payload.buffer.transfer(); detachedAfterClose = payload.buffer.detached; }); }); @@ -283,7 +290,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { for (let i = 0; i < 50 && detachedAfterClose === undefined; i++) { await new Promise(r => setTimeout(r, 10)); } - console.log(JSON.stringify({ detachedAfterClose })); + console.log(JSON.stringify({ detachedWhilePending, detachedAfterClose })); server.close(); `, ], @@ -294,7 +301,13 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); const result = JSON.parse(stdout.trim()); - expect({ detachedAfterClose: result.detachedAfterClose, exitCode, stderr }).toEqual({ + expect({ + detachedWhilePending: result.detachedWhilePending, + detachedAfterClose: result.detachedAfterClose, + exitCode, + stderr, + }).toEqual({ + detachedWhilePending: false, detachedAfterClose: true, exitCode: 0, stderr: expect.any(String), @@ -481,21 +494,27 @@ describe("node:http large Buffer writes are sent zero-copy", () => { 30_000, ); - test("a second write before drain releases the pin on the first buffer", async () => { + // The pinned-tail state is never reached on Windows (see the first test), so there is no pin to release. + test.skipIf(isWindows)("a second write before drain releases the pin on the first buffer", async () => { + let detachedWhilePending: boolean | undefined; let detachedAfterSecondWrite: boolean | undefined; const total = CHUNK_SIZE + 1024; await using server = http.createServer((req, res) => { - const first = makePayload(CHUNK_SIZE); + const first = makeArrayBufferBackedPayload(CHUNK_SIZE); res.writeHead(200, { "Content-Type": "application/octet-stream", "Content-Length": String(total), }); res.write(first); + // The tail of `first` is pending, so its ArrayBuffer is pinned and transfer() copies. + first.buffer.transfer(); + detachedWhilePending = first.buffer.detached; res.write(Buffer.alloc(1024, 0xee)); // The second write spilled first's tail into uWS backpressure and - // released the pin, so transfer() detaches again. - first.buffer.transfer(); + // released the pin, so transfer() detaches again. It throws if the + // first transfer() already detached the buffer. + if (!detachedWhilePending) first.buffer.transfer(); detachedAfterSecondWrite = first.buffer.detached; res.end(); }); @@ -504,6 +523,9 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const body = await fetch(`http://localhost:${port}/`).then(r => r.arrayBuffer()); expect(body.byteLength).toBe(total); - expect(detachedAfterSecondWrite).toBe(true); + expect({ detachedWhilePending, detachedAfterSecondWrite }).toEqual({ + detachedWhilePending: false, + detachedAfterSecondWrite: true, + }); }); }); From bd3be9bab97ec40fe9d8598e1400483a8f25055b Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 05:09:53 +0000 Subject: [PATCH 2/6] ci: retrigger From 9c7e69219739fc2cc2da916a25c04a5885d39474 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 06:00:36 +0000 Subject: [PATCH 3/6] test(node:http): read the second-write response through a paused socket The second-write test read the response with fetch(), which reads on another thread while the handler runs. The test asserts that the first write leaves a pinned tail, so the client must not read before the handler returns. A paused net.Socket, as in the first test of the file, makes that hold on every run. --- .../node/http/node-http-pinned-write.test.ts | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/test/js/node/http/node-http-pinned-write.test.ts b/test/js/node/http/node-http-pinned-write.test.ts index 09c942a14626..adb0a6a6207b 100644 --- a/test/js/node/http/node-http-pinned-write.test.ts +++ b/test/js/node/http/node-http-pinned-write.test.ts @@ -499,6 +499,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { let detachedWhilePending: boolean | undefined; let detachedAfterSecondWrite: boolean | undefined; const total = CHUNK_SIZE + 1024; + const serverReady = Promise.withResolvers(); await using server = http.createServer((req, res) => { const first = makeArrayBufferBackedPayload(CHUNK_SIZE); @@ -517,12 +518,27 @@ describe("node:http large Buffer writes are sent zero-copy", () => { if (!detachedWhilePending) first.buffer.transfer(); detachedAfterSecondWrite = first.buffer.detached; res.end(); + serverReady.resolve(); }); await once(server.listen(0), "listening"); const port = (server.address() as AddressInfo).port; - const body = await fetch(`http://localhost:${port}/`).then(r => r.arrayBuffer()); - expect(body.byteLength).toBe(total); + // A paused net.Socket reads nothing while the handler runs, so the first + // write leaves a tail on every run. fetch() reads on another thread. + const socket = net.connect(port, "127.0.0.1"); + await once(socket, "connect"); + socket.pause(); + socket.write(`GET / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n`); + await serverReady.promise; + const chunks: Buffer[] = []; + socket.on("data", (chunk: Buffer) => chunks.push(chunk)); + const closed = once(socket, "close"); + socket.resume(); + await closed; + const received = Buffer.concat(chunks); + + const body = received.subarray(received.indexOf("\r\n\r\n") + 4); + expect(body.length).toBe(total); expect({ detachedWhilePending, detachedAfterSecondWrite }).toEqual({ detachedWhilePending: false, detachedAfterSecondWrite: true, From 3c8691bbe53a7318cc43b297d4baa59ce09bf1ce Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 07:01:27 +0000 Subject: [PATCH 4/6] test(node:http): await the close event and check the bytes in the pin-release tests - The child of the disconnect test awaits the 'close' event of the response. It polled 50 times 10 ms before. - The disconnect test asserts one { stdout, stderr, exitCode } object, so a child that prints nothing shows its stderr. - The second-write test compares the body bytes with the two writes. It compared only the length. - The second-write test rejects its wait when the handler throws or the socket fails before the handler returns, and it disposes the socket. --- .../node/http/node-http-pinned-write.test.ts | 78 ++++++++++--------- 1 file changed, 42 insertions(+), 36 deletions(-) diff --git a/test/js/node/http/node-http-pinned-write.test.ts b/test/js/node/http/node-http-pinned-write.test.ts index adb0a6a6207b..9dd7b6f384ae 100644 --- a/test/js/node/http/node-http-pinned-write.test.ts +++ b/test/js/node/http/node-http-pinned-write.test.ts @@ -247,7 +247,8 @@ describe("node:http large Buffer writes are sent zero-copy", () => { expect(sha1(body)).toBe(expectedHash); }); - // The pinned-tail state is never reached on Windows (see the first test), so there is no pin to release. + // On Windows the kernel takes the whole write (see the first test). Nothing is pinned there, so the first + // transfer() detaches the buffer and there is no pin to release. test.skipIf(isWindows)("a client disconnect while a large write is draining releases the pin", async () => { // Run in a child so the pin release on the close path is observed in // isolation: after 'close' fires the backing store is unpinned, so @@ -263,7 +264,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const CHUNK_SIZE = ${CHUNK_SIZE}; const PATTERN = Buffer.from(Array.from({ length: 256 }, (_, i) => i)); let detachedWhilePending; - let detachedAfterClose; + const closed = Promise.withResolvers(); const server = http.createServer((req, res) => { // A pending write pins only a Buffer that already has an ArrayBuffer behind it. const payload = Buffer.from(new ArrayBuffer(CHUNK_SIZE)).fill(PATTERN); @@ -276,7 +277,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { // The close path unpinned the backing store, so transfer() detaches. // It throws if the first transfer() already detached the buffer. if (!detachedWhilePending) payload.buffer.transfer(); - detachedAfterClose = payload.buffer.detached; + closed.resolve(payload.buffer.detached); }); }); await once(server.listen(0), "listening"); @@ -287,9 +288,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { await once(s, "data"); s.destroy(); // Wait for the server to observe the close. - for (let i = 0; i < 50 && detachedAfterClose === undefined; i++) { - await new Promise(r => setTimeout(r, 10)); - } + const detachedAfterClose = await closed.promise; console.log(JSON.stringify({ detachedWhilePending, detachedAfterClose })); server.close(); `, @@ -300,17 +299,10 @@ describe("node:http large Buffer writes are sent zero-copy", () => { }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); - const result = JSON.parse(stdout.trim()); - expect({ - detachedWhilePending: result.detachedWhilePending, - detachedAfterClose: result.detachedAfterClose, - exitCode, - stderr, - }).toEqual({ - detachedWhilePending: false, - detachedAfterClose: true, - exitCode: 0, + expect({ stdout: stdout.trim(), stderr, exitCode }).toEqual({ + stdout: JSON.stringify({ detachedWhilePending: false, detachedAfterClose: true }), stderr: expect.any(String), + exitCode: 0, }); }); @@ -494,39 +486,50 @@ describe("node:http large Buffer writes are sent zero-copy", () => { 30_000, ); - // The pinned-tail state is never reached on Windows (see the first test), so there is no pin to release. + // On Windows the kernel takes the whole write (see the first test). Nothing is pinned there, so the first + // transfer() detaches the buffer and there is no pin to release. test.skipIf(isWindows)("a second write before drain releases the pin on the first buffer", async () => { + const first = makeArrayBufferBackedPayload(CHUNK_SIZE); + const firstHash = sha1(first); + const second = Buffer.alloc(1024, 0xee); + const total = CHUNK_SIZE + second.length; let detachedWhilePending: boolean | undefined; let detachedAfterSecondWrite: boolean | undefined; - const total = CHUNK_SIZE + 1024; const serverReady = Promise.withResolvers(); await using server = http.createServer((req, res) => { - const first = makeArrayBufferBackedPayload(CHUNK_SIZE); - res.writeHead(200, { - "Content-Type": "application/octet-stream", - "Content-Length": String(total), - }); - res.write(first); - // The tail of `first` is pending, so its ArrayBuffer is pinned and transfer() copies. - first.buffer.transfer(); - detachedWhilePending = first.buffer.detached; - res.write(Buffer.alloc(1024, 0xee)); - // The second write spilled first's tail into uWS backpressure and - // released the pin, so transfer() detaches again. It throws if the - // first transfer() already detached the buffer. - if (!detachedWhilePending) first.buffer.transfer(); - detachedAfterSecondWrite = first.buffer.detached; - res.end(); - serverReady.resolve(); + try { + res.writeHead(200, { + "Content-Type": "application/octet-stream", + "Content-Length": String(total), + }); + res.write(first); + // The tail of `first` is pending, so its ArrayBuffer is pinned and transfer() copies. + first.buffer.transfer(); + detachedWhilePending = first.buffer.detached; + res.write(second); + // The second write spilled first's tail into uWS backpressure and + // released the pin, so transfer() detaches again. It throws if the + // first transfer() already detached the buffer. + if (!detachedWhilePending) first.buffer.transfer(); + detachedAfterSecondWrite = first.buffer.detached; + res.end(); + serverReady.resolve(); + } catch (e) { + serverReady.reject(e); + res.destroy(); + } }); await once(server.listen(0), "listening"); const port = (server.address() as AddressInfo).port; // A paused net.Socket reads nothing while the handler runs, so the first // write leaves a tail on every run. fetch() reads on another thread. - const socket = net.connect(port, "127.0.0.1"); + await using socket = net.connect(port, "127.0.0.1"); await once(socket, "connect"); + // A socket that fails before the handler returns ends the wait. + socket.once("error", serverReady.reject); + socket.once("close", () => serverReady.reject(new Error("the socket closed before the handler returned"))); socket.pause(); socket.write(`GET / HTTP/1.1\r\nHost: x\r\nConnection: close\r\n\r\n`); await serverReady.promise; @@ -539,6 +542,9 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const body = received.subarray(received.indexOf("\r\n\r\n") + 4); expect(body.length).toBe(total); + // The spill copied the tail of `first`, so its bytes arrive intact after the detach. + expect(sha1(body.subarray(0, CHUNK_SIZE))).toBe(firstHash); + expect(Buffer.compare(body.subarray(CHUNK_SIZE), second)).toBe(0); expect({ detachedWhilePending, detachedAfterSecondWrite }).toEqual({ detachedWhilePending: false, detachedAfterSecondWrite: true, From 32fa0f84860ee13029ec0e637531681de7d5dc82 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 08:50:05 +0000 Subject: [PATCH 5/6] test(node:http): keep the moved storage referenced in the pin-release tests - Both tests keep the ArrayBuffer that the first transfer() returns until the release is checked. If a write holds the tail without a pin, that ArrayBuffer owns the bytes that the write still reads. - The second-write test writes 0xff over the storage that transfer() moved. The client must still receive the bytes of the first write. - The child of the disconnect test reports the byte length of the moved storage. A handler that throws ends the child with its error. --- .../node/http/node-http-pinned-write.test.ts | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/test/js/node/http/node-http-pinned-write.test.ts b/test/js/node/http/node-http-pinned-write.test.ts index 9dd7b6f384ae..4d2d0c60324d 100644 --- a/test/js/node/http/node-http-pinned-write.test.ts +++ b/test/js/node/http/node-http-pinned-write.test.ts @@ -266,30 +266,34 @@ describe("node:http large Buffer writes are sent zero-copy", () => { let detachedWhilePending; const closed = Promise.withResolvers(); const server = http.createServer((req, res) => { - // A pending write pins only a Buffer that already has an ArrayBuffer behind it. - const payload = Buffer.from(new ArrayBuffer(CHUNK_SIZE)).fill(PATTERN); - res.writeHead(200, { "Content-Type": "application/octet-stream" }); - res.write(payload); - // The tail is pending, so the ArrayBuffer is pinned and transfer() copies. - payload.buffer.transfer(); - detachedWhilePending = payload.buffer.detached; - res.once("close", () => { - // The close path unpinned the backing store, so transfer() detaches. - // It throws if the first transfer() already detached the buffer. - if (!detachedWhilePending) payload.buffer.transfer(); - closed.resolve(payload.buffer.detached); - }); + try { + // A Buffer over an ArrayBuffer: a pending write pins that ArrayBuffer. + const payload = Buffer.from(new ArrayBuffer(CHUNK_SIZE)).fill(PATTERN); + res.writeHead(200, { "Content-Type": "application/octet-stream" }); + res.write(payload); + // The tail is pending, so the ArrayBuffer is pinned and transfer() copies. + const copy = payload.buffer.transfer(); + detachedWhilePending = payload.buffer.detached; + res.once("close", () => { + // The close path unpinned the backing store, so transfer() detaches. If the + // first transfer() already detached the buffer, "copy" owns its storage. + const moved = detachedWhilePending ? copy : payload.buffer.transfer(); + closed.resolve({ detachedAfterClose: payload.buffer.detached, movedByteLength: moved.byteLength }); + }); + } catch (e) { + closed.reject(e); + } }); await once(server.listen(0), "listening"); const port = server.address().port; const s = net.connect(port, "127.0.0.1"); await once(s, "connect"); s.write("GET / HTTP/1.1\\r\\nHost: x\\r\\n\\r\\n"); - await once(s, "data"); + // A handler that throws rejects "closed", and that ends the wait for data. + await Promise.race([once(s, "data"), closed.promise]); s.destroy(); // Wait for the server to observe the close. - const detachedAfterClose = await closed.promise; - console.log(JSON.stringify({ detachedWhilePending, detachedAfterClose })); + console.log(JSON.stringify({ detachedWhilePending, ...(await closed.promise) })); server.close(); `, ], @@ -300,7 +304,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect({ stdout: stdout.trim(), stderr, exitCode }).toEqual({ - stdout: JSON.stringify({ detachedWhilePending: false, detachedAfterClose: true }), + stdout: JSON.stringify({ detachedWhilePending: false, detachedAfterClose: true, movedByteLength: CHUNK_SIZE }), stderr: expect.any(String), exitCode: 0, }); @@ -505,14 +509,16 @@ describe("node:http large Buffer writes are sent zero-copy", () => { }); res.write(first); // The tail of `first` is pending, so its ArrayBuffer is pinned and transfer() copies. - first.buffer.transfer(); + const copy = first.buffer.transfer(); detachedWhilePending = first.buffer.detached; res.write(second); // The second write spilled first's tail into uWS backpressure and - // released the pin, so transfer() detaches again. It throws if the - // first transfer() already detached the buffer. - if (!detachedWhilePending) first.buffer.transfer(); + // released the pin, so transfer() detaches again. If the first + // transfer() already detached the buffer, `copy` owns its storage. + const moved = detachedWhilePending ? copy : first.buffer.transfer(); detachedAfterSecondWrite = first.buffer.detached; + // The spill copied the tail, so bytes written over the moved storage must not reach the client. + new Uint8Array(moved).fill(0xff); res.end(); serverReady.resolve(); } catch (e) { @@ -542,7 +548,7 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const body = received.subarray(received.indexOf("\r\n\r\n") + 4); expect(body.length).toBe(total); - // The spill copied the tail of `first`, so its bytes arrive intact after the detach. + // The client gets the bytes of `first`, not the 0xff written over its moved storage. expect(sha1(body.subarray(0, CHUNK_SIZE))).toBe(firstHash); expect(Buffer.compare(body.subarray(CHUNK_SIZE), second)).toBe(0); expect({ detachedWhilePending, detachedAfterSecondWrite }).toEqual({ From 78f47d08490c01a7eb216660f648ddd9d595efb8 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 28 Oct 2026 09:43:01 +0000 Subject: [PATCH 6/6] test(node:http): wait for the first data before the disconnect, and require an open response The child of the disconnect test raced the first data event against the promise of the response close. A response that closed by itself resolved the race, and the test passed with no disconnect of the client. The child now waits for the first data event. A handler that throws, a socket error, or a socket that closes first rejects the wait. The child fails if the response closed before the client disconnects. --- test/js/node/http/node-http-pinned-write.test.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/test/js/node/http/node-http-pinned-write.test.ts b/test/js/node/http/node-http-pinned-write.test.ts index 4d2d0c60324d..87de5e707a1a 100644 --- a/test/js/node/http/node-http-pinned-write.test.ts +++ b/test/js/node/http/node-http-pinned-write.test.ts @@ -264,6 +264,8 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const CHUNK_SIZE = ${CHUNK_SIZE}; const PATTERN = Buffer.from(Array.from({ length: 256 }, (_, i) => i)); let detachedWhilePending; + let responseClosed = false; + const firstData = Promise.withResolvers(); const closed = Promise.withResolvers(); const server = http.createServer((req, res) => { try { @@ -275,22 +277,28 @@ describe("node:http large Buffer writes are sent zero-copy", () => { const copy = payload.buffer.transfer(); detachedWhilePending = payload.buffer.detached; res.once("close", () => { + responseClosed = true; // The close path unpinned the backing store, so transfer() detaches. If the // first transfer() already detached the buffer, "copy" owns its storage. const moved = detachedWhilePending ? copy : payload.buffer.transfer(); closed.resolve({ detachedAfterClose: payload.buffer.detached, movedByteLength: moved.byteLength }); }); } catch (e) { - closed.reject(e); + firstData.reject(e); } }); await once(server.listen(0), "listening"); const port = server.address().port; const s = net.connect(port, "127.0.0.1"); await once(s, "connect"); + // The wait for data ends with an error if the handler throws or the socket fails first. + s.once("data", firstData.resolve); + s.once("error", firstData.reject); + s.once("close", () => firstData.reject(new Error("the socket closed before any data"))); s.write("GET / HTTP/1.1\\r\\nHost: x\\r\\n\\r\\n"); - // A handler that throws rejects "closed", and that ends the wait for data. - await Promise.race([once(s, "data"), closed.promise]); + await firstData.promise; + // Only the disconnect of the client may close the response. + if (responseClosed) throw new Error("the response closed before the client disconnected"); s.destroy(); // Wait for the server to observe the close. console.log(JSON.stringify({ detachedWhilePending, ...(await closed.promise) }));