diff --git a/src/js/node/net.ts b/src/js/node/net.ts index 0609cd36d896..1842f4ec7952 100644 --- a/src/js/node/net.ts +++ b/src/js/node/net.ts @@ -47,6 +47,7 @@ const { kArmHandshakeTimeout, kSecureConnectDone, kVerifyError } = require("inte const ArrayPrototypeIncludes = Array.prototype.includes; const ArrayPrototypeJoin = Array.prototype.join; const ArrayPrototypePush = Array.prototype.push; +const ArrayPrototypeSlice = Array.prototype.slice; const MathMax = Math.max; const MathMin = Math.min; @@ -428,6 +429,14 @@ const SocketHandlers: SocketHandler = { self.connecting = false; if (callback) { const writeChunk = self._pendingData; + if ($isArray(writeChunk)) { + self._pendingData = self[kwriteCallback] = null; + if (writeChunksUntilFull(self, socket, writeChunk, callback)) { + unrefAfterDrain(self, socket); + callback(null); + } + return; + } const res = socket.$write(writeChunk || "", self._pendingEncoding || "utf8"); if (res < 0) { // The retried send failed for good (peer gone): $write returned -errno. @@ -1284,6 +1293,14 @@ const SocketHandlers2: SocketHandler 0) { + parkPendingChunks(self, socket, chunks, callback); + return false; + } + const len = chunks.length; + for (let i = 0; i < len; i++) { + const res = socket.$write(chunks[i], "buffer"); + if (res < 0) { + self[kBytesWritten] = socket.bytesWritten; + process.nextTick(failWrite, self, res, callback); + return false; + } + if (!res) { + self[kBytesWritten] = socket.bytesWritten; + parkPendingChunks(self, socket, i + 1 < len ? ArrayPrototypeSlice.$call(chunks, i + 1) : null, callback); + return false; + } + } + self[kBytesWritten] = socket.bytesWritten; + return true; +} + //TODO: migrate to native Socket.prototype._writev = function _writev(data, callback) { const allBuffers = data.allBuffers; - const chunks = data; - if (allBuffers) { - if (data.length === 1) { - return this._write(data[0], "buffer", callback); - } - for (let i = 0; i < data.length; i++) { - data[i] = data[i].chunk; - } - } else { - if (data.length === 1) { - const { chunk, encoding } = data[0]; - return this._write(chunk, encoding, callback); - } - for (let i = 0; i < data.length; i++) { - const { chunk, encoding } = data[i]; - if (typeof chunk === "string") { - data[i] = Buffer.from(chunk, encoding); - } else { - data[i] = chunk; - } - } + for (let i = 0; i < data.length; i++) { + const entry = data[i]; + const chunk = entry.chunk; + data[i] = allBuffers || typeof chunk !== "string" ? chunk : Buffer.from(chunk, entry.encoding); } - const chunk = Buffer.concat(chunks || []); - return this._write(chunk, "buffer", callback); + if (data.length === 1) { + return this._write(data[0], "buffer", callback); + } + if (process.platform === "win32") { + // Winsock only sync-completes a first large send as one WSASend; no vectored send in usockets here. + return this._write(Buffer.concat(data), "buffer", callback); + } + + // cork()/uncork() can reach _writev before the handle is live; defer like _write. + const connecting = this.connecting; + if (connecting || (!this._handle && this[kupgraded] && !this.destroyed)) { + this._pendingData = data; + this._pendingEncoding = ""; + this[kwriteCallback] = callback; + const retry = onWritevHandleReady.bind(this, data, callback); + const onClose = onWritevCloseBeforeReady.bind(this, connecting, callback); + this.once(connecting ? "connect" : kUpgradeAttached, function () { + this.off("close", onClose); + retry(); + }); + this.once("close", onClose); + return; + } + + dispatchWritev(this, data, callback); }; +function dispatchWritev(self, data, callback) { + const socket = self._handle; + self._pendingData = null; + self._pendingEncoding = ""; + self[kwriteCallback] = null; + if (!socket) { + callback($ERR_SOCKET_CLOSED()); + return; + } + self._unrefTimer(); + if (socket.readyState < 0) { + process.nextTick(callback, new ErrnoException(-9 /* UV_EBADF */, "write")); + return; + } + if (writeChunksUntilFull(self, socket, data, callback)) { + if (self.encrypted) process.nextTick(callback); + else callback(); + } +} + +function onWritevHandleReady(data, callback) { + if (this[kwriteCallback] !== callback) return; + dispatchWritev(this, data, callback); +} + +function onWritevCloseBeforeReady(connecting, callback) { + if (this[kwriteCallback] !== callback) return; + this[kwriteCallback] = null; + callback(connecting ? $ERR_SOCKET_CLOSED_BEFORE_CONNECTION() : $ERR_SOCKET_CLOSED()); +} + Socket.prototype._write = function _write(chunk, encoding, callback) { $debug("Socket.prototype._write"); // If we are still connecting, then buffer this for later. diff --git a/test/js/node/net/node-net.test.ts b/test/js/node/net/node-net.test.ts index 49824ff1a216..f4e038d9f7ec 100644 --- a/test/js/node/net/node-net.test.ts +++ b/test/js/node/net/node-net.test.ts @@ -2659,6 +2659,177 @@ it.skipIf(isWindows)("connect({ localPort }) succeeds when the local port has TI } }); +describe("net.Socket write buffering behind a stalled peer", () => { + // A writer that queues past backpressure fills the Writable buffer; when the + // in-flight _write completes, clearBuffer hands the whole batch to _writev. + // Node holds the caller's Buffers by reference in a uv_write_t, so queuing + // N bytes costs ~N bytes of RSS. Concatenating the batch and copying the + // unsent tail into a native Vec on top of the caller's live references costs + // ~2x that at steady state (and ~3x at the transient peak). + // + // cork()/uncork() forces the whole batch through _writev synchronously so the + // sample is deterministic; the `bufs` array keeps the caller's references + // live so the steady-state difference (native copy vs. reference) is what is + // measured, not the GC-raceable transient peak. + // + // Windows keeps the Buffer.concat path (Winsock only completes a first large + // send synchronously when it is a single WSASend, and usockets has no + // vectored send there), so the by-reference bound only applies on POSIX. + it.skipIf(isWindows)( + "holds queued _writev chunks by reference instead of copying into the native buffer", + async () => { + const CHUNK = 65536; + const N = 1024; // 64 MiB queued + const fixture = ` + const net = require("node:net"); + const CHUNK = ${CHUNK}; + const N = ${N}; + const server = net.createServer({ pauseOnConnect: true }, () => {}); + server.listen(0, "127.0.0.1", () => { + const port = server.address().port; + const c = net.connect(port, "127.0.0.1", () => { + globalThis.gc(); + const rss0 = process.memoryUsage().rss; + const bufs = []; + c.cork(); + for (let n = 0; n < N; n++) { + const b = Buffer.alloc(CHUNK, 0x62); + bufs.push(b); + c.write(b); + } + c.uncork(); + globalThis.gc(); + const rss1 = process.memoryUsage().rss; + process.stdout.write(JSON.stringify({ + writableLength: c.writableLength, + rssDelta: rss1 - rss0, + held: bufs.length, + }) + "\\n"); + c.destroy(); + server.close(); + }); + }); + `; + await using proc = Bun.spawn({ + cmd: [bunExe(), "--expose-gc", "-e", fixture], + env: bunEnv, + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stderr).toBe(""); + const { writableLength, rssDelta, held } = JSON.parse(stdout); + const queued = CHUNK * N; + expect(held).toBe(N); + // Most of the 64 MiB is still queued (a few MiB went to the kernel send + // buffer); writableLength reports the queued byte count either way. + expect(writableLength).toBeGreaterThan(queued * 0.75); + // Holding the queue by reference costs ~1.0x on top of the caller's live + // `bufs` (same Buffers). Copying into the native Vec costs another ~1.0x + // (plus growth headroom), i.e. >= 2.0x. The bound sits between the two, + // with headroom for ASAN/debug per-allocation overhead on the reference + // side. + const bound = queued * (isASAN || isDebug ? 1.7 : 1.5); + expect(rssDelta).toBeLessThan(bound); + expect(exitCode).toBe(0); + }, + ); + + // The chunk-by-chunk path must still deliver every byte in order once the + // peer starts reading: this exercises the drain-resume path where + // _pendingData is an array. + it("delivers every queued byte in order once the peer reads", async () => { + const CHUNK = 65536; + const N = 512; // 32 MiB + const serverRecv = Promise.withResolvers(); + const server = createServer({ pauseOnConnect: true }, sock => { + setImmediate(() => sock.resume()); + const chunks: Buffer[] = []; + sock.on("data", d => chunks.push(d)); + sock.on("end", () => { + sock.destroy(); + serverRecv.resolve(Buffer.concat(chunks)); + }); + sock.on("error", serverRecv.reject); + }); + await new Promise((resolve, reject) => { + server.on("error", reject); + server.listen(0, "127.0.0.1", resolve); + }); + const port = (server.address() as import("node:net").AddressInfo).port; + + const expected = Buffer.allocUnsafe(CHUNK * N); + const finished = Promise.withResolvers(); + let sawFalse = false; + let c: Socket | undefined; + try { + c = connect(port, "127.0.0.1", () => { + for (let n = 0; n < N; n++) { + const b = Buffer.alloc(CHUNK, n & 0xff); + b.copy(expected, n * CHUNK); + if (!c!.write(b)) sawFalse = true; + } + c!.end(); + c!.on("finish", finished.resolve); + }); + c.on("error", err => { + finished.reject(err); + serverRecv.reject(err); + }); + const got = await serverRecv.promise; + await finished.promise; + // The test only exercises the _writev queue path if backpressure was hit. + expect(sawFalse).toBe(true); + expect(got.length).toBe(CHUNK * N); + expect(got.equals(expected)).toBe(true); + expect(c.bytesWritten).toBe(CHUNK * N); + } finally { + c?.destroy(); + server.close(); + } + }); + + // Multi-chunk twin of "a write still waiting for drain does keep it alive": + // the unread reply pauses the client (dropping its hold on the loop), the + // first chunk's drain gives the hold up again via unrefAfterDrain, and the + // batch _writev then parks behind it must re-take it the way _write does, + // or the process exits with the queue unflushed. + it.skipIf(isWindows)("a parked _writev batch keeps the process alive until it drains", async () => { + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + ` + const net = require("net"); + let drained = false; + const server = net.createServer(s => { + s.unref(); + s.pause(); + s.write(Buffer.alloc(256 * 1024, 0x61)); + setTimeout(() => { s.on("data", () => {}); s.resume(); }, 100).unref(); + }); + server.unref(); + server.listen(0, () => { + const c = net.connect(server.address().port, () => { + for (let i = 0; i < 127; i++) c.write(Buffer.alloc(64 * 1024, 0x62)); + c.write(Buffer.alloc(64 * 1024, 0x62), () => { drained = true; c.destroy(); }); + }); + }); + process.on("exit", code => console.log("exit", code, "drained", drained)); + `, + ], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect({ stdout: stdout.trim(), stderr: stderr.trim(), exitCode }).toEqual({ + stdout: "exit 0 drained true", + stderr: "", + exitCode: 0, + }); + }); +}); + // On Windows the connect-error path receives raw WSA codes (WSAECONNRESET, // WSAEADDRINUSE) from getsockopt(SO_ERROR) and the pre-connect bind(); these // must be mapped before the errno whitelist, or every failure degrades to