use std::process::ExitStatus;
use super::slot::ChildSlot;
use super::teardown::{kill_refused, Disposition, LEAVING_RUNNING, LEFT};
use super::OwnedChild;
#[cfg(feature = "tokio")]
use crate::child::lifecycle::unbounded_status;
use crate::child::lifecycle::{wait_for_status, wait_to_exit, Orphaned, Settlement};
use crate::error::Error;
use crate::identity::ProcessId;
use crate::os_process::child_named;
use crate::signal::{Sent, Sig};
#[derive(Debug)]
pub struct Unreaped {
child: ChildSlot,
id: Option<ProcessId>,
front: bool,
drop_kind: DropKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DropKind {
Wait,
LetGo,
}
impl Unreaped {
pub(crate) fn new(child: OwnedChild, id: Option<ProcessId>, front: bool) -> Unreaped {
Unreaped {
child: ChildSlot::new(child),
id,
front,
drop_kind: DropKind::Wait,
}
}
pub fn pid(&self) -> Option<u32> {
self.child.pid()
}
pub fn id(&self) -> Option<ProcessId> {
self.id
}
pub fn wait(mut self) -> Result<ExitStatus, Error> {
let child = self.child.take().expect("a live Unreaped holds its child");
wait_for_status(&child, Orphaned::Warn)
}
#[cfg(feature = "tokio")]
pub fn wait_async(mut self) -> impl std::future::Future<Output = Result<ExitStatus, Error>> {
self.drop_kind = DropKind::LetGo;
async move {
let outcome = self.child.wait_async().await;
let child = self.child.take().expect("a live Unreaped holds its child");
unbounded_status(child.pid(), outcome?, Orphaned::Warn)
}
}
pub fn leak(mut self) {
drop(self.child.take());
}
}
fn dropped(who: &str) -> String {
format!("{who} was dropped without wait or leak")
}
fn reaped_it(status: ExitStatus) -> String {
format!("reaped it ({status})")
}
pub(crate) fn let_go_line(
pid: Option<u32>,
kill: Option<&Result<Sent, Error>>,
look: &Settlement,
) -> (log::Level, String) {
let dropped = dropped(&child_named(pid));
let subject = match kill {
None => dropped,
Some(Ok(Sent::Delivered)) => format!("{dropped} (killed it)"),
Some(Ok(Sent::Gone)) => format!("{dropped} (its kill found nothing to signal)"),
Some(Ok(Sent::Unverified)) => {
format!("{dropped} (its kill was not sent: the child could not be verified)")
}
Some(Err(refused)) => format!("{dropped} and {}", kill_refused(refused)),
};
let (level, line) = match look.status() {
Some(status) => (log::Level::Warn, format!("{subject}: {}", reaped_it(status))),
None => {
let running = match kill {
Some(Ok(Sent::Delivered)) => format!("{subject}; it had not exited yet{LEFT}"),
Some(Ok(Sent::Gone)) => format!("{subject}; the look saw no exit yet{LEFT}"),
_ => format!("{subject}{LEAVING_RUNNING}"),
};
let said = look.wording(&subject, Some(&running), Some(Disposition::Leave));
(said.level(), said.text().to_owned())
}
};
let level = if matches!(kill, Some(Err(_))) {
level.min(log::Level::Warn)
} else {
level
};
(level, line)
}
impl Unreaped {
fn let_go(&self, child: &OwnedChild) {
let kill = (!self.front).then(|| child.signal(Sig::Kill));
let look = Settlement::new(child.wait(Some(crate::wait::now())), false);
let (level, line) = let_go_line(child.pid(), kill.as_ref(), &look);
log::log!(level, "{line}");
}
}
impl Drop for Unreaped {
fn drop(&mut self) {
let Some(child) = self.child.take() else {
return;
};
if self.drop_kind == DropKind::LetGo {
self.let_go(&child);
return;
}
let who = child_named(child.pid());
if std::thread::panicking() {
log::warn!("{} while the thread is panicking; gave it up, unreaped", dropped(&who));
return;
}
let settlement = Settlement::new(wait_to_exit(&child, None), false);
if let Some(status) = settlement.status() {
log::warn!("{}; waited for it and {}", dropped(&who), reaped_it(status));
return;
}
let said = settlement.wording(&dropped(&who), None, Some(Disposition::Leave));
log::log!(said.level(), "{}", said.text());
}
}