Skip to content

Commit d5a9870

Browse files
authored
fix zombie processes (#2)
* fix zombie processes * kill the right thing here
1 parent 7153cf7 commit d5a9870

File tree

3 files changed

+256
-4
lines changed

3 files changed

+256
-4
lines changed

Cargo.lock

Lines changed: 221 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ edition = "2021"
77

88
[dependencies]
99
dbus = "0.9.7"
10+
env_logger = "0.11.5"
11+
log = "0.4.22"

src/main.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use dbus::{
33
blocking::Connection,
44
message::MatchRule,
55
};
6+
use env_logger::Env;
67
use std::{
78
collections::HashMap,
89
error::Error,
@@ -39,7 +40,7 @@ impl DBusInterface for DBusRunner {
3940
.with_namespaced_path(DBUS_NAMESPACE);
4041

4142
let good_to_send = Arc::clone(&self.good_to_send);
42-
self.connection.add_match(rule, move |_: (), _, msg| {
43+
self.connection.add_match(rule, move |(), _, msg| {
4344
let items: HashMap<String, Variant<Box<dyn RefArg>>> =
4445
msg.read3::<String, HashMap<_, _>, Vec<String>>().unwrap().1;
4546
if let Some(playback_status) = items.get("PlaybackStatus") {
@@ -63,6 +64,7 @@ struct IdleApp {
6364
inhibit_duration: i64,
6465
last_block_time: Arc<Mutex<Option<Instant>>>,
6566
inhibit_process: Arc<Mutex<(Option<Child>, Instant)>>,
67+
process_running: Arc<AtomicBool>,
6668
}
6769

6870
impl IdleApp {
@@ -73,6 +75,7 @@ impl IdleApp {
7375
inhibit_duration,
7476
last_block_time: Arc::new(Mutex::new(None)),
7577
inhibit_process: Arc::new(Mutex::new((None::<Child>, Instant::now()))),
78+
process_running: Arc::new(AtomicBool::new(false)),
7679
})
7780
}
7881

@@ -86,8 +89,11 @@ impl IdleApp {
8689
if current_time >= next_check {
8790
if let Ok(mut inhibit) = self.inhibit_process.lock() {
8891
if inhibit.0.is_none() || current_time >= inhibit.1 {
92+
let process_running = Arc::clone(&self.process_running);
8993
if let Some(mut child) = inhibit.0.take() {
94+
child.wait()?;
9095
let _ = child.kill();
96+
process_running.store(false, std::sync::atomic::Ordering::SeqCst);
9197
}
9298
match self
9399
.dbus_runner
@@ -99,16 +105,36 @@ impl IdleApp {
99105
.dbus_runner
100106
.good_to_send
101107
.load(std::sync::atomic::Ordering::SeqCst);
102-
if block {
108+
// Only spawn a single child if its blocking already, move on
109+
log::debug!(
110+
"should_block = {} - process_running = {:?}",
111+
block,
112+
process_running,
113+
);
114+
if block
115+
&& !process_running.load(std::sync::atomic::Ordering::SeqCst)
116+
{
117+
if let Some(mut killing) = inhibit.0.take() {
118+
killing.wait()?;
119+
killing.kill()?;
120+
}
103121
match self.run_cmd() {
104-
Ok(child) => inhibit.0 = Some(child),
122+
Ok(child) => {
123+
log::debug!("Swayidle is inhibiting now!");
124+
inhibit.0 = Some(child);
125+
process_running
126+
.store(true, std::sync::atomic::Ordering::SeqCst);
127+
}
105128
Err(e) => {
106129
eprintln!("unable to block swayidle :: {:?}", e)
107130
}
108131
}
109132
} else if !block {
110133
if let Some(mut killing) = inhibit.0.take() {
111-
let _ = killing.kill();
134+
killing.wait()?;
135+
killing.kill()?;
136+
process_running
137+
.store(false, std::sync::atomic::Ordering::SeqCst);
112138
}
113139
}
114140
}
@@ -126,6 +152,7 @@ impl IdleApp {
126152
}
127153

128154
fn run_cmd(&self) -> Result<Child, Box<dyn Error>> {
155+
log::debug!("command is spawning");
129156
match Command::new("systemd-inhibit")
130157
.arg("--what")
131158
.arg("idle")
@@ -163,6 +190,8 @@ const INHIBIT_DURATION: u64 = 55;
163190
const OVERLAP_DURATION: u64 = 5;
164191

165192
fn main() -> Result<(), Box<dyn Error>> {
193+
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
194+
log::debug!("Swaddle starting up");
166195
let app = IdleApp::new(60)?;
167196
app.run()
168197
}

0 commit comments

Comments
 (0)