Skip to content

Commit

Permalink
fix(server):shutdown all sockets when exit
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Jul 26, 2024
1 parent 81be656 commit 1aa2bb8
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions gdrust/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,16 @@ pub async fn lib_main() -> Result<(), Box<dyn Error>> {
.accept_sockets(shutdown_sender_clone, shutdown_receiver_clone)
.await
});
let shutdown_sender_clone = shutdown_sender.clone();
tokio::spawn(async move {
match tokio::signal::ctrl_c().await {
Ok(()) => {
log::info!("Exiting now...");
shutdown_sender.send(())?;
shutdown_sender_clone.send(())?;
}
Err(err) => {
log::error!("Unable to listen for shutdown signal: {}", err);
shutdown_sender.send(())?;
log::error!("Unable to listen to shutdown signal: {}", err);
shutdown_sender_clone.send(())?;
}
}
anyhow::Ok(())
Expand Down Expand Up @@ -187,7 +188,9 @@ pub async fn lib_main() -> Result<(), Box<dyn Error>> {
anyhow::Ok(())
};
select! {
_ = input_loop => {},
_ = input_loop => {
shutdown_sender.send(())?;
},
_ = shutdown_receiver.recv() => {
log::info!("Command loop exited")
}
Expand Down

0 comments on commit 1aa2bb8

Please sign in to comment.