1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! The one warn of a drop or a failed-spawn cleanup.
//!
//! An event is the drop (or the cleanup) of one handle. Its steps only collect what they found:
//! the root's state, the kill they skipped, the forget of tokio's `Child`, a signal the OS refused.
//! [`DropReport`] composes all of it into a single `warn`, or none when nothing is worth one.
use crate::child::owned::teardown::{Remains, TeardownOutcome};
use crate::containment::dispatch::RootView;
use crate::containment::DropView;
use crate::error::Error;
use crate::signal::RootState;
/// What forgetting tokio's `Child` leaked, and what was seen when the child was asked again.
#[cfg_attr(
not(feature = "tokio"),
allow(dead_code, reason = "only the async child forgets tokio's `Child`")
)]
#[derive(Debug)]
pub(crate) struct Forgot {
/// What the forget leaks (tokio's pidfd and reactor registration, or its `SIGCHLD` watch).
pub(crate) leak: &'static str,
/// What the handle said when it was asked again, if it was.
pub(crate) now: Option<RootState>,
/// Why the child had to be forgotten, when a wait found out (the wait's error, or why its
/// zombie is not ours).
pub(crate) cause: Option<String>,
}
impl Forgot {
#[cfg_attr(
not(feature = "tokio"),
allow(dead_code, reason = "only the async child forgets tokio's `Child`")
)]
/// Whether the child was shown reaped by someone else (or held by launchd), rather than merely
/// not shown to be ours, in which case it may still be running.
pub(crate) fn reaped_elsewhere(&self) -> bool {
self.now
.as_ref()
.is_none_or(|state| !matches!(state, RootState::Unknown(_)))
}
}
/// What kind of thing a part of a report says, to tell a repeat of what an earlier step of the same
/// event reported from something new.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum PartKey {
/// The root's condition and what was therefore skipped, left or forgotten: the same event
/// described again, whatever its wording.
Described,
/// The contained tree's teardown failure.
Tree,
/// The failure to watch a drain after a tree kill that landed: not a repeat of a later kill's
/// failure.
Drain,
/// A failure of the signals, by its text: a different one is new.
Left(String),
}
/// What one drop or cleanup found, for its single `warn`.
pub(crate) struct DropReport<'a> {
/// The look at the root.
pub(crate) view: &'a DropView,
/// The elevation front the event left running instead of signalling, and why.
pub(crate) front: Option<&'a Error>,
/// What the drop did not do because the root's number is untrusted
/// ([`TreeKillOutcome::left_undone`](crate::containment::dispatch::TreeKillOutcome::left_undone)).
pub(crate) skipped: Option<String>,
/// What forgetting tokio's `Child` leaked, if the event forgot it.
pub(crate) forgot: Option<Forgot>,
/// The contained tree's teardown failure, if it failed.
pub(crate) tree: Option<String>,
/// The failure to watch the tree's drain after a kill that landed, if it failed.
pub(crate) drain: Option<String>,
/// What the signals could not do: a refused kill, a failed reap.
pub(crate) left: Vec<String>,
}
impl<'a> DropReport<'a> {
pub(crate) fn new(view: &'a DropView) -> DropReport<'a> {
DropReport {
view,
front: None,
skipped: None,
forgot: None,
tree: None,
drain: None,
left: Vec::new(),
}
}
/// Takes `torn`'s clause into the report, led by the report's caller: a clause at warn becomes
/// a part of the one warn, and a lower one is logged at its level, led by the same caller
/// ([`TeardownOutcome::take_warn`]). What is left of `torn` is returned.
pub(crate) fn fold_clause(&mut self, torn: TeardownOutcome) -> Remains {
let (warn, remains) = torn.take_warn(self.view.caller.label());
self.left.extend(warn);
remains
}
/// Log the report: one `warn`, or nothing when there is nothing to report. `reported`: what an
/// earlier step of this same event warned of (a failed spawn's cleanup, before its handle's
/// drop). A report that only repeats it is a `debug` record; one with anything new warns.
/// Returns the kinds of part it warned of, none when it did not warn.
pub(crate) fn emit(self, reported: &[PartKey]) -> Vec<PartKey> {
let parts = self.parts();
if parts.is_empty() {
return Vec::new();
}
let text = format!(
"{}: {}",
self.view.caller.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) {
let this = self.view.caller.this_event();
parts.push((
PartKey::Described,
format!(
"the root is already reaped, so {this} does not {action}, whose number may now belong to an \
unrelated process. Descendants that outlived the reaped root are not torn down by {this}; \
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!(
"{}: {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",
crate::elevation::front::front_named(Some(pid))
),
));
}
parts.extend(self.tree.iter().map(|tree| (PartKey::Tree, tree.clone())));
parts.extend(self.drain.iter().map(|drain| (PartKey::Drain, drain.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
}
/// The root's state, when it is worth a warn: unsettled, or forgotten. A second look that
/// shows the reap names it, rather than the first look's doubt.
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,
}
}
}