Skip to main content

run_with_timeout

Function run_with_timeout 

Source
pub fn run_with_timeout(cmd: Command, timeout: Duration) -> Option<Output>
Expand description

Run cmd with a wall-clock timeout. Returns Some(Output) on completion, or None on timeout (or spawn failure / wait failure). On timeout the child is killed best-effort via a platform-native shell-out (taskkill /F /T /PID on Windows, kill -9 on POSIX) so the wedged process tree exits with the wrapper.

Stdio defaults: stdin null, stdout/stderr piped. Callers may override stdin before calling but should leave the pipes alone — the reader thread relies on them being captured to drain output while we wait.

Implementation: spawn the child, hand wait_with_output to a background thread that sends the result through a channel, then recv_timeout on the main thread. On timeout we kill the PID via the OS-native tool (we can’t call Child::kill here because the Child moved into the reader thread).