unix: send SIGTERM instead of killing the service (#59)

This commit is contained in:
Ferdinand Schober
2023-12-27 19:56:43 +01:00
committed by GitHub
parent 4b6faea93a
commit e155819542
4 changed files with 40 additions and 24 deletions

View File

@@ -25,7 +25,7 @@ pub fn run() -> Result<()> {
loop {
let mut buf = String::new();
match std::io::stdin().read_line(&mut buf) {
Ok(0) => break,
Ok(0) => return,
Ok(len) => {
if let Some(events) = parse_cmd(buf, len) {
for event in events.iter() {
@@ -48,14 +48,16 @@ pub fn run() -> Result<()> {
}
}
Err(e) => {
log::error!("error reading from stdin: {e}");
break;
if e.kind() != ErrorKind::UnexpectedEof {
log::error!("error reading from stdin: {e}");
}
return;
}
}
}
})?;
let writer = thread::Builder::new()
let _ = thread::Builder::new()
.name("cli-frontend-notify".to_string())
.spawn(move || {
loop {
@@ -124,7 +126,7 @@ pub fn run() -> Result<()> {
}
})?;
match reader.join() {
Ok(_) => (),
Ok(_) => {}
Err(e) => {
let msg = match (e.downcast_ref::<&str>(), e.downcast_ref::<String>()) {
(Some(&s), _) => s,
@@ -134,17 +136,6 @@ pub fn run() -> Result<()> {
log::error!("reader thread paniced: {msg}");
}
}
match writer.join() {
Ok(_) => (),
Err(e) => {
let msg = match (e.downcast_ref::<&str>(), e.downcast_ref::<String>()) {
(Some(&s), _) => s,
(_, Some(s)) => s,
_ => "no panic info",
};
log::error!("writer thread paniced: {msg}");
}
}
Ok(())
}

View File

@@ -39,6 +39,15 @@ pub fn run() -> Result<()> {
let mut service = start_service()?;
frontend::run_frontend(&config)?;
log::info!("terminating service");
#[cfg(unix)]
{
// on unix we give the service a chance to terminate gracefully
let pid = service.id() as libc::pid_t;
unsafe {
libc::kill(pid, libc::SIGINT);
}
service.wait()?;
}
service.kill()?;
}

View File

@@ -198,7 +198,10 @@ impl Server {
Some(e) => e,
None => return Err::<(), anyhow::Error>(anyhow!("frontend channel closed")),
};
Self::handle_frontend_event(&producer_notify_tx, &consumer_notify_tx, &client_manager, &resolve_tx, &mut frontend, &port_tx, frontend_event).await;
let exit = Self::handle_frontend_event(&producer_notify_tx, &consumer_notify_tx, &client_manager, &resolve_tx, &mut frontend, &port_tx, frontend_event).await;
if exit {
return Ok(());
}
}
notify = frontend_notify_rx.recv() => {
let notify = match notify {
@@ -294,12 +297,23 @@ impl Server {
let reaper = task::spawn_local(async move {
tokio::select! {
_ = signal::ctrl_c() => {
producer_task.abort();
receiver_task.abort();
frontend_task.abort();
resolver_task.abort();
udp_task.abort();
log::info!("terminating service");
},
_ = producer_task => {
// TODO restart producer?
}
_ = receiver_task => {
// TODO restart producer?
}
_ = frontend_task => {
// frontend exited => exit requested
}
_ = resolver_task => {
// resolver exited
}
_ = udp_task => {
// udp exited
}
}
});