Skip to content
Closed
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
512ef2f
node:net: feed _writev chunks to the socket one at a time instead of …
robobun Jul 26, 2026
d9766f8
lint: read chunk.chunk / this.connecting into locals (no-duplicate-co…
robobun Jul 26, 2026
9de12ed
trim code comments flagged by comment-cop
robobun Jul 26, 2026
55e277b
keep the _writev TODO attached to _writev; drop helper doc comment
robobun Jul 26, 2026
dafc8a8
gate writeChunksUntilFull on empty native buffer; primordial slice; r…
robobun Jul 26, 2026
258f882
test: reject serverRecv from the client error handler too
robobun Jul 26, 2026
483a18c
match _write's error code in the _writev close-before-ready path
robobun Jul 26, 2026
1d39cb4
keep the Buffer.concat _writev path on Windows
robobun Jul 26, 2026
6f21740
[autofix.ci] apply automated fixes
autofix-ci[bot] Jul 26, 2026
aeb327a
shrink Windows concat comment to one line (comment-cop)
robobun Jul 26, 2026
6d54716
drop unreachable win32 errno arm in _writev/onWritevHandleReady ready…
robobun Jul 26, 2026
e87d208
Merge branch 'main' into farm/4a2709d6/net-writev-no-concat
robobun Aug 15, 2026
a4f8849
Merge remote-tracking branch 'origin/main' into farm/4a2709d6/net-wri…
robobun Aug 15, 2026
544ff28
mirror #33974's loop-hold contract in the parked _writev path
robobun Aug 15, 2026
e677715
Merge remote-tracking branch 'origin/main' into farm/4a2709d6/net-wri…
robobun Aug 15, 2026
53c7991
test: use --expose-gc + globalThis.gc() in the RSS fixture so the sam…
robobun Sep 10, 2026
47ecee5
net: share the _writev dispatch body between the direct and deferred …
robobun Sep 10, 2026
5f37fbb
Merge remote-tracking branch 'origin/main' into farm/4a2709d6/net-wri…
robobun Sep 10, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 123 additions & 24 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -422,6 +423,11 @@ 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)) callback(null);
return;
}
Comment thread
robobun marked this conversation as resolved.
Resolved
const res = socket.$write(writeChunk || "", self._pendingEncoding || "utf8");
if (res < 0) {
// The retried send failed for good (peer gone): $write returned -errno.
Expand Down Expand Up @@ -1285,6 +1291,14 @@ const SocketHandlers2: SocketHandler<NonNullable<import("node:net").Socket["_han
self.connecting = false;
if (callback) {
const writeChunk = self._pendingData;
if ($isArray(writeChunk)) {
self._pendingData = self[kwriteCallback] = null;
if (writeChunksUntilFull(self, socket, writeChunk, callback)) {
if (self[kended] && !self[kUserUnrefed] && socket === self._handle) socket.unref?.();
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.
Expand Down Expand Up @@ -1848,7 +1862,11 @@ Object.defineProperty(Socket.prototype, "bytesWritten", {
for (let i = 0; i < data.length; i++) {
const chunk = data[i];
if (data.allBuffers || chunk instanceof Buffer) bytes += chunk.length;
else bytes += Buffer.byteLength(chunk.chunk, chunk.encoding);
else {
const inner = chunk.chunk;
if (inner !== undefined) bytes += Buffer.byteLength(inner, chunk.encoding);
else bytes += chunk.length;
}
}
} else if (data) {
// Writes are either a string or a Buffer.
Expand Down Expand Up @@ -2737,35 +2755,116 @@ Socket.prototype.destroySoon = function destroySoon() {
else this.once("finish", this.destroy);
};

function parkPendingChunks(self, socket, chunks, callback) {
self._pendingData = chunks;
self._pendingEncoding = "";
self[kwriteCallback] = callback;
if (self[kended] && !self[kUserUnrefed]) socket.ref?.();
}

function writeChunksUntilFull(self, socket, chunks, callback) {
// Never feed a chunk while the native buffer still holds a tail (open() calls drain() manually).
if (getBufferedAmount(socket) > 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");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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);
}
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;
}

const socket = this._handle;
this._pendingData = null;
this._pendingEncoding = "";
this[kwriteCallback] = null;
if (!socket) {
callback($ERR_SOCKET_CLOSED());
return false;
}
this._unrefTimer();
if (socket.readyState < 0) {
process.nextTick(callback, new ErrnoException(-9 /* UV_EBADF */, "write"));
return false;
}

if (writeChunksUntilFull(this, socket, data, callback)) {
if (this.encrypted) process.nextTick(callback);
else callback();
}
const chunk = Buffer.concat(chunks || []);
return this._write(chunk, "buffer", callback);
};

function onWritevHandleReady(data, callback) {
if (this[kwriteCallback] !== callback) return;
const socket = this._handle;
this._pendingData = null;
this._pendingEncoding = "";
this[kwriteCallback] = null;
if (!socket) {
callback($ERR_SOCKET_CLOSED());
return;
}
this._unrefTimer();
if (socket.readyState < 0) {
process.nextTick(callback, new ErrnoException(-9 /* UV_EBADF */, "write"));
return;
}
if (writeChunksUntilFull(this, socket, data, callback)) {
if (this.encrypted) process.nextTick(callback);
else callback();
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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.
Expand Down
130 changes: 130 additions & 0 deletions test/js/node/net/node-net.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1823,3 +1823,133 @@ it.skipIf(isWindows)("connect({ localPort }) succeeds when the local port has TI
target.close();
}
});

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", () => {
Bun.gc(true);
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();
Bun.gc(true);
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(), "-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<Buffer>();
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<void>((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<void>();
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();
}
});
});
Loading