Skip to main content

run_with_timeout

Function run_with_timeout 

Source
pub fn run_with_timeout<F, T>(timeout: Duration, operation: F) -> Result<T>
where F: FnOnce(Arc<AtomicBool>) -> Result<T> + Send + 'static, T: Send + 'static,
Expand description

Run an operation with timeout protection

Spawns the operation in a dedicated thread and enforces the specified timeout. If the operation doesn’t complete within the timeout, the cancellation flag is set and an error is returned.

§Arguments

  • timeout - Maximum duration to wait for operation completion (0 = no timeout)
  • operation - The operation to run (receives cancellation flag)

§Returns

The operation’s result if it completes within timeout, otherwise a timeout error

§Errors

Returns an error if:

  • Operation times out
  • Plugin thread panics during execution
  • Operation returns an error

§Examples

use crush_core::plugin::timeout::run_with_timeout;
use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use std::time::Duration;

let timeout = Duration::from_secs(5);
let result = run_with_timeout(timeout, |cancel_flag| {
    // Operation code here
    Ok(vec![1, 2, 3])
});