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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
use std::{process::ExitStatus, time::Duration};
use tokio::{process::Child as TokioChild, task::JoinSet};
use crate::{BoxedFuture, Context as _, co};
/// A child process spawned with [`Command`](crate::Command)
pub struct Child {
pub(crate) name: String,
pub(crate) inner: TokioChild,
pub(crate) io: ChildIo,
}
impl Child {
/// Block the thread and wait for the child to finish, and check if the ExitStatus is 0
///
/// Note that currently any error that occurred in input/output tasks are treated
/// as warning and not hard error.
///
/// # Blocking
/// This will block the current thread while trying to join the child.
/// Use [`co_wait_nz`](Self::co_wait_nz) to avoid blocking if in async context.
pub fn wait_nz(self) -> crate::Result<()> {
let status = wait_internal(&self.name, self.inner, self.io)?;
if !status.success() {
crate::bail!("{} exited with non-zero status", self.name);
}
Ok(())
}
/// Block the thread and wait for the child to finish,
/// and return its [`ExitStatus`]
///
/// Note that currently any error that occurred in input/output tasks are treated
/// as warning and not hard error.
///
/// # Blocking
/// This will block the current thread while trying to join the child.
/// Use [`co_wait`](Self::co_wait) to avoid blocking if in async context.
#[inline(always)]
pub fn wait(self) -> crate::Result<ExitStatus> {
wait_internal(&self.name, self.inner, self.io)
}
/// Wait for the child asynchronously using the current tokio runtime,
/// and check if the ExitStatus is 0.
///
/// Note that currently any error that occurred in input/output tasks are treated
/// as warning and not hard error.
///
/// # Panic
/// Will panic if called outside of a tokio runtime context
pub async fn co_wait_nz(self) -> crate::Result<()> {
let status = co_wait_internal(&self.name, self.inner, self.io).await?;
if !status.success() {
crate::bail!("{} exited with non-zero status", self.name);
}
Ok(())
}
/// Wait for the child asynchronously using the current tokio runtime,
/// and return its [`ExitStatus`]
///
/// Note that currently any error that occurred in input/output tasks are treated
/// as warning and not hard error.
///
/// # Panic
/// Will panic if called outside of a tokio runtime context
#[inline(always)]
pub async fn co_wait(self) -> crate::Result<ExitStatus> {
co_wait_internal(&self.name, self.inner, self.io).await
}
/// Create a wait guard that will automatically wait for the child
/// (and ignore the error) when going out of scope.
#[inline(always)]
pub fn wait_guard(self) -> ChildWaitGuard {
ChildWaitGuard { inner: Some(self) }
}
/// Wait for the child to exit for at least the timeout.
/// Return the exit status if exited
pub async fn co_wait_timeout(
&mut self,
timeout: Duration,
) -> crate::Result<Option<ExitStatus>> {
let ms = 100;
let mut total_ms = 0;
loop {
match self.inner.try_wait() {
Ok(Some(s)) => return Ok(Some(s)),
Ok(None) => {}
Err(e) => {
crate::rethrow!(e, "io error while waiting {}", self.name)
}
}
tokio::time::sleep(Duration::from_millis(ms)).await;
total_ms += ms;
if Duration::from_millis(total_ms) >= timeout {
break;
}
}
Ok(None)
}
pub fn wait_timeout(&mut self, timeout: Duration) -> crate::Result<Option<ExitStatus>> {
let ms = 100;
let mut total_ms = 0;
loop {
match self.inner.try_wait() {
Ok(Some(s)) => return Ok(Some(s)),
Ok(None) => {}
Err(e) => {
crate::rethrow!(e, "io error while waiting {}", self.name)
}
}
std::thread::sleep(Duration::from_millis(ms));
total_ms += ms;
if Duration::from_millis(total_ms) >= timeout {
break;
}
}
Ok(None)
}
/// Kill the child and wait for it to exit asynchronously. Return the exit status
///
/// # Panic
/// Will panic if called outside of a tokio runtime context
pub async fn co_kill(mut self) -> crate::Result<ExitStatus> {
let mut ms = 100;
for i in 0..5 {
crate::trace!("trying to kill child '{}', attempt {}", self.name, i + 1);
crate::check!(
self.inner.start_kill(),
"failed to send kill signal to child"
)?;
match self.inner.try_wait() {
Ok(Some(s)) => return Ok(s),
Ok(None) => {}
Err(e) => {
crate::rethrow!(e, "io error while killing {}", self.name)
}
}
tokio::time::sleep(Duration::from_millis(ms)).await;
ms *= 4;
}
self.io.co_join(&self.name).await;
crate::bail!("failed to kill child '{}' after many attempts", self.name);
}
/// Kill the child and block the current thread until the child exits. Return the exit status.
///
/// # Blocking
/// This will block the current thread while trying to join the child.
/// Use [`co_kill`](Self::co_kill) to avoid blocking if in async context.
pub fn kill(mut self) -> crate::Result<ExitStatus> {
let mut ms = 100;
for i in 0..5 {
crate::trace!("trying to kill child '{}', attempt {}", self.name, i + 1);
crate::check!(
self.inner.start_kill(),
"failed to send kill signal to child"
)?;
match self.inner.try_wait() {
Ok(Some(s)) => return Ok(s),
Ok(None) => {}
Err(e) => {
crate::rethrow!(e, "io error while killing {}", self.name)
}
}
std::thread::sleep(Duration::from_millis(ms));
ms *= 4;
}
self.io.join(&self.name);
crate::bail!("failed to kill child '{}' after many attempts", self.name);
}
}
fn wait_internal(name: &str, mut child: TokioChild, io: ChildIo) -> crate::Result<ExitStatus> {
// consume the child by waiting
let wait_task = co::spawn(async move { child.wait().await });
// ensure the IO tasks are finished first, since blocking
// on child could dead lock if the child is waiting for IO
io.join(name);
crate::check!(wait_task.join()?, "io error while executing {name}")
}
async fn co_wait_internal(
name: &str,
mut child: TokioChild,
io: ChildIo,
) -> crate::Result<ExitStatus> {
// consume the child by waiting
let wait_task = co::spawn(async move { child.wait().await });
// ensure the IO tasks are finished first, since blocking
// on child could dead lock if the child is waiting for IO
io.co_join(name).await;
crate::check!(
wait_task.co_join().await?,
"io error while executing {name}"
)
}
/// IO Task for a child
pub(crate) struct ChildIo {
// (panicked, stdin_err)
inner: co::Handle<(bool, crate::Result<()>)>,
}
impl ChildIo {
pub fn start(
stdin: Option<BoxedFuture<crate::Result<()>>>,
stdout: Option<BoxedFuture<()>>,
stderr: Option<BoxedFuture<()>>,
) -> Self {
let combined_future = async move {
let mut j = JoinSet::new();
if let Some(x) = stdin {
j.spawn(x);
}
if let Some(x) = stdout {
j.spawn(async move {
x.await;
Ok(())
});
}
if let Some(x) = stderr {
j.spawn(async move {
x.await;
Ok(())
});
}
let mut panicked = false;
let mut stdin_err = Ok(());
while let Some(x) = j.join_next().await {
match x {
// could not join because panicked
Err(_) => panicked = true,
Ok(Err(e)) => {
// this must be stdin because
// the out tasks return ()
stdin_err = Err(e);
}
Ok(Ok(())) => {}
}
}
(panicked, stdin_err)
};
let io_task = co::spawn(combined_future);
Self { inner: io_task }
}
pub fn join(self, name: &str) {
Self::do_join(name, self.inner.join_maybe_aborted())
}
pub async fn co_join(self, name: &str) {
Self::do_join(name, self.inner.co_join_maybe_aborted().await)
}
fn do_join(name: &str, result: crate::Result<Option<(bool, crate::Result<()>)>>) {
match result {
Ok(Some((panicked, stdin_err))) => {
if let Err(e) = stdin_err {
crate::warn!("failed to write to stdin while executing {name}: {e:?}")
}
if panicked {
crate::warn!("some io tasks panicked while executing {name}");
}
}
Ok(None) => {
// aborted
}
Err(_) => {
crate::warn!("some io tasks panicked while executing {name}");
}
}
}
}
/// A guard that automatically calls `wait` on a child
/// when dropped. The result of the `wait` is ignored.
///
/// This can be constructed with [`Child::wait_guard`]
pub struct ChildWaitGuard {
inner: Option<Child>,
}
impl Drop for ChildWaitGuard {
fn drop(&mut self) {
let Some(child) = self.inner.take() else {
return;
};
match child.wait() {
Err(e) => crate::trace!("wait guard: error while waiting for child: {e}"),
Ok(x) => crate::trace!("wait guard: child exited with status: {x}"),
};
}
}
impl ChildWaitGuard {
/// Call [`wait_nz`](Child::wait_nz) on the inner child
pub fn wait_nz(mut self) -> crate::Result<()> {
self.inner.take().unwrap().wait_nz()
}
/// Call [`wait`](Child::wait) on the inner child
pub fn wait(mut self) -> crate::Result<ExitStatus> {
self.inner.take().unwrap().wait()
}
}