Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/RunTime/Shutdown/
DisposeTerminalsSafely.rs

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