Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to blocking in thread for child process wait #13

Merged
merged 1 commit into from
Nov 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/imageproxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::process::Stdio;
use std::sync::{Arc, Mutex};
use tokio::io::{AsyncBufRead, AsyncReadExt};
use tokio::sync::Mutex as AsyncMutex;
use tokio::task::JoinError;
use tracing::instrument;

pub const OCI_TYPE_LAYER_GZIP: &str = "application/vnd.oci.image.layer.v1.tar+gzip";
Expand Down Expand Up @@ -67,7 +68,9 @@ struct Reply {
value: serde_json::Value,
}

type ChildFuture = Pin<Box<dyn Future<Output = std::io::Result<std::process::Output>>>>;
type ChildFuture = Pin<
Box<dyn Future<Output = std::result::Result<std::io::Result<std::process::Output>, JoinError>>>,
>;

/// Manage a child process proxy to fetch container images.
pub struct ImageProxy {
Expand Down Expand Up @@ -142,17 +145,18 @@ impl ImageProxy {
}
c.stdout(Stdio::null()).stderr(Stdio::piped());
c.stdin(Stdio::from(theirsock));
let mut c = tokio::process::Command::from(c);
c.kill_on_drop(true);
let child = c.spawn().context("Failed to spawn skopeo")?;
tracing::debug!("Spawned skopeo pid={:?}", child.id());
let childwait = Box::pin(child.wait_with_output());

// Here we use std sync API via thread because tokio installs
// a SIGCHLD handler which can conflict with e.g. the glib one
// which may also be in process.
// xref https://github.com/tokio-rs/tokio/issues/3520#issuecomment-968985861
let childwait = tokio::task::spawn_blocking(move || child.wait_with_output());
let sockfd = Arc::new(Mutex::new(mysock));

let r = Self {
sockfd,
childwait: Arc::new(AsyncMutex::new(childwait)),
childwait: Arc::new(AsyncMutex::new(Box::pin(childwait))),
};

// Verify semantic version
Expand Down Expand Up @@ -241,7 +245,7 @@ impl ImageProxy {
Ok(r?)
}
r = childwait.as_mut() => {
let r = r?;
let r = r??;
let stderr = String::from_utf8_lossy(&r.stderr);
return Err(anyhow::anyhow!("proxy unexpectedly exited during request method {}: {}\n{}", method, r.status, stderr))
}
Expand Down Expand Up @@ -325,7 +329,7 @@ impl ImageProxy {
drop(sendbuf);
tracing::debug!("sent shutdown request");
let mut childwait = self.childwait.lock().await;
let output = childwait.as_mut().await?;
let output = childwait.as_mut().await??;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("proxy failed: {}\n{}", output.status, stderr)
Expand Down