Display actual port when binding --port 0

This change causes lightpanda to display the actual port number (instead of 0)
when binding a dynamic port (--port 0), which makes automating based on
scraping lightpanda output simple.
This commit is contained in:
Patrick Wyatt
2026-04-29 21:44:41 -07:00
parent 1f40c30901
commit 47d96ab8ad
2 changed files with 11 additions and 3 deletions

View File

@@ -57,8 +57,9 @@ pub fn init(app: *App, address: net.Address) !*Server {
.clients_pool = std.heap.MemoryPool(Client).init(allocator),
};
try self.app.network.bind(address, self, onAccept);
log.info(.app, "server running", .{ .address = address });
var bound_address = address;
try self.app.network.bind(&bound_address, self, onAccept);
log.info(.app, "server running", .{ .address = bound_address });
return self;
}

View File

@@ -351,7 +351,7 @@ pub fn deinit(self: *Network) void {
pub fn bind(
self: *Network,
address: net.Address,
address: *net.Address,
ctx: *anyopaque,
on_accept: *const fn (ctx: *anyopaque, socket: posix.socket_t) void,
) !void {
@@ -367,6 +367,13 @@ pub fn bind(
try posix.bind(listener, &address.any, address.getOsSockLen());
try posix.listen(listener, self.config.maxPendingConnections());
// When the caller requests port 0, the OS assigns an ephemeral port; read
// the actual bound address back so callers (e.g. logging) see the real port.
var bound: posix.sockaddr.storage = undefined;
var bound_len: posix.socklen_t = @sizeOf(posix.sockaddr.storage);
try posix.getsockname(listener, @ptrCast(&bound), &bound_len);
address.* = net.Address.initPosix(@ptrCast(@alignCast(&bound)));
if (self.listener != null) return error.TooManyListeners;
self.listener = .{