Skip to main content

Mountain/ApplicationState/Internal/Recovery/RecoverState/
SafeStateOperationWithTimeout.rs

1#![allow(non_snake_case)]
2
3//! Run a synchronous, blocking state operation off-thread with a hard
4//! timeout. The thread is allowed to finish in the background after
5//! the timeout fires; only the receiver gives up. Used during
6//! recovery where a hung repair must not stall the main runtime.
7
8use CommonLibrary::Error::CommonError::CommonError;
9
10use crate::dev_log;
11
12pub fn Fn<T, F>(Operation:F, TimeoutMs:u64, OperationName:&str) -> Result<T, CommonError>
13where
14	F: FnOnce() -> Result<T, CommonError> + Send + 'static,
15	T: Send + 'static, {
16	let (Sender, Receiver) = std::sync::mpsc::channel();
17
18	std::thread::spawn(move || {
19		let _ = Sender.send(Operation());
20	});
21
22	match Receiver.recv_timeout(std::time::Duration::from_millis(TimeoutMs)) {
23		Ok(Result) => Result,
24		Err(_) => {
25			dev_log!(
26				"lifecycle",
27				"error: [RecoverState] Operation '{}' timed out after {}ms",
28				OperationName,
29				TimeoutMs
30			);
31			Err(CommonError::Unknown { Description:format!("Operation '{}' timed out", OperationName) })
32		},
33	}
34}