Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't automatically fall back to automatic port if web socket port is already in use, only recommend using 0 instead #6296

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/re_ws_comms/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ pub enum RerunServerError {
#[error("Failed to bind to WebSocket port {0}: {1}")]
BindFailed(RerunServerPort, std::io::Error),

#[error("Failed to bind to WebSocket port {0} since the address is already in use. Use port 0 to let the OS choose a free port.")]
BindFailedAddrInUse(RerunServerPort),

#[error("Received an invalid message")]
InvalidMessagePrefix,

Expand Down
15 changes: 6 additions & 9 deletions crates/re_ws_comms/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,13 @@ impl RerunServer {
) -> Result<Self, RerunServerError> {
let bind_addr = format!("{bind_ip}:{port}");

let listener_socket = match TcpListener::bind(bind_addr) {
Ok(listener) => listener,
Err(err) if err.kind() == std::io::ErrorKind::AddrInUse => {
let bind_addr = format!("{bind_ip}:0");

TcpListener::bind(bind_addr)
.map_err(|err| RerunServerError::BindFailed(RerunServerPort(0), err))?
let listener_socket = TcpListener::bind(bind_addr).map_err(|err| {
if err.kind() == std::io::ErrorKind::AddrInUse {
RerunServerError::BindFailedAddrInUse(port)
} else {
RerunServerError::BindFailed(port, err)
}
Err(err) => return Err(RerunServerError::BindFailed(port, err)),
};
})?;

// Blocking listener socket seems much easier at first glance:
// No polling needed and as such no extra libraries!
Expand Down
Loading