Skip to content

Commit 518afe8

Browse files
Qiumixfaervan
authored andcommitted
ref(media&volume): using tokio command to drop resources
1 parent 4fefac3 commit 518afe8

3 files changed

Lines changed: 4 additions & 21 deletions

File tree

src/modules/media.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
};
2828
use crate::{impl_on_click, impl_wrapper};
2929

30-
use super::{KillOnDrop, Module};
30+
use super::Module;
3131

3232
#[derive(Debug, Builder)]
3333
pub struct MediaMod {
@@ -485,6 +485,7 @@ impl Module for MediaMod {
485485
"playerctl --follow metadata --format '{\"title\": \"{{title}}\", \"artist\": \"{{artist}}\", \"album\": \"{{album}}\", \"art_url\": \"{{mpris:artUrl}}\", \"length\": {{mpris:length}}, \"status\": \"{{status}}\", \"player\": \"{{playerName}}\"}'",
486486
)
487487
.stdout(Stdio::piped())
488+
.kill_on_drop(true)
488489
.spawn()
489490
.expect("Failed to read output from playerctl");
490491

@@ -493,10 +494,6 @@ impl Module for MediaMod {
493494
.take()
494495
.expect("child did not have a handle to stdout");
495496

496-
// Kill playerctl when the subscription ends (e.g. on reload),
497-
// so it doesn't leak and accumulate processes.
498-
let _kill_on_drop = KillOnDrop(child);
499-
500497
let mut reader = BufReader::new(stdout).lines();
501498
let mut last_track = String::new();
502499

src/modules/mod.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,6 @@ pub trait Module: Any + Debug + Send + Sync + Downcast {
160160
}
161161
impl_downcast!(Module);
162162

163-
/// Kills the wrapped child process when dropped, so subscription child
164-
/// processes (e.g. `pactl subscribe`, `playerctl --follow`) don't leak when
165-
/// the subscription ends (e.g. on config reload).
166-
pub struct KillOnDrop(pub tokio::process::Child);
167-
168-
impl Drop for KillOnDrop {
169-
fn drop(&mut self) {
170-
let _ = self.0.start_kill();
171-
}
172-
}
173-
174163
pub trait Action: Any + Debug + Send + Sync + Downcast {
175164
fn as_message(&self) -> Message;
176165
}

src/modules/volume.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
};
2222
use crate::{impl_on_click, impl_wrapper};
2323

24-
use super::{KillOnDrop, Module};
24+
use super::Module;
2525

2626
#[derive(Default, Debug, Builder)]
2727
pub struct VolumeMod {
@@ -100,6 +100,7 @@ impl Module for VolumeMod {
100100
let mut child = Command::new("sh")
101101
.arg("-c")
102102
.arg("pactl subscribe")
103+
.kill_on_drop(true)
103104
.stdout(Stdio::piped())
104105
.spawn()
105106
.expect("Failed to spawn pactl to monitor volume changes");
@@ -109,10 +110,6 @@ impl Module for VolumeMod {
109110
.take()
110111
.expect("child did not have a handle to stdout");
111112

112-
// Kill pactl when the subscription ends (e.g. on reload), so
113-
// it doesn't leak and exhaust the pulse client limit.
114-
let _kill_on_drop = KillOnDrop(child);
115-
116113
let mut reader = BufReader::new(stdout).lines();
117114

118115
while let Some(line) = reader.next_line().await.unwrap() {

0 commit comments

Comments
 (0)