diff --git a/Cargo.lock b/Cargo.lock index 0df3b36..44c0edc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -241,6 +241,7 @@ dependencies = [ "futures-util", "libc", "nix 0.24.1", + "scopeguard", "sendfd", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 783066d..b1b7e62 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ color-eyre = "0.6" futures-util = "0.3" libc = "0.2" nix = { version = "0.24", features = ["fs"], default-features = false } +scopeguard = "1.1.0" sendfd = { version = "0.4", features = ["tokio"] } serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src/main.rs b/src/main.rs index 6e87af9..f13cb1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,12 @@ async fn main() -> Result<()> { systemd::start_systemd_target() .await .wrap_err("failed to start systemd target")?; + // Always stop the target when the process exits or panics. + scopeguard::defer! { + if let Ok(manager) = systemd_client::manager::build_blocking_proxy() { + manager.stop_unit("cosmic-session.target", "replace").ok(); + } + } let env_vars = env_rx .await .expect("failed to receive environmental variables")