Exit on compositor exit

This commit is contained in:
Lucy 2022-07-12 15:57:10 -04:00
parent bc07650f01
commit 378f137d0a
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1
2 changed files with 20 additions and 15 deletions

View file

@ -18,6 +18,7 @@ use tokio::{
mpsc::{self, unbounded_channel}, mpsc::{self, unbounded_channel},
oneshot, oneshot,
}, },
task::JoinHandle,
}; };
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::Instrument; use tracing::Instrument;
@ -190,7 +191,7 @@ pub fn run_compositor(
token: CancellationToken, token: CancellationToken,
mut socket_rx: mpsc::UnboundedReceiver<Vec<UnixStream>>, mut socket_rx: mpsc::UnboundedReceiver<Vec<UnixStream>>,
env_tx: oneshot::Sender<HashMap<String, String>>, env_tx: oneshot::Sender<HashMap<String, String>>,
) -> Result<()> { ) -> Result<JoinHandle<Result<()>>> {
let (tx, mut rx) = unbounded_channel::<ProcessEvent>(); let (tx, mut rx) = unbounded_channel::<ProcessEvent>();
let (session, comp) = UnixStream::pair().wrap_err("failed to create pair of unix sockets")?; let (session, comp) = UnixStream::pair().wrap_err("failed to create pair of unix sockets")?;
let (mut session_rx, mut session_tx) = session.into_split(); let (mut session_rx, mut session_tx) = session.into_split();
@ -206,7 +207,7 @@ pub fn run_compositor(
}; };
let span = info_span!(parent: None, "cosmic-comp"); let span = info_span!(parent: None, "cosmic-comp");
let _span = span.clone(); let _span = span.clone();
tokio::spawn( Ok(tokio::spawn(
async move { async move {
ProcessHandler::new(tx, &token).run( ProcessHandler::new(tx, &token).run(
"cosmic-comp", "cosmic-comp",
@ -236,6 +237,5 @@ pub fn run_compositor(
Result::<()>::Ok(()) Result::<()>::Ok(())
} }
.instrument(_span), .instrument(_span),
); ))
Ok(())
} }

View file

@ -35,9 +35,8 @@ async fn main() -> Result<()> {
let token = CancellationToken::new(); let token = CancellationToken::new();
let (socket_tx, socket_rx) = mpsc::unbounded_channel(); let (socket_tx, socket_rx) = mpsc::unbounded_channel();
let (env_tx, env_rx) = oneshot::channel(); let (env_tx, env_rx) = oneshot::channel();
if let Err(err) = comp::run_compositor(token.child_token(), socket_rx, env_tx) { let compositor_handle = comp::run_compositor(token.child_token(), socket_rx, env_tx)
error!("compositor errored: {:?}", err); .wrap_err("failed to start compositor")?;
}
systemd::start_systemd_target() systemd::start_systemd_target()
.await .await
.wrap_err("failed to start systemd target")?; .wrap_err("failed to start systemd target")?;
@ -69,17 +68,23 @@ async fn main() -> Result<()> {
socket_tx.send(sockets).unwrap(); socket_tx.send(sockets).unwrap();
let mut signals = Signals::new(vec![libc::SIGTERM, libc::SIGINT]).unwrap(); let mut signals = Signals::new(vec![libc::SIGTERM, libc::SIGINT]).unwrap();
while let Some(signal) = signals.next().await { loop {
match signal { tokio::select! {
libc::SIGTERM | libc::SIGINT => { _ = compositor_handle => {
info!("received request to terminate"); info!("compositor exited");
token.cancel();
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
break; break;
},
signal = signals.next() => match signal {
Some(libc::SIGTERM | libc::SIGINT) => {
info!("received request to terminate");
break;
}
Some(signal) => unreachable!("received unhandled signal {}", signal),
None => break,
} }
_ => unreachable!("received unhandled signal {}", signal),
} }
} }
token.cancel();
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
Ok(()) Ok(())
} }