From 7cd1cda42f0d436443d969a05ea94b8e365bba41 Mon Sep 17 00:00:00 2001 From: Markus Rudy Date: Tue, 16 Jun 2026 09:05:54 +0200 Subject: [PATCH] Fix race condition in create_v2_cgroup There used to be a race condition while creating the cgroup hierarchy: if a parent directory was created after .exists() but before .create_dir, the function failed unnecessarily. This commit changes the function to use create_dir_all, which is the atomic variant of the above pattern, and fixes some surrounding docstrings on the way. Signed-off-by: Markus Rudy --- src/fs/cgroup.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/fs/cgroup.rs b/src/fs/cgroup.rs index f92fd99..6daa3e0 100644 --- a/src/fs/cgroup.rs +++ b/src/fs/cgroup.rs @@ -550,18 +550,18 @@ fn create_v2_cgroup( // path: "a/b/c" let elements = path.split('/').collect::>(); let last_index = elements.len() - 1; + // Build up the directory hierarchy element by element, enabling the controllers for all + // parents along the way. for (i, ele) in elements.iter().enumerate() { // ROOT/a fp.push(ele); - // create dir, need not check if is a file or directory - if !fp.exists() { - if let Err(e) = std::fs::create_dir(fp.clone()) { - return Err(Error::with_cause(ErrorKind::FsError, e)); - } + // create dir if necessary + if let Err(e) = std::fs::create_dir_all(fp.clone()) { + return Err(Error::with_cause(ErrorKind::FsError, e)); } if i < last_index { - // enable controllers for substree + // enable controllers for subtree enable_controllers(&controllers, &fp); } }