Repository: oven-sh/bun
File: src/http/websocket_client.zig (Zig)

Code before:
            this.send_buffer.update(write_len);

            if (do_write) {
                if (comptime Environment.allow_assert) {
                    if (this.proxy_tunnel == null) {
                        bun.assert(!this.tcp.isShutdown());
                        bun.assert(!this.tcp.isClosed());
                        bun.assert(this.tcp.isEstablished());
                    }
                }
                return this.sendBuffer(this.send_buffer.readableSlice(0));
            }

            return true;
        }

        fn sendBuffer(
            this: *WebSocket,
            out_buf: []const u8,
        ) bool {
            bun.assert(out_buf.len > 0);
            // Do not use MSG_MORE, see https://github.com/oven-sh/bun/issues/4010
            if (this.proxy_tunnel) |tunnel| {
                // In tunnel mode, write through the tunnel's TLS layer
                // instead of the detached raw socket.
                _ = tunnel.write(out_buf) catch {
                    this.terminate(ErrorCode.failed_to_write);
                    return false;
                };
                const readable = this.send_buffer.readableSlice(0);
                if (readable.ptr == out_buf.ptr) {
                    this.send_buffer.discard(out_buf.len);
                }
                return true;
            }
            if (this.tcp.isClosed()) {
                return false;
            }
            const wrote = this.tcp.write(out_buf);
            if (wrote < 0) {
                this.terminate(ErrorCode.failed_to_write);
                return false;
            }
            const expected = @as(usize, @intCast(wrote));
            const readable = this.send_buffer.readableSlice(0);
            if (readable.ptr == out_buf.ptr) {
                this.send_buffer.discard(expected);
            }

            return true;
        }

Review comment (suggestion):
This will need to handle incomplete writes - we can't assume that one write will flush everything. The other code path in `onProxyTLSHandshakeComplete` is also not handling incomplete writes. 

Code after:

            const writable = this.send_buffer.writableWithSize(write_len) catch unreachable;
            bytes.copy(this.globalThis, writable[0..write_len], content_byte_len, opcode);
            this.send_buffer.update(write_len);

            if (do_write) {
                if (comptime Environment.allow_assert) {
                    if (this.proxy_tunnel == null) {
                        bun.assert(!this.tcp.isShutdown());
                        bun.assert(!this.tcp.isClosed());
                        bun.assert(this.tcp.isEstablished());
                    }
                }
                return this.sendBuffer(this.send_buffer.readableSlice(0));
            }

            return true;
        }

        fn sendBuffer(
            this: *WebSocket,
            out_buf: []const u8,
        ) bool {
            bun.assert(out_buf.len > 0);
            // Do not use MSG_MORE, see https://github.com/oven-sh/bun/issues/4010
            const wrote: usize = if (this.proxy_tunnel) |tunnel|
                // In tunnel mode, route through the tunnel's TLS layer
                // instead of the detached raw socket.
                tunnel.write(out_buf) catch {
                    this.terminate(ErrorCode.failed_to_write);
                    return false;
                }
            else blk: {
                if (this.tcp.isClosed()) {
                    return false;
                }
                const w = this.tcp.write(out_buf);
                if (w < 0) {
                    this.terminate(ErrorCode.failed_to_write);
                    return false;
                }
                break :blk @intCast(w);
            };
            const readable = this.send_buffer.readableSlice(0);
            if (readable.ptr == out_buf.ptr) {
                this.send_buffer.discard(wrote);
            }
            return true;
        }

        fn sendPong(this: *WebSocket, socket: Socket) bool {