Merge PR #6468: fix(photocraft): --control 0 logs the port actually bound (#6427)

This commit is contained in:
aiongg committed 2026-10-22 02:24:53 +00:00
commit 28a1ee7b6e
1 file changed
+22 -1
+22 -1
View File
@@ -25,7 +25,7 @@ pub fn start(port: u16, token: String, ctx: egui::Context) -> Receiver<ControlRe
return rx;
}
};
eprintln!("photocraft: control server listening on 127.0.0.1:{port}");
eprintln!("photocraft: control server listening on {}", listening_addr(&listener, port));
std::thread::spawn(move || {
let limiter = ConnectionLimiter::new(MAX_CONNECTIONS);
let token = Arc::new(token);
@@ -47,6 +47,16 @@ pub fn start(port: u16, token: String, ctx: egui::Context) -> Receiver<ControlRe
rx
}
/// The address `listener` is bound to: with `--control 0` the OS picks the port, and a client
/// needs that one, not the requested 0 (#6427). Falls back to the requested port if the OS
/// can't report it.
fn listening_addr(listener: &TcpListener, requested_port: u16) -> String {
match listener.local_addr() {
Ok(addr) => addr.to_string(),
Err(_) => format!("127.0.0.1:{requested_port}"),
}
}
fn serve(stream: TcpStream, token: &str, tx: Sender<ControlRequest>, ctx: egui::Context) {
serve_with_auth_deadline(stream, token, tx, ctx, AUTH_DEADLINE);
}
@@ -131,6 +141,17 @@ mod tests {
use super::*;
use std::io::BufRead;
/// #6427: with port 0 the log names the port the OS bound, which a client can connect to.
#[test]
fn port_zero_logs_the_bound_port() {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let bound = listener.local_addr().unwrap();
assert_ne!(bound.port(), 0);
let logged = listening_addr(&listener, 0);
assert_eq!(logged, format!("127.0.0.1:{}", bound.port()));
TcpStream::connect(&logged).unwrap();
}
/// #4965: a connection that keeps sending without authenticating is closed at the deadline
/// (releasing its connection permit), and nothing it sent is dispatched.
#[test]