Skip to content

Commit

Permalink
fix(net) fix bytesWritten drain (#14949)
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari authored Nov 2, 2024
1 parent 6914c5e commit 85fd471
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
25 changes: 18 additions & 7 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,12 @@ fn NewSocket(comptime ssl: bool) type {
if (vm.isShuttingDown()) {
return;
}
this.ref();
defer this.deref();
this.internalFlush();
// is not writable if we have buffered data or if we are already detached
if (this.buffered_data_for_node_net.len > 0 or this.socket.isDetached()) return;

vm.eventLoop().enter();
defer vm.eventLoop().exit();

Expand Down Expand Up @@ -2363,15 +2369,10 @@ fn NewSocket(comptime ssl: bool) type {
};
}

pub fn flush(
this: *This,
_: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
) JSValue {
JSC.markBinding(@src());
fn internalFlush(this: *This) void {
if (this.buffered_data_for_node_net.len > 0) {
const written: usize = @intCast(@max(this.socket.write(this.buffered_data_for_node_net.slice(), false), 0));

this.bytes_written += written;
if (written > 0) {
if (this.buffered_data_for_node_net.len > written) {
const remaining = this.buffered_data_for_node_net.slice()[written..];
Expand All @@ -2385,6 +2386,15 @@ fn NewSocket(comptime ssl: bool) type {
}

this.socket.flush();
}

pub fn flush(
this: *This,
_: *JSC.JSGlobalObject,
_: *JSC.CallFrame,
) JSValue {
JSC.markBinding(@src());
this.internalFlush();

return JSValue.jsUndefined();
}
Expand Down Expand Up @@ -2706,6 +2716,7 @@ fn NewSocket(comptime ssl: bool) type {
) JSValue {
return JSC.JSValue.jsNumber(this.bytes_written + this.buffered_data_for_node_net.len);
}

pub fn getALPNProtocol(
this: *This,
globalObject: *JSC.JSGlobalObject,
Expand Down
4 changes: 0 additions & 4 deletions src/bun.js/api/sockets.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ function generate(ssl) {
alpnProtocol: {
getter: "getALPNProtocol",
},
bytesWritten: {
getter: "getBytesWritten",
},
write: {
fn: "write",
length: 3,
Expand Down Expand Up @@ -169,7 +166,6 @@ function generate(ssl) {
bytesWritten: {
getter: "getBytesWritten",
},

setServername: {
fn: "setServername",
length: 1,
Expand Down
8 changes: 4 additions & 4 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ const Socket = (function (InternalSocket) {
if (callback) {
const writeChunk = self._pendingData;

if (socket.$write(writeChunk || "", "utf8")) {
if (!writeChunk || socket.$write(writeChunk || "", self._pendingEncoding || "utf8")) {
self._pendingData = self.#writeCallback = null;
callback(null);
} else {
Expand Down Expand Up @@ -856,12 +856,12 @@ const Socket = (function (InternalSocket) {
if (!socket) {
// detached but connected? wait for the socket to be attached
this.#writeCallback = callback;
this._pendingEncoding = "buffer";
this._pendingData = Buffer.from(chunk, encoding);
this._pendingEncoding = encoding;
this._pendingData = chunk;
return;
}

const success = socket?.$write(chunk, encoding);
const success = socket.$write(chunk, encoding);
this[kBytesWritten] = socket.bytesWritten;
if (success) {
callback();
Expand Down

0 comments on commit 85fd471

Please sign in to comment.