Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure cgroup error behavior is consistent with runc #333

Merged
merged 2 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 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,34 @@ pub(crate) fn default_devices() -> Vec<LinuxDevice> {
},
]
}

/// Attempts to delete the path the requested number of times.
pub(crate) fn delete_with_retry<P: AsRef<Path>, L: Into<Option<Duration>>>(
path: P,
retries: u32,
limit_backoff: L,
) -> Result<()> {
let mut attempts = 0;
let mut delay = Duration::from_millis(10);
let path = path.as_ref();
let limit = if let Some(backoff) = limit_backoff.into() {
backoff
} else {
Duration::MAX
};

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

std::thread::sleep(delay);
attempts += attempts;
delay *= attempts;
if delay > limit {
delay = limit;
}
}

bail!("could not delete {:?}", path)
}
3 changes: 2 additions & 1 deletion cgroups/src/v1/manager.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs;
use std::path::Path;
use std::time::Duration;
use std::{collections::HashMap, path::PathBuf};

use anyhow::bail;
Expand Down Expand Up @@ -161,7 +162,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, 4, Duration::from_millis(100))?;
}
}

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)
}
15 changes: 13 additions & 2 deletions cgroups/src/v2/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{
fs::{self},
os::unix::fs::PermissionsExt,
path::{Path, PathBuf},
time::Duration,
};

use anyhow::Result;
Expand Down Expand Up @@ -120,8 +121,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, 4, Duration::from_millis(100))?;
}

Ok(())
}
Expand Down
45 changes: 41 additions & 4 deletions src/container/builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
syscall::Syscall,
utils,
};
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use cgroups::{self, common::CgroupManager};
use nix::unistd::Pid;
use oci_spec::runtime::{LinuxResources, Spec};
Expand Down Expand Up @@ -44,7 +44,15 @@ pub(super) struct ContainerBuilderImpl<'a> {

impl<'a> ContainerBuilderImpl<'a> {
pub(super) fn create(&mut self) -> Result<()> {
self.run_container()
if let Err(outer) = self.run_container().context("failed to create container") {
if let Err(inner) = self.cleanup_container() {
return Err(outer.context(inner));
}

return Err(outer);
}

Ok(())
}

fn run_container(&mut self) -> Result<()> {
Expand Down Expand Up @@ -170,6 +178,33 @@ impl<'a> ContainerBuilderImpl<'a> {

Ok(())
}

fn cleanup_container(&self) -> Result<()> {
let linux = self.spec.linux.as_ref().context("no linux in spec")?;
let cgroups_path = utils::get_cgroup_path(&linux.cgroups_path, &self.container_id);
let cmanager = cgroups::common::create_cgroup_manager(&cgroups_path, self.use_systemd)?;

let mut errors = Vec::new();
if let Err(e) = cmanager.remove().context("failed to remove cgroup") {
errors.push(e.to_string());
}

if let Some(container) = &self.container {
if container.root.exists() {
if let Err(e) = fs::remove_dir_all(&container.root)
.with_context(|| format!("could not delete {}", container.root.display()))
{
errors.push(e.to_string());
}
}
}

if !errors.is_empty() {
bail!("failed to cleanup container: {}", errors.join(";"));
}

Ok(())
}
}

fn setup_mapping(rootless: &Rootless, pid: Pid) -> Result<()> {
Expand Down Expand Up @@ -201,10 +236,12 @@ fn apply_cgroups<C: CgroupManager + ?Sized>(
};
cmanager
.add_task(pid)
.context("Failed to add tasks to cgroup manager")?;
.with_context(|| format!("failed to add task {} to cgroup manager", pid))?;

cmanager
.apply(&controller_opt)
.context("Failed to apply resource limits through cgroup")?;
.context("failed to apply resource limits to cgroup")?;

Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub fn set_name(_name: &str) -> Result<()> {
pub fn get_cgroup_path(cgroups_path: &Option<PathBuf>, container_id: &str) -> PathBuf {
match cgroups_path {
Some(cpath) => cpath.clone(),
None => PathBuf::from(format!("/youki/{}", container_id)),
None => PathBuf::from(container_id),
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ mod tests {
let cid = "sample_container_id";
assert_eq!(
get_cgroup_path(&None, cid),
PathBuf::from("/youki/sample_container_id")
PathBuf::from("sample_container_id")
);
assert_eq!(
get_cgroup_path(&Some(PathBuf::from("/youki")), cid),
Expand Down