Skip to content

Commit

Permalink
fix(mio): drop everything before exiting the process
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Feb 15, 2024
1 parent 0387adf commit 1ae4a83
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
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

0 comments on commit 1ae4a83

Please sign in to comment.