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: Actually close the connections properly #9

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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
44 changes: 33 additions & 11 deletions src/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,13 @@ async fn active_side(
let _d = echo_test(&connection, config, pb).await?;
}
}

// Close the connection gracefully.
// We're always the ones last receiving data, because
// `echo_test` waits for data on the connection as the last thing.
connection.close(0u32.into(), b"done");
connection.closed().await;

Ok(())
}

Expand Down Expand Up @@ -591,19 +598,34 @@ async fn recv_test(

/// Accepts connections and answers requests (echo, drain or send) as passive side.
async fn passive_side(gui: Gui, connection: Connection) -> anyhow::Result<()> {
loop {
match connection.accept_bi().await {
Ok((send, recv)) => {
if let Err(cause) = handle_test_request(send, recv, &gui).await {
eprintln!("Error handling test request {cause}");
let conn = connection.clone();
let accept_loop = async move {
let result = loop {
match conn.accept_bi().await {
Ok((send, recv)) => {
if let Err(cause) = handle_test_request(send, recv, &gui).await {
eprintln!("Error handling test request {cause}");
}
}
}
Err(cause) => {
eprintln!("error accepting bidi stream {cause}");
break Err(cause.into());
}
Err(cause) => {
eprintln!("error accepting bidi stream {cause}");
break Err(cause.into());
}
};
};
}

conn.close(0u32.into(), b"internal err");
conn.closed().await;
eprintln!("Connection closed.");

result
};
let conn_closed = async move {
connection.closed().await;
eprintln!("Connection closed.");
anyhow::Ok(())
};
futures_lite::future::race(conn_closed, accept_loop).await
}

/// Configures a relay map with some default values.
Expand Down
Loading