Skip to main content

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

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