Skip to content

Commit

Permalink
Ensure cgroup is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Furisto committed Sep 25, 2021
1 parent 4224d56 commit d978b03
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
19 changes: 19 additions & 0 deletions cgroups/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
fs::{self, File},
io::{BufRead, BufReader, Write},
path::{Path, PathBuf},
time::Duration,
};

use anyhow::{bail, Context, Result};
Expand Down Expand Up @@ -355,3 +356,21 @@ pub(crate) fn default_devices() -> Vec<LinuxDevice> {
},
]
}

pub(crate) fn delete_with_retry<P: AsRef<Path>>(path: P) -> Result<()> {
let mut attempts = 0;
let mut delay = Duration::from_millis(10);
let path = path.as_ref();

while attempts < 5 {
if fs::remove_dir(path).is_ok() {
return Ok(());
}

std::thread::sleep(delay);
attempts += attempts;
delay *= attempts;
}

bail!("could not delete {:?}", path)
}
2 changes: 1 addition & 1 deletion cgroups/src/v1/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl CgroupManager for Manager {
let _ = nix::sys::signal::kill(Pid::from_raw(pid), nix::sys::signal::SIGKILL);
}

util::delete_with_retry(cgroup_path.1)?;
common::delete_with_retry(cgroup_path.1)?;
}
}

Expand Down
27 changes: 2 additions & 25 deletions cgroups/src/v1/util.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
time::Duration,
};
use std::{collections::HashMap, path::PathBuf};

use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, Result};
use procfs::process::Process;

use super::{controller_type::CONTROLLERS, ControllerType};
Expand Down Expand Up @@ -56,21 +51,3 @@ pub fn get_subsystem_mount_point(subsystem: &ControllerType) -> Result<PathBuf>
.map(|m| m.mount_point)
.ok_or_else(|| anyhow!("could not find mountpoint for {}", subsystem))
}

pub(crate) fn delete_with_retry<P: AsRef<Path>>(path: P) -> Result<()> {
let mut attempts = 0;
let mut delay = Duration::from_millis(10);
let path = path.as_ref();

while attempts < 5 {
if fs::remove_dir(path).is_ok() {
return Ok(());
}

std::thread::sleep(delay);
attempts += attempts;
delay *= attempts;
}

bail!("could not delete {:?}", path)
}
14 changes: 12 additions & 2 deletions cgroups/src/v2/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,18 @@ impl CgroupManager for Manager {
}

fn remove(&self) -> Result<()> {
log::debug!("remove cgroup {:?}", self.full_path);
fs::remove_dir(&self.full_path)?;
if self.full_path.exists() {
log::debug!("remove cgroup {:?}", self.full_path);
let procs_path = self.full_path.join(CGROUP_PROCS);
let procs = fs::read_to_string(&procs_path)?;

for line in procs.lines() {
let pid: i32 = line.parse()?;
let _ = nix::sys::signal::kill(Pid::from_raw(pid), nix::sys::signal::SIGKILL);
}

common::delete_with_retry(&self.full_path)?;
}

Ok(())
}
Expand Down

0 comments on commit d978b03

Please sign in to comment.