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(mio): drop everything before exiting the process #535

Merged
merged 1 commit into from
Feb 15, 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
29 changes: 19 additions & 10 deletions examples/miotcp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,20 @@ fn main() -> io::Result<()> {
},
token => {
// Maybe received an event for a TCP connection.
let done = if let Some(connection) = connections.get_mut(&token) {
let status = if let Some(connection) = connections.get_mut(&token) {
handle_connection_event(poll.registry(), connection, event)?
} else {
// Sporadic events happen, we can safely ignore them.
false
EventStatus::Continue
};
if done {
if let Some(mut connection) = connections.remove(&token) {
poll.registry().deregister(&mut connection)?;
match status {
EventStatus::Continue => {}
EventStatus::Done => {
if let Some(mut connection) = connections.remove(&token) {
poll.registry().deregister(&mut connection)?;
}
}
EventStatus::Exit => return Ok(()),
}
}
}
Expand All @@ -108,12 +112,17 @@ fn next(current: &mut Token) -> Token {
Token(next)
}

/// Returns `true` if the connection is done.
enum EventStatus {
Continue,
Done,
Exit,
}

fn handle_connection_event(
registry: &Registry,
connection: &mut TcpStream,
event: &Event,
) -> io::Result<bool> {
) -> io::Result<EventStatus> {
if event.is_writable() {
// We can (maybe) write to the connection.
match connection.write(DATA) {
Expand Down Expand Up @@ -171,7 +180,7 @@ fn handle_connection_event(
if let Ok(str_buf) = from_utf8(received_data) {
println!("Received data: {}", str_buf.trim_end());
if str_buf.trim_end() == "exit" {
std::process::exit(0);
return Ok(EventStatus::Exit);
}
} else {
println!("Received (none UTF-8) data: {:?}", received_data);
Expand All @@ -180,11 +189,11 @@ fn handle_connection_event(

if connection_closed {
println!("Connection closed");
return Ok(true);
return Ok(EventStatus::Done);
}
}

Ok(false)
Ok(EventStatus::Continue)
}

fn would_block(err: &io::Error) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion examples/mioudp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn main() -> io::Result<()> {
socket.send_to(&buf[..packet_size], source_address)?;
if let Ok(str_buf) = from_utf8(&buf[..packet_size]) {
if str_buf.trim_end() == "exit" {
std::process::exit(0);
return Ok(());
}
}
}
Expand Down