Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(photocraft): `--control 0` logs the port actually bound (#6427)

Khawar Zamman Wanieee8d8c4ea62
diff --git a/apps/photocraft/src/control_server.rs b/apps/photocraft/src/control_server.rs
index 30ae610b7..d73984eb8 100644
--- a/apps/photocraft/src/control_server.rs
+++ b/apps/photocraft/src/control_server.rs
@@ -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]