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

Fix crash when trying to listen on a taken TCP port #1650

Merged
merged 3 commits into from
Mar 21, 2023
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
50 changes: 26 additions & 24 deletions crates/re_sdk_comms/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::time::Instant;

use anyhow::Context as _;
use anyhow::Context;
use rand::{Rng as _, SeedableRng};

use re_log_types::{LogMsg, TimePoint, TimeType, TimelineName};
Expand All @@ -29,28 +29,11 @@ impl Default for ServerOptions {
}

async fn listen_for_new_clients(
port: u16,
listener: TcpListener,
options: ServerOptions,
tx: Sender<LogMsg>,
mut shutdown_rx: tokio::sync::broadcast::Receiver<()>,
) {
let bind_addr = format!("0.0.0.0:{port}");

let listener = TcpListener::bind(&bind_addr)
.await
.with_context(|| format!("Failed to bind TCP address {bind_addr:?}"))
.unwrap();

if options.quiet {
re_log::debug!(
"Hosting a SDK server over TCP at {bind_addr}. Connect with the Rerun logging SDK."
);
} else {
re_log::info!(
"Hosting a SDK server over TCP at {bind_addr}. Connect with the Rerun logging SDK."
);
}

loop {
let incoming = tokio::select! {
res = listener.accept() => res,
Expand All @@ -74,18 +57,37 @@ async fn listen_for_new_clients(
///
/// ``` no_run
/// # use re_sdk_comms::{serve, ServerOptions};
/// let (sender, receiver) = tokio::sync::broadcast::channel(1);
/// let log_msg_rx = serve(80, ServerOptions::default(), receiver)?;
/// # Ok::<(), anyhow::Error>(())
/// #[tokio::main]
/// async fn main() {
/// let (sender, receiver) = tokio::sync::broadcast::channel(1);
/// let log_msg_rx = serve(80, ServerOptions::default(), receiver).await.unwrap();
/// }
/// ```
pub fn serve(
pub async fn serve(
port: u16,
options: ServerOptions,
shutdown_rx: tokio::sync::broadcast::Receiver<()>,
) -> anyhow::Result<Receiver<LogMsg>> {
let (tx, rx) = re_smart_channel::smart_channel(re_smart_channel::Source::TcpServer { port });

tokio::spawn(listen_for_new_clients(port, options, tx, shutdown_rx));
let bind_addr = format!("0.0.0.0:{port}");
let listener = TcpListener::bind(&bind_addr).await.with_context(|| {
format!(
"Failed to bind TCP address {bind_addr:?}. Another Rerun instance is probably running."
)
})?;

if options.quiet {
re_log::debug!(
"Hosting a SDK server over TCP at {bind_addr}. Connect with the Rerun logging SDK."
);
} else {
re_log::info!(
"Hosting a SDK server over TCP at {bind_addr}. Connect with the Rerun logging SDK."
);
}

tokio::spawn(listen_for_new_clients(listener, options, tx, shutdown_rx));

Ok(rx)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rerun/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ async fn run_impl(
// `rerun.spawn()` doesn't need to log that a connection has been made
quiet: call_source.is_python(),
};
re_sdk_comms::serve(args.port, server_options, shutdown_rx.resubscribe())?
re_sdk_comms::serve(args.port, server_options, shutdown_rx.resubscribe()).await?
}

#[cfg(not(feature = "server"))]
Expand Down