Skip to main content

Mountain/RunTime/Shutdown/
DisposeTerminalsSafely.rs

1#![allow(non_snake_case)]
2
3//! Dispose every active PTY through `TerminalProvider::DisposeTerminal`.
4//! Errors per terminal are collected; the loop never aborts early.
5
6use std::sync::Arc;
7
8use CommonLibrary::{
9	Environment::Requires::Requires,
10	Error::CommonError::CommonError,
11	Terminal::TerminalProvider::TerminalProvider as TerminalProviderTrait,
12};
13
14use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
15
16impl ApplicationRunTime {
17	pub async fn DisposeTerminalsSafely(&self) -> Result<(), CommonError> {
18		let TerminalProvider:Arc<dyn TerminalProviderTrait> = self.Environment.Require();
19
20		let TerminalIdentifiers:Vec<u64> = {
21			let TerminalsGuard = self
22				.Environment
23				.ApplicationState
24				.Feature
25				.Terminals
26				.ActiveTerminals
27				.lock()
28				.map_err(|E| CommonError::StateLockPoisoned { Context:E.to_string() })?;
29			TerminalsGuard.keys().cloned().collect()
30		};
31
32		let mut DisposalErrors:Vec<String> = Vec::new();
33
34		for Identifier in TerminalIdentifiers {
35			match TerminalProvider.DisposeTerminal(Identifier).await {
36				Ok(()) => {
37					dev_log!(
38						"lifecycle",
39						"[ApplicationRunTime] Terminal {} disposed successfully",
40						Identifier
41					)
42				},
43				Err(Error) => {
44					DisposalErrors.push(format!("Terminal {}: {}", Identifier, Error));
45					dev_log!(
46						"lifecycle",
47						"warn: [ApplicationRunTime] Failed to dispose terminal {}: {}",
48						Identifier,
49						Error
50					);
51				},
52			}
53		}
54
55		if !DisposalErrors.is_empty() {
56			Err(CommonError::Unknown {
57				Description:format!(
58					"Terminal disposal completed with {} errors: {:?}",
59					DisposalErrors.len(),
60					DisposalErrors
61				),
62			})
63		} else {
64			Ok(())
65		}
66	}
67}