Compare commits

...

1 Commits

Author SHA1 Message Date
Cursor Agent
6a4de887fb Implement synchronous writes for stdout/stderr in Bun socket implementation 2025-06-05 23:11:58 +00:00
2 changed files with 135 additions and 11 deletions

View File

@@ -1432,7 +1432,9 @@ fn NewSocket(comptime ssl: bool) type {
owned_protos: bool = true,
is_paused: bool = false,
allow_half_open: bool = false,
_: u7 = 0,
/// When created from stdout/stderr fd, force synchronous writes
is_stdio_sync: bool = false,
_: u6 = 0,
};
pub fn hasPendingActivity(this: *This) callconv(.C) bool {
@@ -1491,6 +1493,15 @@ fn NewSocket(comptime ssl: bool) type {
);
},
.fd => |f| {
// Check if this is stdout or stderr
if (f.stdioTag()) |tag| {
switch (tag) {
.std_out, .std_err => {
this.flags.is_stdio_sync = true;
},
else => {},
}
}
const socket = This.Socket.fromFd(this.socket_context.?, f, This, this, null, false) orelse return error.ConnectionFailed;
this.onOpen(socket);
},
@@ -2509,6 +2520,11 @@ fn NewSocket(comptime ssl: bool) type {
const remaining = bytes[uwrote..];
if (remaining.len > 0) {
this.buffered_data_for_node_net.append(bun.default_allocator, remaining) catch bun.outOfMemory();
// Flush immediately for stdout/stderr sockets to ensure synchronous behavior
if (this.flags.is_stdio_sync) {
this.internalFlush();
}
}
}
@@ -2537,16 +2553,29 @@ fn NewSocket(comptime ssl: bool) type {
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..];
_ = bun.c.memmove(this.buffered_data_for_node_net.ptr, remaining.ptr, remaining.len);
this.buffered_data_for_node_net.len = @truncate(remaining.len);
} else {
this.buffered_data_for_node_net.deinitWithAllocator(bun.default_allocator);
this.buffered_data_for_node_net = .{};
// For stdio sockets, keep writing until all data is flushed
while (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..];
_ = bun.c.memmove(this.buffered_data_for_node_net.ptr, remaining.ptr, remaining.len);
this.buffered_data_for_node_net.len = @truncate(remaining.len);
} else {
this.buffered_data_for_node_net.deinitWithAllocator(bun.default_allocator);
this.buffered_data_for_node_net = .{};
}
}
// For non-stdio sockets, only do one write attempt
if (!this.flags.is_stdio_sync) {
break;
}
// If no data was written, avoid infinite loop
if (written == 0) {
break;
}
}
}

View File

@@ -0,0 +1,95 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
'use strict';
// Make sure that sync writes to stderr get processed before exiting.
require('../common');
function parent() {
const spawn = require('child_process').spawn;
const assert = require('assert');
let i = 0;
children.forEach(function(_, c) {
const child = spawn(process.execPath, [__filename, String(c)]);
let err = '';
child.stderr.on('data', function(c) {
err += c;
});
child.on('close', function() {
assert.strictEqual(err, `child ${c}\nfoo\nbar\nbaz\n`);
console.log(`ok ${++i} child #${c}`);
if (i === children.length)
console.log(`1..${i}`);
});
});
}
// using console.error
function child0() {
console.error('child 0');
console.error('foo');
console.error('bar');
console.error('baz');
}
// Using process.stderr
function child1() {
process.stderr.write('child 1\n');
process.stderr.write('foo\n');
process.stderr.write('bar\n');
process.stderr.write('baz\n');
}
// using a net socket
function child2() {
const net = require('net');
const socket = new net.Socket({
fd: 2,
readable: false,
writable: true,
});
socket.write('child 2\n');
socket.write('foo\n');
socket.write('bar\n');
socket.write('baz\n');
}
function child3() {
console.error('child 3\nfoo\nbar\nbaz');
}
function child4() {
process.stderr.write('child 4\nfoo\nbar\nbaz\n');
}
const children = [ child0, child1, child2, child3, child4 ];
if (!process.argv[2]) {
parent();
} else {
children[process.argv[2]]();
// Immediate process.exit to kill any waiting stuff.
process.exit();
}