use crate::{ExecutionResult, ProcessGroupPolicy, error};
impl<SE: crate::extensions::ShellExtensions> crate::Shell<SE> {
pub async fn on_exit(&mut self) -> Result<(), error::Error> {
self.invoke_exit_trap_handler_if_registered().await?;
Ok(())
}
async fn invoke_exit_trap_handler_if_registered(
&mut self,
) -> Result<ExecutionResult, error::Error> {
let Some(handler) = self
.traps
.get_handler(crate::traps::TrapSignal::Exit)
.cloned()
else {
return Ok(ExecutionResult::success());
};
let mut params = self.default_exec_params();
params.process_group_policy = ProcessGroupPolicy::SameProcessGroup;
let orig_last_exit_status = self.last_exit_status;
self.enter_trap_handler(Some(&handler));
let result = self
.run_string(&handler.command, &handler.source_info, ¶ms)
.await;
self.leave_trap_handler();
self.last_exit_status = orig_last_exit_status;
result
}
}