Skip to main content

Crate job_limits

Crate job_limits 

Source
Expand description

Put a memory limit on a child process, and clean up the whole process tree when a Guard is dropped.

It works the same way on unix and Windows.

unixWindows
Memory limitsetrlimit(RLIMIT_AS) before the child startsPROCESS_MEMORY of a Job Object
CleanupThe child gets its own process group; dropping the Guard sends SIGKILL to the whole groupKILL_ON_JOB_CLOSE of a Job Object
use std::process::Command;

let limits = job_limits::Limits::new().process_memory(1 << 30);
let (mut child, guard) = limits.spawn(&mut Command::new("my-tool"))?;
child.wait()?;
drop(guard); // ends any grandchildren left behind

§What the limit means

The limit applies to the memory one child process can allocate. Once it is exceeded, allocations fail (whether the child then dies is up to the child). Each OS counts differently: address space on unix, commit charge on Windows. The same value therefore does not give exactly the same behavior.

  • Linux enforces the limit. macOS may not enforce RLIMIT_AS. A failure to set the limit is ignored (the child runs without it).
  • On Windows, the limit is not in effect for the short time between starting the child and putting it into the Job.
  • On unix, if the child ends before the Guard is dropped and its process group becomes empty, the process ID may be reused by an unrelated group, which the Guard would then kill. Dropping the Guard right after waiting for the child avoids this in practice.

Structs§

Guard
Cleans up a child process. Dropping it ends the child and its descendants.
Limits
Limits to put on a child process.