Skip to content

Commit

Permalink
Try implementing solution pointed by @yihuaf in #2144 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
YJDoc2 committed Jan 4, 2024
1 parent b60889d commit a8fd37e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions crates/libcontainer/src/process/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,24 @@ fn clone(cb: CloneCb, flags: u64, exit_signal: Option<u64>) -> Result<Pid, Clone
// arg is actually a raw pointer to the Box closure. so here, we re-box the
// pointer back into a box closure so the main takes ownership of the
// memory. Then we can call the closure.

Check warning on line 207 in crates/libcontainer/src/process/fork.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, gnu)

Diff in /home/runner/work/youki/youki/crates/libcontainer/src/process/fork.rs

Check warning on line 207 in crates/libcontainer/src/process/fork.rs

View workflow job for this annotation

GitHub Actions / check (x86_64, musl)

Diff in /home/runner/work/youki/youki/crates/libcontainer/src/process/fork.rs

Check warning on line 207 in crates/libcontainer/src/process/fork.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, gnu)

Diff in /home/runner/work/youki/youki/crates/libcontainer/src/process/fork.rs

Check warning on line 207 in crates/libcontainer/src/process/fork.rs

View workflow job for this annotation

GitHub Actions / check (aarch64, musl)

Diff in /home/runner/work/youki/youki/crates/libcontainer/src/process/fork.rs
// The reason for test/non-test split via cfg is that after forking,
// the malloc and free call (from Box) can race and hang up. This is seen only in
// CI tests due to tests being run in parallel via cargo, so leaking memory by leaking
// box to prevent it, only in test config. See https://github.com/containers/youki/issues/2144
// and https://github.com/containers/youki/issues/2144#issuecomment-1624844755
// for more detailed analysis
#[cfg(not(test))]
extern "C" fn main(data: *mut libc::c_void) -> libc::c_int {
unsafe { Box::from_raw(data as *mut CloneCb)() }
}
#[cfg(test)]
extern "C" fn main(data: *mut libc::c_void) -> libc::c_int {
let mut func = unsafe { Box::from_raw(data as *mut CloneCb) };
let ret = func();
Box::into_raw(func);
ret
}

// The nix::sched::clone wrapper doesn't provide the right interface. Using
// the clone syscall is one of the rare cases where we don't want rust to
Expand Down

0 comments on commit a8fd37e

Please sign in to comment.