diff options
| -rw-r--r-- | docs/catalog.md | 2 | ||||
| -rw-r--r-- | test/web/catalog_ui.mjs | 9 | ||||
| -rw-r--r-- | web/main.zig | 34 |
3 files changed, 43 insertions, 2 deletions
diff --git a/docs/catalog.md b/docs/catalog.md index 24b6fb9..79e5580 100644 --- a/docs/catalog.md +++ b/docs/catalog.md @@ -181,6 +181,8 @@ history, direct tab links, builds through `/fs`, logs in the file browser, preservation of unsaved edits and focused controls, and mobile layout. It writes inspection screenshots to `.zig-cache/catalog-ui/`. It uses the installed `google-chrome-stable` binary (or `CLOUD9_CHROME`), with no npm dependencies. +It also checks that live HTTP ports stay exclusive and recent connections do not +prevent an immediate restart of the dashboard or site listeners. ## Installed workstation configuration diff --git a/test/web/catalog_ui.mjs b/test/web/catalog_ui.mjs index bed40b2..d030842 100644 --- a/test/web/catalog_ui.mjs +++ b/test/web/catalog_ui.mjs @@ -119,6 +119,15 @@ try { assert.equal(await evaluate(`document.documentElement.scrollWidth <= innerWidth`), true, 'mobile page does not overflow'); await fs.writeFile('.zig-cache/catalog-ui/mobile.png', Buffer.from((await cdp('Page.captureScreenshot', { format: 'png' })).data, 'base64')); assert.deepEqual(exceptions, []); + // A live port remains exclusive, but TIME_WAIT must not break a restart. + const competitor = start(binary, ['--listen', `127.0.0.1:${dashboard}`]); + await wait(() => competitor.exitCode !== null, 'competing listener refused'); + assert.notEqual(competitor.exitCode, 0); + assert.match(competitor.output, /address already in use/); + web.kill('SIGTERM'); await once(web, 'exit'); + await fs.unlink(path.join(work, 'catalog.sock')); + start(binary, ['--listen', `127.0.0.1:${dashboard}`, '--catalog', path.join(work, 'config.json'), '--catalog-socket', path.join(work, 'catalog.sock'), '--state-dir', path.join(work, 'state')]); + await wait(async () => (await fetch(url)).ok && (await fetch(`http://127.0.0.1:${sitePort}`)).ok, 'immediate restart with recent HTTP traffic'); console.log('catalog_ui: shared shell/style, tab history, file/log navigation, build control, focus, dirty edits and mobile layout passed'); } catch (e) { for (const child of children) console.error(child.output.slice(-5000)); 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) { |
