summaryrefslogtreecommitdiff
path: root/web/main.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-12 16:00:23 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:16 -0300
commit48281b473571d93d5a35d2250213902d1411de40 (patch)
tree1800004710583d5b73d73775bf0e5326814fcc25 /web/main.zig
parentd4dfee52a67d2e2b9a52f7e7b18b3b3cc185fac4 (diff)
downloadcloud9-48281b473571d93d5a35d2250213902d1411de40.tar.gz
cloud9-48281b473571d93d5a35d2250213902d1411de40.zip
9web: allow HTTP listener restarts without sharing live ports
Set SO_REUSEADDR before binding Linux HTTP sockets so recent connections do not block a systemd restart. Avoid SO_REUSEPORT: the std.Io combined reuse option would permit another live process to bind the same endpoint. Return the listener to std.Io for cancellable accepts. Exercise immediate dashboard/site restarts after HTTP traffic and rejection of a competing live listener in the Chromium suite. Catalogue, HTTP filesystem and all 32 original E2E scenarios also pass.
Diffstat (limited to 'web/main.zig')
-rw-r--r--web/main.zig34
1 files changed, 32 insertions, 2 deletions
diff --git a/web/main.zig b/web/main.zig
index eaf6804..9d108c6 100644
--- a/web/main.zig
+++ b/web/main.zig
@@ -186,7 +186,7 @@ pub fn main(init: std.process.Init) !void {
};
defer m.stop();
- var listener = c9.transport.listen(io, .{ .tcp = address }, max_connections) catch |err| {
+ var listener = listenHttp(io, address) catch |err| {
if (err == error.AddressInUse) {
std.debug.print("9web: cannot listen on {s}: address already in use.\nUse --listen 127.0.0.1:0 to select a free port; the URL is printed at startup.\n", .{listen_text});
} else std.debug.print("9web: cannot listen on {s}: {s}\n", .{ listen_text, @errorName(err) });
@@ -227,7 +227,7 @@ pub fn main(init: std.process.Init) !void {
const l = try allocator.create(Io.net.Server);
var bind_address = address;
bind_address.setPort(site.port);
- l.* = try c9.transport.listen(io, .{ .tcp = bind_address }, max_connections);
+ l.* = try listenHttp(io, bind_address);
try group.concurrent(io, siteAcceptLoop, .{ &app, l, &group, si });
std.debug.print("9web: site {s} on {f} from {s}\n", .{ site.name, l.socket.address, site.root });
}
@@ -278,6 +278,36 @@ pub fn main(init: std.process.Init) !void {
}
}
+fn listenHttp(io: Io, address: Io.net.IpAddress) !Io.net.Server {
+ // HTTP's short connections leave TIME_WAIT sockets behind. Reuse addresses
+ // across service restarts, but do not share a live port between instances.
+ if (@import("builtin").os.tag != .linux)
+ return address.listen(io, .{ .kernel_backlog = max_connections });
+ // std.Io's reuse_address also sets REUSEPORT. Configure only REUSEADDR
+ // before bind, then hand the socket back to std.Io for cancellable accepts.
+ const linux = std.os.linux;
+ var addr: std.c.sockaddr.storage = undefined;
+ var len = c9.transport.ipSockaddr(address, &addr);
+ const rc = linux.socket(addr.family, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, linux.IPPROTO.TCP);
+ if (linux.errno(rc) != .SUCCESS) return error.Socket;
+ const fd: i32 = @intCast(rc);
+ errdefer _ = linux.close(fd);
+ const on: c_int = 1;
+ if (linux.errno(linux.setsockopt(fd, linux.SOL.SOCKET, linux.SO.REUSEADDR, @ptrCast(&on), @sizeOf(c_int))) != .SUCCESS)
+ return error.SocketOption;
+ switch (linux.errno(linux.bind(fd, @ptrCast(&addr), len))) {
+ .SUCCESS => {},
+ .ADDRINUSE => return error.AddressInUse,
+ else => return error.Bind,
+ }
+ if (linux.errno(linux.listen(fd, max_connections)) != .SUCCESS) return error.Listen;
+ if (linux.errno(linux.getsockname(fd, @ptrCast(&addr), &len)) != .SUCCESS) return error.SocketAddress;
+ var bound = address;
+ const actual: *const std.c.sockaddr.in = @ptrCast(&addr); // port has the same offset in sockaddr_in6
+ bound.setPort(std.mem.bigToNative(u16, actual.port));
+ return .{ .socket = .{ .handle = fd, .address = bound }, .options = {} };
+}
+
fn siteAcceptLoop(app: *App, listener: *Io.net.Server, group: *Io.Group, si: usize) void {
defer listener.deinit(app.io);
while (true) {