2021-10-14 00:06:53 +00:00
|
|
|
use actix_web::web::Bytes;
|
2023-09-04 02:30:47 +00:00
|
|
|
use flume::r#async::RecvFut;
|
2021-08-31 02:19:47 +00:00
|
|
|
use std::{
|
2023-11-10 00:20:59 +00:00
|
|
|
ffi::OsStr,
|
2021-09-04 00:53:53 +00:00
|
|
|
future::Future,
|
2021-08-31 02:19:47 +00:00
|
|
|
pin::Pin,
|
2023-07-10 20:29:41 +00:00
|
|
|
process::{ExitStatus, Stdio},
|
2023-12-18 05:14:39 +00:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicU8, Ordering},
|
|
|
|
Arc, Mutex,
|
|
|
|
},
|
|
|
|
task::{Context, Poll, Wake, Waker},
|
2023-08-05 17:41:06 +00:00
|
|
|
time::{Duration, Instant},
|
2021-08-31 02:19:47 +00:00
|
|
|
};
|
2021-09-14 01:22:42 +00:00
|
|
|
use tokio::{
|
|
|
|
io::{AsyncRead, AsyncWriteExt, ReadBuf},
|
2023-08-05 17:41:06 +00:00
|
|
|
process::{Child, ChildStdin, ChildStdout, Command},
|
2023-10-21 00:08:11 +00:00
|
|
|
task::JoinHandle,
|
2021-09-14 01:22:42 +00:00
|
|
|
};
|
2022-04-07 18:28:28 +00:00
|
|
|
use tracing::{Instrument, Span};
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2023-09-05 02:58:57 +00:00
|
|
|
use crate::{error_code::ErrorCode, future::WithTimeout};
|
2023-09-02 01:50:10 +00:00
|
|
|
|
2023-07-22 21:47:59 +00:00
|
|
|
struct MetricsGuard {
|
|
|
|
start: Instant,
|
|
|
|
armed: bool,
|
|
|
|
command: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MetricsGuard {
|
|
|
|
fn guard(command: String) -> Self {
|
2023-07-23 02:11:28 +00:00
|
|
|
metrics::increment_counter!("pict-rs.process.start", "command" => command.clone());
|
2023-07-22 21:47:59 +00:00
|
|
|
|
|
|
|
Self {
|
|
|
|
start: Instant::now(),
|
|
|
|
armed: true,
|
|
|
|
command,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disarm(mut self) {
|
|
|
|
self.armed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for MetricsGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
metrics::histogram!(
|
|
|
|
"pict-rs.process.duration",
|
|
|
|
self.start.elapsed().as_secs_f64(),
|
|
|
|
"command" => self.command.clone(),
|
|
|
|
"completed" => (!self.armed).to_string(),
|
|
|
|
);
|
|
|
|
|
2023-07-23 02:11:28 +00:00
|
|
|
metrics::increment_counter!("pict-rs.process.end", "completed" => (!self.armed).to_string() , "command" => self.command.clone());
|
2023-07-22 21:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:16:12 +00:00
|
|
|
#[derive(Debug)]
|
2023-07-14 00:21:28 +00:00
|
|
|
struct StatusError(ExitStatus);
|
2021-09-09 19:16:12 +00:00
|
|
|
|
2021-08-31 02:19:47 +00:00
|
|
|
pub(crate) struct Process {
|
2023-07-14 00:21:57 +00:00
|
|
|
command: String,
|
2021-09-14 01:22:42 +00:00
|
|
|
child: Child,
|
2023-07-22 21:47:59 +00:00
|
|
|
guard: MetricsGuard,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout: Duration,
|
2022-04-07 02:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl std::fmt::Debug for Process {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("Process").field("child", &"Child").finish()
|
|
|
|
}
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 23:58:32 +00:00
|
|
|
struct DropHandle {
|
|
|
|
inner: JoinHandle<()>,
|
|
|
|
}
|
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
struct ProcessReadState {
|
|
|
|
flags: AtomicU8,
|
|
|
|
parent: Mutex<Option<Waker>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ProcessReadWaker {
|
|
|
|
state: Arc<ProcessReadState>,
|
|
|
|
flag: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ProcessRead {
|
|
|
|
inner: ChildStdout,
|
2023-09-04 02:30:47 +00:00
|
|
|
err_recv: RecvFut<'static, std::io::Error>,
|
2023-08-05 17:41:06 +00:00
|
|
|
handle: DropHandle,
|
2023-12-18 05:14:39 +00:00
|
|
|
closed: bool,
|
|
|
|
state: Arc<ProcessReadState>,
|
|
|
|
span: Span,
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
|
|
|
|
2023-07-10 20:29:41 +00:00
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
|
|
pub(crate) enum ProcessError {
|
2023-07-17 02:51:14 +00:00
|
|
|
#[error("Required command {0} not found, make sure it exists in pict-rs' $PATH")]
|
2023-07-10 20:29:41 +00:00
|
|
|
NotFound(String),
|
|
|
|
|
2023-07-17 02:51:14 +00:00
|
|
|
#[error("Cannot run command {0} due to invalid permissions on binary, make sure the pict-rs user has permission to run it")]
|
|
|
|
PermissionDenied(String),
|
|
|
|
|
2023-07-10 20:29:41 +00:00
|
|
|
#[error("Reached process spawn limit")]
|
|
|
|
LimitReached,
|
|
|
|
|
2023-08-05 17:41:06 +00:00
|
|
|
#[error("{0} timed out")]
|
|
|
|
Timeout(String),
|
|
|
|
|
2023-07-17 18:30:08 +00:00
|
|
|
#[error("{0} Failed with {1}")]
|
|
|
|
Status(String, ExitStatus),
|
2023-07-10 20:29:41 +00:00
|
|
|
|
|
|
|
#[error("Unknown process error")]
|
|
|
|
Other(#[source] std::io::Error),
|
|
|
|
}
|
|
|
|
|
2023-09-02 01:50:10 +00:00
|
|
|
impl ProcessError {
|
|
|
|
pub(crate) const fn error_code(&self) -> ErrorCode {
|
|
|
|
match self {
|
|
|
|
Self::NotFound(_) => ErrorCode::COMMAND_NOT_FOUND,
|
|
|
|
Self::PermissionDenied(_) => ErrorCode::COMMAND_PERMISSION_DENIED,
|
|
|
|
Self::LimitReached | Self::Other(_) => ErrorCode::COMMAND_ERROR,
|
|
|
|
Self::Timeout(_) => ErrorCode::COMMAND_TIMEOUT,
|
|
|
|
Self::Status(_, _) => ErrorCode::COMMAND_FAILURE,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 02:19:47 +00:00
|
|
|
impl Process {
|
2023-11-10 00:20:59 +00:00
|
|
|
pub(crate) fn run<T>(
|
|
|
|
command: &str,
|
|
|
|
args: &[T],
|
|
|
|
envs: &[(&str, &OsStr)],
|
|
|
|
timeout: u64,
|
|
|
|
) -> Result<Self, ProcessError>
|
|
|
|
where
|
|
|
|
T: AsRef<OsStr>,
|
|
|
|
{
|
|
|
|
let res = tracing::trace_span!(parent: None, "Create command", %command).in_scope(|| {
|
|
|
|
Self::spawn(
|
|
|
|
command,
|
2023-11-10 00:26:57 +00:00
|
|
|
Command::new(command).args(args).envs(envs.iter().copied()),
|
2023-11-10 00:20:59 +00:00
|
|
|
timeout,
|
|
|
|
)
|
|
|
|
});
|
2023-07-10 20:29:41 +00:00
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(this) => Ok(this),
|
|
|
|
Err(e) => match e.kind() {
|
|
|
|
std::io::ErrorKind::NotFound => Err(ProcessError::NotFound(command.to_string())),
|
2023-07-17 02:51:14 +00:00
|
|
|
std::io::ErrorKind::PermissionDenied => {
|
|
|
|
Err(ProcessError::PermissionDenied(command.to_string()))
|
|
|
|
}
|
2023-07-10 20:29:41 +00:00
|
|
|
std::io::ErrorKind::WouldBlock => Err(ProcessError::LimitReached),
|
|
|
|
_ => Err(ProcessError::Other(e)),
|
|
|
|
},
|
|
|
|
}
|
2021-09-14 01:22:42 +00:00
|
|
|
}
|
|
|
|
|
2023-08-05 17:41:06 +00:00
|
|
|
fn spawn(command: &str, cmd: &mut Command, timeout: u64) -> std::io::Result<Self> {
|
2023-07-14 00:21:57 +00:00
|
|
|
tracing::trace_span!(parent: None, "Spawn command", %command).in_scope(|| {
|
2023-07-22 21:47:59 +00:00
|
|
|
let guard = MetricsGuard::guard(command.into());
|
|
|
|
|
2023-07-10 20:29:41 +00:00
|
|
|
let cmd = cmd
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.kill_on_drop(true);
|
2021-09-25 20:23:05 +00:00
|
|
|
|
2023-07-14 00:21:57 +00:00
|
|
|
cmd.spawn().map(|child| Process {
|
|
|
|
child,
|
|
|
|
command: String::from(command),
|
2023-07-22 21:47:59 +00:00
|
|
|
guard,
|
2023-08-05 17:41:06 +00:00
|
|
|
timeout: Duration::from_secs(timeout),
|
2023-07-14 00:21:57 +00:00
|
|
|
})
|
2022-04-07 17:56:40 +00:00
|
|
|
})
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(skip(self))]
|
2023-08-05 17:41:06 +00:00
|
|
|
pub(crate) async fn wait(self) -> Result<(), ProcessError> {
|
|
|
|
let Process {
|
|
|
|
command,
|
|
|
|
mut child,
|
|
|
|
guard,
|
|
|
|
timeout,
|
|
|
|
} = self;
|
|
|
|
|
2023-09-05 02:58:57 +00:00
|
|
|
let res = child.wait().with_timeout(timeout).await;
|
2023-07-10 20:29:41 +00:00
|
|
|
|
|
|
|
match res {
|
2023-08-05 17:41:06 +00:00
|
|
|
Ok(Ok(status)) if status.success() => {
|
|
|
|
guard.disarm();
|
2023-07-22 21:47:59 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-08-05 17:41:06 +00:00
|
|
|
Ok(Ok(status)) => Err(ProcessError::Status(command, status)),
|
|
|
|
Ok(Err(e)) => Err(ProcessError::Other(e)),
|
|
|
|
Err(_) => {
|
|
|
|
child.kill().await.map_err(ProcessError::Other)?;
|
|
|
|
|
|
|
|
Err(ProcessError::Timeout(command))
|
|
|
|
}
|
2021-10-23 19:14:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
pub(crate) fn bytes_read(self, input: Bytes) -> ProcessRead {
|
2022-10-02 02:17:18 +00:00
|
|
|
self.spawn_fn(move |mut stdin| {
|
2022-09-25 22:35:52 +00:00
|
|
|
let mut input = input;
|
|
|
|
async move { stdin.write_all_buf(&mut input).await }
|
|
|
|
})
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
pub(crate) fn read(self) -> ProcessRead {
|
2022-10-02 02:17:18 +00:00
|
|
|
self.spawn_fn(|_| async { Ok(()) })
|
2022-09-25 20:17:33 +00:00
|
|
|
}
|
|
|
|
|
2023-06-01 22:33:43 +00:00
|
|
|
#[allow(unknown_lints)]
|
|
|
|
#[allow(clippy::let_with_type_underscore)]
|
2022-10-02 03:47:52 +00:00
|
|
|
#[tracing::instrument(level = "trace", skip_all)]
|
2023-12-18 05:14:39 +00:00
|
|
|
fn spawn_fn<F, Fut>(self, f: F) -> ProcessRead
|
2022-09-25 22:35:52 +00:00
|
|
|
where
|
|
|
|
F: FnOnce(ChildStdin) -> Fut + 'static,
|
|
|
|
Fut: Future<Output = std::io::Result<()>>,
|
|
|
|
{
|
2023-08-05 17:41:06 +00:00
|
|
|
let Process {
|
|
|
|
command,
|
|
|
|
mut child,
|
|
|
|
guard,
|
|
|
|
timeout,
|
|
|
|
} = self;
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2023-08-05 17:41:06 +00:00
|
|
|
let stdin = child.stdin.take().expect("stdin exists");
|
|
|
|
let stdout = child.stdout.take().expect("stdout exists");
|
|
|
|
|
2023-09-04 02:30:47 +00:00
|
|
|
let (tx, rx) = crate::sync::channel::<std::io::Error>(1);
|
|
|
|
let rx = rx.into_recv_async();
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
let background_span =
|
|
|
|
tracing::info_span!(parent: None, "Background process task", %command);
|
|
|
|
background_span.follows_from(Span::current());
|
|
|
|
|
|
|
|
let span = tracing::info_span!(parent: None, "Foreground process task", %command);
|
2022-04-07 18:28:28 +00:00
|
|
|
span.follows_from(Span::current());
|
|
|
|
|
2023-09-04 02:30:47 +00:00
|
|
|
let handle = crate::sync::spawn(
|
2023-10-21 00:08:11 +00:00
|
|
|
"await-process",
|
2023-09-04 02:30:47 +00:00
|
|
|
async move {
|
|
|
|
let child_fut = async {
|
|
|
|
(f)(stdin).await?;
|
|
|
|
|
|
|
|
child.wait().await
|
|
|
|
};
|
|
|
|
|
2023-09-05 02:58:57 +00:00
|
|
|
let error = match child_fut.with_timeout(timeout).await {
|
2023-09-04 02:30:47 +00:00
|
|
|
Ok(Ok(status)) if status.success() => {
|
|
|
|
guard.disarm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Ok(Ok(status)) => {
|
|
|
|
std::io::Error::new(std::io::ErrorKind::Other, StatusError(status))
|
|
|
|
}
|
|
|
|
Ok(Err(e)) => e,
|
|
|
|
Err(_) => std::io::ErrorKind::TimedOut.into(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let _ = tx.send(error);
|
|
|
|
let _ = child.kill().await;
|
|
|
|
}
|
2023-12-18 05:14:39 +00:00
|
|
|
.instrument(background_span),
|
2023-09-04 02:30:47 +00:00
|
|
|
);
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2022-04-07 02:40:49 +00:00
|
|
|
ProcessRead {
|
2021-09-04 00:53:53 +00:00
|
|
|
inner: stdout,
|
|
|
|
err_recv: rx,
|
2021-10-20 23:58:32 +00:00
|
|
|
handle: DropHandle { inner: handle },
|
2023-12-18 05:14:39 +00:00
|
|
|
closed: false,
|
|
|
|
state: ProcessReadState::new_woken(),
|
|
|
|
span,
|
2022-04-07 02:40:49 +00:00
|
|
|
}
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
2021-08-31 02:19:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
impl ProcessReadState {
|
|
|
|
fn new_woken() -> Arc<Self> {
|
|
|
|
Arc::new(Self {
|
|
|
|
flags: AtomicU8::new(0xff),
|
|
|
|
parent: Mutex::new(None),
|
|
|
|
})
|
|
|
|
}
|
2023-07-14 00:21:28 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
fn clone_parent(&self) -> Option<Waker> {
|
|
|
|
let guard = self.parent.lock().unwrap();
|
|
|
|
guard.as_ref().map(|w| w.clone())
|
|
|
|
}
|
2023-07-14 00:21:28 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
fn into_parts(self) -> (AtomicU8, Option<Waker>) {
|
|
|
|
let ProcessReadState { flags, parent } = self;
|
2023-08-05 17:41:06 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
let parent = parent.lock().unwrap().take();
|
|
|
|
|
|
|
|
(flags, parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcessRead {
|
|
|
|
fn get_waker(&self, flag: u8) -> Option<Waker> {
|
|
|
|
let mask = 0xff ^ flag;
|
|
|
|
let previous = self.state.flags.fetch_and(mask, Ordering::AcqRel);
|
|
|
|
let active = previous & flag;
|
|
|
|
|
|
|
|
if active == flag {
|
|
|
|
Some(
|
|
|
|
Arc::new(ProcessReadWaker {
|
|
|
|
state: self.state.clone(),
|
|
|
|
flag,
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-08-05 17:41:06 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
fn set_parent_waker(&self, parent: &Waker) {
|
|
|
|
let mut guard = self.state.parent.lock().unwrap();
|
|
|
|
if let Some(waker) = guard.as_mut() {
|
|
|
|
if !waker.will_wake(parent) {
|
|
|
|
*waker = parent.clone();
|
2023-08-05 17:41:06 +00:00
|
|
|
}
|
2023-12-18 05:14:39 +00:00
|
|
|
} else {
|
|
|
|
*guard = Some(parent.clone());
|
2022-04-07 02:40:49 +00:00
|
|
|
}
|
2023-12-18 05:14:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const RECV_WAKER: u8 = 0b_0010;
|
|
|
|
const HANDLE_WAKER: u8 = 0b_0100;
|
|
|
|
|
|
|
|
impl AsyncRead for ProcessRead {
|
|
|
|
fn poll_read(
|
|
|
|
mut self: Pin<&mut Self>,
|
|
|
|
cx: &mut Context<'_>,
|
|
|
|
buf: &mut ReadBuf<'_>,
|
|
|
|
) -> Poll<std::io::Result<()>> {
|
|
|
|
self.set_parent_waker(cx.waker());
|
2021-09-04 00:53:53 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
let span = self.span.clone();
|
|
|
|
let guard = span.enter();
|
|
|
|
|
|
|
|
let value = loop {
|
|
|
|
// always poll for bytes when poll_read is called
|
2023-07-14 00:21:28 +00:00
|
|
|
let before_size = buf.filled().len();
|
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
if let Poll::Ready(res) = Pin::new(&mut self.inner).poll_read(cx, buf) {
|
|
|
|
if let Err(e) = res {
|
|
|
|
self.closed = true;
|
2023-07-14 00:21:28 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
break Poll::Ready(Err(e));
|
|
|
|
} else if buf.filled().len() == before_size {
|
|
|
|
self.closed = true;
|
2023-07-14 00:21:28 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
break Poll::Ready(Ok(()));
|
|
|
|
} else {
|
|
|
|
break Poll::Ready(Ok(()));
|
2023-07-14 00:21:28 +00:00
|
|
|
}
|
2023-12-18 05:14:39 +00:00
|
|
|
} else if let Some(waker) = self.get_waker(RECV_WAKER) {
|
|
|
|
// only poll recv if we've been explicitly woken
|
|
|
|
let mut recv_cx = Context::from_waker(&waker);
|
|
|
|
|
|
|
|
if let Poll::Ready(res) = Pin::new(&mut self.err_recv).poll(&mut recv_cx) {
|
|
|
|
if let Ok(err) = res {
|
|
|
|
self.closed = true;
|
|
|
|
break Poll::Ready(Err(err));
|
|
|
|
}
|
2023-07-14 00:21:28 +00:00
|
|
|
}
|
2023-12-18 05:14:39 +00:00
|
|
|
} else if let Some(waker) = self.get_waker(HANDLE_WAKER) {
|
|
|
|
// only poll handle if we've been explicitly woken
|
|
|
|
let mut handle_cx = Context::from_waker(&waker);
|
|
|
|
|
|
|
|
if let Poll::Ready(res) = Pin::new(&mut self.handle.inner).poll(&mut handle_cx) {
|
|
|
|
if let Err(e) = res {
|
|
|
|
self.closed = true;
|
|
|
|
break Poll::Ready(Err(std::io::Error::new(std::io::ErrorKind::Other, e)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if self.closed {
|
|
|
|
break Poll::Ready(Ok(()));
|
|
|
|
} else {
|
|
|
|
break Poll::Pending;
|
|
|
|
}
|
|
|
|
};
|
2023-07-14 00:21:28 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
drop(guard);
|
2023-07-14 00:21:28 +00:00
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
value
|
2021-09-04 00:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-20 23:58:32 +00:00
|
|
|
impl Drop for DropHandle {
|
2021-09-11 21:35:38 +00:00
|
|
|
fn drop(&mut self) {
|
2021-10-20 23:58:32 +00:00
|
|
|
self.inner.abort();
|
2021-09-11 21:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-18 05:14:39 +00:00
|
|
|
impl Wake for ProcessReadWaker {
|
|
|
|
fn wake(self: Arc<Self>) {
|
|
|
|
match Arc::try_unwrap(self) {
|
|
|
|
Ok(ProcessReadWaker { state, flag }) => match Arc::try_unwrap(state) {
|
|
|
|
Ok(state) => {
|
|
|
|
let (flags, parent) = state.into_parts();
|
|
|
|
|
|
|
|
flags.fetch_and(flag, Ordering::AcqRel);
|
|
|
|
|
|
|
|
if let Some(parent) = parent {
|
|
|
|
parent.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(state) => {
|
|
|
|
state.flags.fetch_or(flag, Ordering::AcqRel);
|
|
|
|
|
|
|
|
if let Some(waker) = state.clone_parent() {
|
|
|
|
waker.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(this) => this.wake_by_ref(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wake_by_ref(self: &Arc<Self>) {
|
|
|
|
self.state.flags.fetch_or(self.flag, Ordering::AcqRel);
|
|
|
|
|
|
|
|
if let Some(parent) = self.state.clone_parent() {
|
|
|
|
parent.wake();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-09 19:16:12 +00:00
|
|
|
impl std::fmt::Display for StatusError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
2023-07-14 00:21:28 +00:00
|
|
|
write!(f, "Command failed with bad status: {}", self.0)
|
2021-09-09 19:16:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::error::Error for StatusError {}
|