From 3743a0242bcfe47d89245084e7eac808736c8313 Mon Sep 17 00:00:00 2001 From: Igor Somov Date: Tue, 7 Apr 2026 21:33:51 -0300 Subject: [PATCH] fix(server): preserve receive timeout on Windows --- src/server/server.zig | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/server/server.zig b/src/server/server.zig index 6bf800b..4805811 100644 --- a/src/server/server.zig +++ b/src/server/server.zig @@ -408,7 +408,9 @@ pub fn Blocking(comptime H: type) type { // directly when integrating with an http server pub fn readLoop(self: *Self, hc: *HandlerConn(H)) !void { defer self.cleanupConn(hc); - try posix.setsockopt(hc.socket, posix.SOL.SOCKET, posix.SO.RCVTIMEO, &Timeout.none); + if (shouldClearReceiveTimeout(builtin.os.tag)) { + try posix.setsockopt(hc.socket, posix.SOL.SOCKET, posix.SO.RCVTIMEO, &Timeout.none); + } // In BlockingMode, we always assign a reader for the duration of the connection // In scenarios where client rarely send data, this is going to use up an unecessary amount @@ -1829,6 +1831,13 @@ fn socketWrite(socket: posix.socket_t, buf: []const u8) !usize { return posix.write(socket, buf); } +fn shouldClearReceiveTimeout(os_tag: std.Target.Os.Tag) bool { + // Regression: on Windows, resetting SO_RCVTIMEO to a zero timeval after the + // handshake can cause the next websocket read to fail immediately. Leave the + // handshake timeout in place there until the underlying socket behavior is fixed. + return os_tag != .windows; +} + fn timestamp() u32 { if (comptime @hasDecl(posix, "CLOCK") == false or posix.CLOCK == void) { return @intCast(std.time.timestamp()); @@ -1992,6 +2001,12 @@ test "Conn: close" { } } +test "shouldClearReceiveTimeout skips Windows" { + try t.expectEqual(false, shouldClearReceiveTimeout(.windows)); + try t.expectEqual(true, shouldClearReceiveTimeout(.linux)); + try t.expectEqual(true, shouldClearReceiveTimeout(.macos)); +} + fn testStream(handshake: bool) !net.Stream { const timeout = std.mem.toBytes(std.posix.timeval{ .sec = 0, .usec = 20_000 }); const address = try std.net.Address.parseIp("127.0.0.1", 9292);