use crate::containment::dispatch::RootView;
use crate::containment::DropView;
use crate::error::Error;
use crate::signal::RootState;
#[cfg_attr(
not(feature = "tokio"),
allow(dead_code, reason = "only the async child forgets tokio's `Child`")
)]
#[derive(Debug)]
pub(crate) struct Forgot {
pub(crate) leak: &'static str,
pub(crate) now: Option<RootState>,
pub(crate) cause: Option<String>,
}
impl Forgot {
#[cfg_attr(
not(feature = "tokio"),
allow(dead_code, reason = "only the async child forgets tokio's `Child`")
)]
pub(crate) fn reaped_elsewhere(&self) -> bool {
self.now
.as_ref()
.is_none_or(|state| !matches!(state, RootState::Unknown(_)))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum PartKey {
Described,
Tree,
Left(String),
}
pub(crate) struct DropReport<'a> {
pub(crate) label: &'a str,
pub(crate) view: &'a DropView,
pub(crate) front: Option<&'a Error>,
pub(crate) skipped: Option<String>,
pub(crate) forgot: Option<Forgot>,
pub(crate) tree: Option<String>,
pub(crate) left: Vec<String>,
}
impl<'a> DropReport<'a> {
pub(crate) fn new(label: &'a str, view: &'a DropView) -> DropReport<'a> {
DropReport {
label,
view,
front: None,
skipped: None,
forgot: None,
tree: None,
left: Vec::new(),
}
}
pub(crate) fn emit(self, reported: &[PartKey]) -> Vec<PartKey> {
let parts = self.parts();
if parts.is_empty() {
return Vec::new();
}
let text = format!(
"{}: {}",
self.label,
parts
.iter()
.map(|(_, text)| text.as_str())
.collect::<Vec<_>>()
.join("; ")
);
if parts.iter().all(|(key, _)| reported.contains(key)) {
log::debug!("{text}");
return Vec::new();
}
log::warn!("{text}");
parts.into_iter().map(|(key, _)| key).collect()
}
fn parts(&self) -> Vec<(PartKey, String)> {
let pid = self.view.root_pid;
let reaped_now = matches!(
self.forgot.as_ref().and_then(|f| f.now.as_ref()),
Some(RootState::Reaped)
);
let reaped = reaped_now || matches!(self.view.root, RootView::Reaped);
let mut parts: Vec<(PartKey, String)> = Vec::new();
if let Some(mut state) = self.state_text(pid, reaped_now) {
if self.view.unsettled() && !reaped_now {
if let Some(action) = &self.skipped {
state.push_str(&format!(
", so it does not {action}, whose number may belong to an unrelated process"
));
}
}
if matches!(self.view.root, RootView::Unpinned) && !reaped_now {
state.push_str("; the root itself is neither signalled nor waited on");
}
parts.push((PartKey::Described, state));
}
if let (RootView::Reaped, Some(action)) = (&self.view.root, &self.skipped) {
parts.push((
PartKey::Described,
format!(
"the root is already reaped, so this drop does not {action}, whose number may now belong to an \
unrelated process. Descendants that outlived the reaped root are not torn down by this drop; \
call kill_tree() before wait() to end them (https://github.com/bindreams/cosca/issues/382)"
),
));
}
if let Some(why) = self.front.filter(|_| !reaped) {
parts.push((
PartKey::Described,
format!(
"elevation front pid {pid}: {why}; the front is killed through its cgroup if it is still in it; \
cosca does not wait for it, and tokio reaps it once it exits"
),
));
}
parts.extend(self.tree.iter().map(|tree| (PartKey::Tree, tree.clone())));
parts.extend(self.left.iter().map(|left| (PartKey::Left(left.clone()), left.clone())));
if let Some(forgot) = &self.forgot {
if let Some(cause) = &forgot.cause {
parts.push((PartKey::Described, format!("the wait found it foreign: {cause}")));
}
parts.push((
PartKey::Described,
format!("forgetting tokio's handle for it leaks {}", forgot.leak),
));
}
parts
}
fn state_text(&self, pid: u32, reaped_now: bool) -> Option<String> {
let first_doubt = match &self.view.root {
RootView::Unknown(e) => Some(e.to_string()),
RootView::Unpinned => Some(crate::signal::UNPINNED_WHY.to_owned()),
RootView::Reaped | RootView::Trusted => None,
};
if let (true, Some(doubt)) = (reaped_now, &first_doubt) {
return Some(format!(
"RootState::Reaped: the root ({pid}) was reaped by someone else (its own handle first could not say: {doubt})"
));
}
match (&self.view.root, &self.forgot) {
(RootView::Unpinned, _) => Some(format!(
"RootState::Unpinned: the root ({pid}) is not pinned by this process ({})",
crate::signal::UNPINNED_WHY
)),
(RootView::Unknown(e), _) => Some(format!(
"RootState::Unknown: the root's ({pid}) own handle could not say whether it was reaped ({e})"
)),
(RootView::Reaped | RootView::Trusted, Some(forgot)) => Some(match &forgot.now {
Some(RootState::Unknown(e)) => {
format!("child {pid} cannot be shown to be ours (RootState::Unknown: {e})")
}
Some(RootState::Unpinned) => format!(
"child {pid} is not pinned by this process (RootState::Unpinned: {})",
crate::signal::UNPINNED_WHY
),
Some(RootState::Reaped | RootState::Unreaped) | None => {
format!("child {pid} was reaped by someone else, or cannot be shown to be ours")
}
}),
(RootView::Reaped | RootView::Trusted, None) => None,
}
}
}