| Crates.io | cputimeout |
| lib.rs | cputimeout |
| version | 0.1.0 |
| created_at | 2025-07-19 16:08:02.119768+00 |
| updated_at | 2025-07-19 16:08:02.119768+00 |
| description | a very unsafe way of time-outing a cpu-bounded task. |
| homepage | |
| repository | https://github.com/sahandevs/cputimeout |
| max_upload_size | |
| id | 1760354 |
| size | 24,345 |
very unsafe way of time-outing a cpu-bounded task.
(use this crate at your own risk!)
cputimeout = "*"
use cputimeout::timeout_cpu;
timeout_cpu(
|| loop {},
std::time::Duration::from_millis(100),
).unwrap();
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)));
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.