authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-08-10 11:06:36-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-08-10 11:06:36-07:00
loge25168d01be35bea2c8e811fb5fddab847ae6938
tree2b085034a0cd8e46fb778dea134d48e55e7be418
parent6eeceb4b14965a11d3f6b2969b82a696ba99bffc
parent95f57c3369181123aa7ebd4a9654fc489f8bb4fd
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #24774 from kcbanner/fixup_webui_windows

Fix `respondWebSocket`, use overlapped sockets on Windows, and re-enable --webui

6 files changed, 75 insertions(+), 29 deletions(-)

lib/build-web/index.html+2
......@@ -105,6 +105,7 @@
105105 <th scope="col">Semantic Analysis</th>
106106 <th scope="col">Code Generation</th>
107107 <th scope="col">Linking</th>
108 <th scope="col">Total</th>
108109 </tr>
109110 </thead>
110111 <!-- HTML does not allow placing a 'slot' inside of a 'tbody' for backwards-compatibility
......@@ -125,6 +126,7 @@
125126 <th scope="col">Semantic Analysis</th>
126127 <th scope="col">Code Generation</th>
127128 <th scope="col">Linking</th>
129 <th scope="col">Total</th>
128130 </tr>
129131 </thead>
130132 <!-- HTML does not allow placing a 'slot' inside of a 'tbody' for backwards-compatibility
lib/build-web/time_report.zig+4
......@@ -175,6 +175,7 @@ pub fn compileResultMessage(msg_bytes: []u8) error{OutOfMemory}!void {
175175 \\ <td>{D}</td>
176176 \\ <td>{D}</td>
177177 \\ <td>{D}</td>
178 \\ <td>{D}</td>
178179 \\</tr>
179180 \\
180181 , .{
......@@ -182,6 +183,7 @@ pub fn compileResultMessage(msg_bytes: []u8) error{OutOfMemory}!void {
182183 file.ns_sema,
183184 file.ns_codegen,
184185 file.ns_link,
186 file.ns_sema + file.ns_codegen + file.ns_link,
185187 });
186188 }
187189 if (slowest_files.len > max_table_rows) {
......@@ -203,6 +205,7 @@ pub fn compileResultMessage(msg_bytes: []u8) error{OutOfMemory}!void {
203205 \\ <td>{D}</td>
204206 \\ <td>{D}</td>
205207 \\ <td>{D}</td>
208 \\ <td>{D}</td>
206209 \\</tr>
207210 \\
208211 , .{
......@@ -212,6 +215,7 @@ pub fn compileResultMessage(msg_bytes: []u8) error{OutOfMemory}!void {
212215 decl.ns_sema,
213216 decl.ns_codegen,
214217 decl.ns_link,
218 decl.ns_sema + decl.ns_codegen + decl.ns_link,
215219 });
216220 }
217221 if (slowest_decls.len > max_table_rows) {
lib/std/Build/WebServer.zig+2-11
......@@ -65,16 +65,6 @@ pub fn init(opts: Options) WebServer {
6565 std.process.fatal("--webui not yet implemented for single-threaded builds", .{});
6666 }
6767
68 if (builtin.os.tag == .windows) {
69 // At the time of writing, there are two bugs in the standard library which break this feature on Windows:
70 // * Reading from a socket on one thread while writing to it on another seems to deadlock.
71 // * Vectored writes to sockets currently trigger an infinite loop when a buffer has length 0.
72 //
73 // Both of these bugs are expected to be solved by changes which are currently in the unmerged
74 // 'wrangle-writer-buffering' branch. Until that makes it in, this must remain disabled.
75 std.process.fatal("--webui is currently disabled on Windows due to bugs", .{});
76 }
77
7868 const all_steps = opts.all_steps;
7969
8070 const step_names_trailing = opts.gpa.alloc(u8, len: {
......@@ -297,7 +287,8 @@ fn serveWebSocket(ws: *WebServer, sock: *http.Server.WebSocket) !noreturn {
297287 copy.* = @atomicLoad(u8, shared, .monotonic);
298288 }
299289
300 _ = try std.Thread.spawn(.{}, recvWebSocketMessages, .{ ws, sock });
290 const recv_thread = try std.Thread.spawn(.{}, recvWebSocketMessages, .{ ws, sock });
291 defer recv_thread.join();
301292
302293 {
303294 const hello_header: abi.Hello = .{
lib/std/http/Server.zig-1
......@@ -546,7 +546,6 @@ pub const Request = struct {
546546 try out.writeAll("connection: upgrade\r\nupgrade: websocket\r\nsec-websocket-accept: ");
547547 const base64_digest = try out.writableArray(28);
548548 assert(std.base64.standard.Encoder.encode(base64_digest, &digest).len == base64_digest.len);
549 out.advance(base64_digest.len);
550549 try out.writeAll("\r\n");
551550
552551 for (options.extra_headers) |header| {
lib/std/net.zig+63-11
......@@ -259,6 +259,7 @@ pub const Address = extern union {
259259 /// Sets SO_REUSEADDR and SO_REUSEPORT on POSIX.
260260 /// Sets SO_REUSEADDR on Windows, which is roughly equivalent.
261261 reuse_address: bool = false,
262 /// Sets O_NONBLOCK.
262263 force_nonblocking: bool = false,
263264 };
264265
......@@ -1998,11 +1999,8 @@ pub const Stream = struct {
19981999 return n;
19992000 }
20002001
2001 fn streamBufs(r: *Reader, bufs: []windows.ws2_32.WSABUF) Error!u32 {
2002 var n: u32 = undefined;
2003 var flags: u32 = 0;
2004 const rc = windows.ws2_32.WSARecvFrom(r.net_stream.handle, bufs.ptr, @intCast(bufs.len), &n, &flags, null, null, null, null);
2005 if (rc != 0) switch (windows.ws2_32.WSAGetLastError()) {
2002 fn handleRecvError(winsock_error: windows.ws2_32.WinsockError) Error!void {
2003 switch (winsock_error) {
20062004 .WSAECONNRESET => return error.ConnectionResetByPeer,
20072005 .WSAEFAULT => unreachable, // a pointer is not completely contained in user address space.
20082006 .WSAEINPROGRESS, .WSAEINTR => unreachable, // deprecated and removed in WSA 2.2
......@@ -2013,10 +2011,39 @@ pub const Stream = struct {
20132011 .WSAENOTCONN => return error.SocketNotConnected,
20142012 .WSAEWOULDBLOCK => return error.WouldBlock,
20152013 .WSANOTINITIALISED => unreachable, // WSAStartup must be called before this function
2016 .WSA_IO_PENDING => unreachable, // not using overlapped I/O
2014 .WSA_IO_PENDING => unreachable,
20172015 .WSA_OPERATION_ABORTED => unreachable, // not using overlapped I/O
20182016 else => |err| return windows.unexpectedWSAError(err),
2017 }
2018 }
2019
2020 fn streamBufs(r: *Reader, bufs: []windows.ws2_32.WSABUF) Error!u32 {
2021 var flags: u32 = 0;
2022 var overlapped: windows.OVERLAPPED = std.mem.zeroes(windows.OVERLAPPED);
2023
2024 var n: u32 = undefined;
2025 if (windows.ws2_32.WSARecv(
2026 r.net_stream.handle,
2027 bufs.ptr,
2028 @intCast(bufs.len),
2029 &n,
2030 &flags,
2031 &overlapped,
2032 null,
2033 ) == windows.ws2_32.SOCKET_ERROR) switch (windows.ws2_32.WSAGetLastError()) {
2034 .WSA_IO_PENDING => {
2035 var result_flags: u32 = undefined;
2036 if (windows.ws2_32.WSAGetOverlappedResult(
2037 r.net_stream.handle,
2038 &overlapped,
2039 &n,
2040 windows.TRUE,
2041 &result_flags,
2042 ) == windows.FALSE) try handleRecvError(windows.ws2_32.WSAGetLastError());
2043 },
2044 else => |winsock_error| try handleRecvError(winsock_error),
20192045 };
2046
20202047 return n;
20212048 }
20222049 },
......@@ -2136,10 +2163,8 @@ pub const Stream = struct {
21362163 return io_w.consume(n);
21372164 }
21382165
2139 fn sendBufs(handle: Stream.Handle, bufs: []windows.ws2_32.WSABUF) Error!u32 {
2140 var n: u32 = undefined;
2141 const rc = windows.ws2_32.WSASend(handle, bufs.ptr, @intCast(bufs.len), &n, 0, null, null);
2142 if (rc == windows.ws2_32.SOCKET_ERROR) switch (windows.ws2_32.WSAGetLastError()) {
2166 fn handleSendError(winsock_error: windows.ws2_32.WinsockError) Error!void {
2167 switch (winsock_error) {
21432168 .WSAECONNABORTED => return error.ConnectionResetByPeer,
21442169 .WSAECONNRESET => return error.ConnectionResetByPeer,
21452170 .WSAEFAULT => unreachable, // a pointer is not completely contained in user address space.
......@@ -2155,10 +2180,37 @@ pub const Stream = struct {
21552180 .WSAESHUTDOWN => unreachable, // cannot send on a socket after write shutdown
21562181 .WSAEWOULDBLOCK => return error.WouldBlock,
21572182 .WSANOTINITIALISED => unreachable, // WSAStartup must be called before this function
2158 .WSA_IO_PENDING => unreachable, // not using overlapped I/O
2183 .WSA_IO_PENDING => unreachable,
21592184 .WSA_OPERATION_ABORTED => unreachable, // not using overlapped I/O
21602185 else => |err| return windows.unexpectedWSAError(err),
2186 }
2187 }
2188
2189 fn sendBufs(handle: Stream.Handle, bufs: []windows.ws2_32.WSABUF) Error!u32 {
2190 var n: u32 = undefined;
2191 var overlapped: windows.OVERLAPPED = std.mem.zeroes(windows.OVERLAPPED);
2192 if (windows.ws2_32.WSASend(
2193 handle,
2194 bufs.ptr,
2195 @intCast(bufs.len),
2196 &n,
2197 0,
2198 &overlapped,
2199 null,
2200 ) == windows.ws2_32.SOCKET_ERROR) switch (windows.ws2_32.WSAGetLastError()) {
2201 .WSA_IO_PENDING => {
2202 var result_flags: u32 = undefined;
2203 if (windows.ws2_32.WSAGetOverlappedResult(
2204 handle,
2205 &overlapped,
2206 &n,
2207 windows.TRUE,
2208 &result_flags,
2209 ) == windows.FALSE) try handleSendError(windows.ws2_32.WSAGetLastError());
2210 },
2211 else => |winsock_error| try handleSendError(winsock_error),
21612212 };
2213
21622214 return n;
21632215 }
21642216 },
lib/std/posix.zig+4-6
......@@ -3615,13 +3615,11 @@ pub const SocketError = error{
36153615
36163616pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t {
36173617 if (native_os == .windows) {
3618 // NOTE: windows translates the SOCK.NONBLOCK/SOCK.CLOEXEC flags into
3619 // windows-analogous operations
3618 // These flags are not actually part of the Windows API, instead they are converted here for compatibility
36203619 const filtered_sock_type = socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC);
3621 const flags: u32 = if ((socket_type & SOCK.CLOEXEC) != 0)
3622 windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT
3623 else
3624 0;
3620 var flags: u32 = windows.ws2_32.WSA_FLAG_OVERLAPPED;
3621 if ((socket_type & SOCK.CLOEXEC) != 0) flags |= windows.ws2_32.WSA_FLAG_NO_HANDLE_INHERIT;
3622
36253623 const rc = try windows.WSASocketW(
36263624 @bitCast(domain),
36273625 @bitCast(filtered_sock_type),