fix(all): race condition in process

This commit is contained in:
Kasper Juul Hermansen 2023-02-20 11:42:03 +01:00
parent b86710d71e
commit a13a2a9ecb
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 68 additions and 66 deletions

View File

@ -3,7 +3,7 @@ use genco::prelude::rust;
use genco::quote; use genco::quote;
use itertools::Itertools; use itertools::Itertools;
use crate::functions::{CommonFunctions}; use crate::functions::CommonFunctions;
use crate::rust::functions::{ use crate::rust::functions::{
field_options_struct_name, format_function, format_name, format_optional_args, field_options_struct_name, format_function, format_name, format_optional_args,
format_struct_comment, format_struct_name, format_struct_comment, format_struct_name,
@ -12,7 +12,7 @@ use crate::utility::OptionExt;
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> { pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
let selection = rust::import("crate::querybuilder", "Selection"); let selection = rust::import("crate::querybuilder", "Selection");
let child = rust::import("std::process", "Child"); let child = rust::import("tokio::process", "Child");
let conn = rust::import("dagger_core::connect_params", "ConnectParams"); let conn = rust::import("dagger_core::connect_params", "ConnectParams");
let arc = rust::import("std::sync", "Arc"); let arc = rust::import("std::sync", "Arc");

View File

@ -6,6 +6,8 @@ use std::{
sync::Arc, sync::Arc,
}; };
use tokio::io::AsyncBufReadExt;
use crate::{config::Config, connect_params::ConnectParams}; use crate::{config::Config, connect_params::ConnectParams};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -24,7 +26,7 @@ impl CliSession {
&self, &self,
config: &Config, config: &Config,
cli_path: &PathBuf, cli_path: &PathBuf,
) -> eyre::Result<(ConnectParams, Child)> { ) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
self.inner.connect(config, cli_path).await self.inner.connect(config, cli_path).await
} }
} }
@ -37,13 +39,13 @@ impl InnerCliSession {
&self, &self,
config: &Config, config: &Config,
cli_path: &PathBuf, cli_path: &PathBuf,
) -> eyre::Result<(ConnectParams, Child)> { ) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
let proc = self.start(config, cli_path)?; let proc = self.start(config, cli_path)?;
let params = self.get_conn(proc).await?; let params = self.get_conn(proc).await?;
Ok(params) Ok(params)
} }
fn start(&self, config: &Config, cli_path: &PathBuf) -> eyre::Result<std::process::Child> { fn start(&self, config: &Config, cli_path: &PathBuf) -> eyre::Result<tokio::process::Child> {
let mut args: Vec<String> = vec!["session".into()]; let mut args: Vec<String> = vec!["session".into()];
if let Some(workspace) = &config.workdir_path { if let Some(workspace) = &config.workdir_path {
let abs_path = canonicalize(workspace)?; let abs_path = canonicalize(workspace)?;
@ -54,7 +56,7 @@ impl InnerCliSession {
args.extend(["--project".into(), abs_path.to_string_lossy().to_string()]) args.extend(["--project".into(), abs_path.to_string_lossy().to_string()])
} }
let proc = std::process::Command::new( let proc = tokio::process::Command::new(
cli_path cli_path
.to_str() .to_str()
.ok_or(eyre::anyhow!("could not get string from path"))?, .ok_or(eyre::anyhow!("could not get string from path"))?,
@ -72,8 +74,8 @@ impl InnerCliSession {
async fn get_conn( async fn get_conn(
&self, &self,
mut proc: std::process::Child, mut proc: tokio::process::Child,
) -> eyre::Result<(ConnectParams, std::process::Child)> { ) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
let stdout = proc let stdout = proc
.stdout .stdout
.take() .take()
@ -87,22 +89,22 @@ impl InnerCliSession {
let (sender, mut receiver) = tokio::sync::mpsc::channel(1); let (sender, mut receiver) = tokio::sync::mpsc::channel(1);
tokio::spawn(async move { tokio::spawn(async move {
let stdout_bufr = BufReader::new(stdout); let stdout_bufr = tokio::io::BufReader::new(stdout);
for line in stdout_bufr.lines() { for line in stdout_bufr.lines().next_line().await {
let out = line.as_ref().unwrap(); let out = line.as_ref().unwrap();
if let Ok(conn) = serde_json::from_str::<ConnectParams>(&out) { if let Ok(conn) = serde_json::from_str::<ConnectParams>(&out) {
sender.send(conn).await.unwrap(); sender.send(conn).await.unwrap();
} }
if let Ok(line) = line { if let Some(line) = line {
println!("dagger: {}", line); println!("dagger: {}", line);
} }
} }
}); });
tokio::spawn(async move { tokio::spawn(async move {
let stderr_bufr = BufReader::new(stderr); let stdout_bufr = tokio::io::BufReader::new(stderr);
for line in stderr_bufr.lines() { for line in stdout_bufr.lines().next_line().await {
if let Ok(line) = line { if let Some(line) = line {
println!("dagger: {}", line); println!("dagger: {}", line);
} }
//panic!("could not start dagger session: {}", out) //panic!("could not start dagger session: {}", out)

View File

@ -11,7 +11,7 @@ impl Engine {
Self {} Self {}
} }
async fn from_cli(&self, cfg: &Config) -> eyre::Result<(ConnectParams, Child)> { async fn from_cli(&self, cfg: &Config) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
let cli = Downloader::new("0.3.12".into())?.get_cli().await?; let cli = Downloader::new("0.3.12".into())?.get_cli().await?;
let cli_session = CliSession::new(); let cli_session = CliSession::new();
@ -19,7 +19,7 @@ impl Engine {
Ok(cli_session.connect(cfg, &cli).await?) Ok(cli_session.connect(cfg, &cli).await?)
} }
pub async fn start(&self, cfg: &Config) -> eyre::Result<(ConnectParams, Child)> { pub async fn start(&self, cfg: &Config) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
// TODO: Add from existing session as well // TODO: Add from existing session as well
self.from_cli(cfg).await self.from_cli(cfg).await
} }

View File

@ -3,8 +3,8 @@ use crate::querybuilder::Selection;
use dagger_core::connect_params::ConnectParams; use dagger_core::connect_params::ConnectParams;
use derive_builder::Builder; use derive_builder::Builder;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::process::Child;
use std::sync::Arc; use std::sync::Arc;
use tokio::process::Child;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct CacheId(String); pub struct CacheId(String);
@ -35,7 +35,7 @@ impl CacheVolume {
pub async fn id( pub async fn id(
&self, &self,
) -> eyre::Result<CacheId> { ) -> eyre::Result<CacheId> {
let query = self.selection.select("id"); let mut query = self.selection.select("id");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -216,7 +216,7 @@ impl Container {
pub async fn default_args( pub async fn default_args(
&self, &self,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let query = self.selection.select("defaultArgs"); let mut query = self.selection.select("defaultArgs");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -239,7 +239,7 @@ impl Container {
pub async fn entrypoint( pub async fn entrypoint(
&self, &self,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let query = self.selection.select("entrypoint"); let mut query = self.selection.select("entrypoint");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -258,7 +258,7 @@ impl Container {
pub fn env_variables( pub fn env_variables(
&self, &self,
) -> Vec<EnvVariable> { ) -> Vec<EnvVariable> {
let query = self.selection.select("envVariables"); let mut query = self.selection.select("envVariables");
return vec![EnvVariable { return vec![EnvVariable {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -274,7 +274,7 @@ impl Container {
pub fn exec( pub fn exec(
&self, &self,
) -> Container { ) -> Container {
let query = self.selection.select("exec"); let mut query = self.selection.select("exec");
return Container { return Container {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -321,7 +321,7 @@ impl Container {
pub async fn exit_code( pub async fn exit_code(
&self, &self,
) -> eyre::Result<isize> { ) -> eyre::Result<isize> {
let query = self.selection.select("exitCode"); let mut query = self.selection.select("exitCode");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -405,7 +405,7 @@ impl Container {
pub fn fs( pub fn fs(
&self, &self,
) -> Directory { ) -> Directory {
let query = self.selection.select("fs"); let mut query = self.selection.select("fs");
return Directory { return Directory {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -417,7 +417,7 @@ impl Container {
pub async fn id( pub async fn id(
&self, &self,
) -> eyre::Result<ContainerId> { ) -> eyre::Result<ContainerId> {
let query = self.selection.select("id"); let mut query = self.selection.select("id");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -436,7 +436,7 @@ impl Container {
pub fn labels( pub fn labels(
&self, &self,
) -> Vec<Label> { ) -> Vec<Label> {
let query = self.selection.select("labels"); let mut query = self.selection.select("labels");
return vec![Label { return vec![Label {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -448,7 +448,7 @@ impl Container {
pub async fn mounts( pub async fn mounts(
&self, &self,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let query = self.selection.select("mounts"); let mut query = self.selection.select("mounts");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -499,7 +499,7 @@ impl Container {
pub async fn platform( pub async fn platform(
&self, &self,
) -> eyre::Result<Platform> { ) -> eyre::Result<Platform> {
let query = self.selection.select("platform"); let mut query = self.selection.select("platform");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -546,7 +546,7 @@ impl Container {
pub fn rootfs( pub fn rootfs(
&self, &self,
) -> Directory { ) -> Directory {
let query = self.selection.select("rootfs"); let mut query = self.selection.select("rootfs");
return Directory { return Directory {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -559,7 +559,7 @@ impl Container {
pub async fn stderr( pub async fn stderr(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("stderr"); let mut query = self.selection.select("stderr");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -568,7 +568,7 @@ impl Container {
pub async fn stdout( pub async fn stdout(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("stdout"); let mut query = self.selection.select("stdout");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -576,7 +576,7 @@ impl Container {
pub async fn user( pub async fn user(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("user"); let mut query = self.selection.select("user");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -588,7 +588,7 @@ impl Container {
pub fn with_default_args( pub fn with_default_args(
&self, &self,
) -> Container { ) -> Container {
let query = self.selection.select("withDefaultArgs"); let mut query = self.selection.select("withDefaultArgs");
return Container { return Container {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -1188,7 +1188,7 @@ impl Container {
pub async fn workdir( pub async fn workdir(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("workdir"); let mut query = self.selection.select("workdir");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1298,7 +1298,7 @@ impl Directory {
pub fn docker_build( pub fn docker_build(
&self, &self,
) -> Container { ) -> Container {
let query = self.selection.select("dockerBuild"); let mut query = self.selection.select("dockerBuild");
return Container { return Container {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -1345,7 +1345,7 @@ impl Directory {
pub async fn entries( pub async fn entries(
&self, &self,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let query = self.selection.select("entries"); let mut query = self.selection.select("entries");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1397,7 +1397,7 @@ impl Directory {
pub async fn id( pub async fn id(
&self, &self,
) -> eyre::Result<DirectoryId> { ) -> eyre::Result<DirectoryId> {
let query = self.selection.select("id"); let mut query = self.selection.select("id");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1703,7 +1703,7 @@ impl EnvVariable {
pub async fn name( pub async fn name(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("name"); let mut query = self.selection.select("name");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1711,7 +1711,7 @@ impl EnvVariable {
pub async fn value( pub async fn value(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("value"); let mut query = self.selection.select("value");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1727,7 +1727,7 @@ impl File {
pub async fn contents( pub async fn contents(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("contents"); let mut query = self.selection.select("contents");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1746,7 +1746,7 @@ impl File {
pub async fn id( pub async fn id(
&self, &self,
) -> eyre::Result<FileId> { ) -> eyre::Result<FileId> {
let query = self.selection.select("id"); let mut query = self.selection.select("id");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1754,7 +1754,7 @@ impl File {
pub fn secret( pub fn secret(
&self, &self,
) -> Secret { ) -> Secret {
let query = self.selection.select("secret"); let mut query = self.selection.select("secret");
return Secret { return Secret {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -1766,7 +1766,7 @@ impl File {
pub async fn size( pub async fn size(
&self, &self,
) -> eyre::Result<isize> { ) -> eyre::Result<isize> {
let query = self.selection.select("size"); let mut query = self.selection.select("size");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1806,7 +1806,7 @@ impl GitRef {
pub async fn digest( pub async fn digest(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("digest"); let mut query = self.selection.select("digest");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1818,7 +1818,7 @@ impl GitRef {
pub fn tree( pub fn tree(
&self, &self,
) -> Directory { ) -> Directory {
let query = self.selection.select("tree"); let mut query = self.selection.select("tree");
return Directory { return Directory {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -1878,7 +1878,7 @@ impl GitRepository {
pub async fn branches( pub async fn branches(
&self, &self,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let query = self.selection.select("branches"); let mut query = self.selection.select("branches");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -1916,7 +1916,7 @@ impl GitRepository {
pub async fn tags( pub async fn tags(
&self, &self,
) -> eyre::Result<Vec<String>> { ) -> eyre::Result<Vec<String>> {
let query = self.selection.select("tags"); let mut query = self.selection.select("tags");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2029,7 +2029,7 @@ impl Host {
pub fn workdir( pub fn workdir(
&self, &self,
) -> Directory { ) -> Directory {
let query = self.selection.select("workdir"); let mut query = self.selection.select("workdir");
return Directory { return Directory {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2074,7 +2074,7 @@ impl HostVariable {
pub fn secret( pub fn secret(
&self, &self,
) -> Secret { ) -> Secret {
let query = self.selection.select("secret"); let mut query = self.selection.select("secret");
return Secret { return Secret {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2086,7 +2086,7 @@ impl HostVariable {
pub async fn value( pub async fn value(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("value"); let mut query = self.selection.select("value");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2102,7 +2102,7 @@ impl Label {
pub async fn name( pub async fn name(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("name"); let mut query = self.selection.select("name");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2110,7 +2110,7 @@ impl Label {
pub async fn value( pub async fn value(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("value"); let mut query = self.selection.select("value");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2126,7 +2126,7 @@ impl Project {
pub fn extensions( pub fn extensions(
&self, &self,
) -> Vec<Project> { ) -> Vec<Project> {
let query = self.selection.select("extensions"); let mut query = self.selection.select("extensions");
return vec![Project { return vec![Project {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2138,7 +2138,7 @@ impl Project {
pub fn generated_code( pub fn generated_code(
&self, &self,
) -> Directory { ) -> Directory {
let query = self.selection.select("generatedCode"); let mut query = self.selection.select("generatedCode");
return Directory { return Directory {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2150,7 +2150,7 @@ impl Project {
pub async fn install( pub async fn install(
&self, &self,
) -> eyre::Result<bool> { ) -> eyre::Result<bool> {
let query = self.selection.select("install"); let mut query = self.selection.select("install");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2158,7 +2158,7 @@ impl Project {
pub async fn name( pub async fn name(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("name"); let mut query = self.selection.select("name");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2166,7 +2166,7 @@ impl Project {
pub async fn schema( pub async fn schema(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("schema"); let mut query = self.selection.select("schema");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2174,7 +2174,7 @@ impl Project {
pub async fn sdk( pub async fn sdk(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("sdk"); let mut query = self.selection.select("sdk");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2248,7 +2248,7 @@ impl Query {
pub fn container( pub fn container(
&self, &self,
) -> Container { ) -> Container {
let query = self.selection.select("container"); let mut query = self.selection.select("container");
return Container { return Container {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2287,7 +2287,7 @@ impl Query {
pub async fn default_platform( pub async fn default_platform(
&self, &self,
) -> eyre::Result<Platform> { ) -> eyre::Result<Platform> {
let query = self.selection.select("defaultPlatform"); let mut query = self.selection.select("defaultPlatform");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2299,7 +2299,7 @@ impl Query {
pub fn directory( pub fn directory(
&self, &self,
) -> Directory { ) -> Directory {
let query = self.selection.select("directory"); let mut query = self.selection.select("directory");
return Directory { return Directory {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2391,7 +2391,7 @@ impl Query {
pub fn host( pub fn host(
&self, &self,
) -> Host { ) -> Host {
let query = self.selection.select("host"); let mut query = self.selection.select("host");
return Host { return Host {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2495,7 +2495,7 @@ impl Query {
pub fn socket( pub fn socket(
&self, &self,
) -> Socket { ) -> Socket {
let query = self.selection.select("socket"); let mut query = self.selection.select("socket");
return Socket { return Socket {
proc: self.proc.clone(), proc: self.proc.clone(),
@ -2537,7 +2537,7 @@ impl Secret {
pub async fn id( pub async fn id(
&self, &self,
) -> eyre::Result<SecretId> { ) -> eyre::Result<SecretId> {
let query = self.selection.select("id"); let mut query = self.selection.select("id");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2545,7 +2545,7 @@ impl Secret {
pub async fn plaintext( pub async fn plaintext(
&self, &self,
) -> eyre::Result<String> { ) -> eyre::Result<String> {
let query = self.selection.select("plaintext"); let mut query = self.selection.select("plaintext");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }
@ -2561,7 +2561,7 @@ impl Socket {
pub async fn id( pub async fn id(
&self, &self,
) -> eyre::Result<SocketId> { ) -> eyre::Result<SocketId> {
let query = self.selection.select("id"); let mut query = self.selection.select("id");
query.execute(&graphql_client(&self.conn)).await query.execute(&graphql_client(&self.conn)).await
} }