cputimeout

Crates.iocputimeout
lib.rscputimeout
version0.1.0
created_at2025-07-19 16:08:02.119768+00
updated_at2025-07-19 16:08:02.119768+00
descriptiona very unsafe way of time-outing a cpu-bounded task.
homepage
repositoryhttps://github.com/sahandevs/cputimeout
max_upload_size
id1760354
size24,345
Sahand Akbarzadeh (sahandevs)

documentation

README

cputimeout

very unsafe way of time-outing a cpu-bounded task.

(use this crate at your own risk!)

Usage

  • step 0: don't use this :)
  • step 1: but if you have to...
cputimeout = "*"
use cputimeout::timeout_cpu;

timeout_cpu(
    || loop {},
    std::time::Duration::from_millis(100),
).unwrap();

Nesting

state: just a PoC solution, works up to 10 nested calls

// inner smaller than outer
let r = timeout_cpu(
    || timeout_cpu(|| loop {}, Duration::from_millis(50)),
    Duration::from_millis(100),
);

assert!(matches!(r, Ok(Err(Error::TimedOut))));

// outer smaller than inner
let r = timeout_cpu(
    || timeout_cpu(|| loop {}, Duration::from_millis(100)),
    Duration::from_millis(50),
);
assert!(matches!(r, Err(Error::TimedOut)));

Allocation tracker

state: just a PoC solution, there are some corner cases.

if the task timeouts, this crate does a jmp so it may not call free() or Drop on allocated resources. If you can, allocate everything outside the function and only pass reference to it. If you can't (for example you are using a C library that calls malloc) you can use this feature. this interposes malloc calls and tracks all allocations that the task makes and calls free on them when timeouts.

just add mem-tracker to features list and everything should work.

TODO

  • follow threads
  • resource tracker
  • tokio support
  • benchmark
  • tests
  • documentation
Commit count: 0

cargo fmt