use std::{
cell::RefCell,
fs,
path::{Path, PathBuf},
rc::Rc,
sync::{Arc, Mutex},
time::Duration,
};
use crate::support::os;
pub struct RetainedInputs {
_directory: Rc<RefCell<Option<tempfile::TempDir>>>,
}
struct Handoff {
pid: u32,
}
pub struct Launched {
root: PathBuf,
inputs: Rc<RefCell<Option<tempfile::TempDir>>>,
processes: Option<super::Processes>,
handoff: Option<Handoff>,
launch: Vec<(u32, u64)>,
native: Arc<Mutex<Option<artifactize_core::process::ChildIdentity>>>,
}
impl Launched {
pub fn new(root: tempfile::TempDir) -> Self {
Self {
root: root.path().to_owned(),
inputs: Rc::new(RefCell::new(Some(root))),
processes: Some(super::Processes::default()),
handoff: None,
launch: Vec::new(),
native: Arc::default(),
}
}
pub fn path(&self) -> &Path {
&self.root
}
pub fn retain_inputs(&self) -> RetainedInputs {
RetainedInputs {
_directory: self.inputs.clone(),
}
}
pub fn observe(
&self,
run: artifactize_core::review::log::Run,
) -> artifactize_core::review::log::Run {
let address = self.native.clone();
run.observe_launch(move |identity| *address.lock().unwrap() = Some(identity))
}
pub fn native_identity(&self) -> Option<artifactize_core::process::ChildIdentity> {
*self.native.lock().unwrap()
}
fn adopt_launch(&mut self) -> bool {
if let Some(identity) = self.native_identity() {
self.own_identity(identity.pid.get(), identity.start_time);
}
let Ok(bytes) = fs::read(self.path().join("launch-identities")) else {
return !self.launch.is_empty();
};
let Ok(pids) = serde_json::from_slice::<Vec<u32>>(&bytes) else {
return !self.launch.is_empty();
};
for pid in pids {
if let Some(start) = os::observed_start_time(pid) {
self.own_identity(pid, start);
}
}
!self.launch.is_empty()
}
fn own_identity(&mut self, pid: u32, start: u64) {
if !self.launch.contains(&(pid, start)) {
self.processes.as_mut().unwrap().own_identity(pid, start);
self.launch.push((pid, start));
}
}
pub fn own(&mut self) -> Option<u32> {
if let Some(handoff) = &self.handoff {
return Some(handoff.pid);
}
self.adopt_launch();
let pid = fs::read_to_string(self.path().join("pid"))
.ok()?
.parse()
.ok()?;
self.own_identity(pid, os::observed_start_time(pid)?);
self.adopt_launch();
self.handoff = Some(Handoff { pid });
Some(pid)
}
pub fn close(mut self) -> Result<(), String> {
self.release()
}
fn release(&mut self) -> Result<(), String> {
if self.processes.is_none() {
return Ok(());
}
let _ = fs::write(self.path().join("release-handoff"), "");
const HANDOFF_BOUND: Duration = Duration::from_secs(30);
const HANDOFF_POLL: Duration = Duration::from_millis(10);
let deadline = std::time::Instant::now() + os::patience(HANDOFF_BOUND);
loop {
if self.own().is_some()
|| self.adopt_launch() && self.path().join("withhold-handoff").exists()
{
break;
}
if std::time::Instant::now() >= deadline {
break;
}
std::thread::sleep(HANDOFF_POLL);
}
let released = fs::write(self.path().join("release"), "");
let joined = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
self.processes
.take()
.unwrap()
.release()
.expect("join detached launch");
}));
let exited = !self.launch.is_empty()
&& self
.launch
.iter()
.all(|&(pid, start)| !os::same_process_running(pid, start));
if released.is_err() || joined.is_err() || !exited {
let retained = self.inputs.borrow_mut().take().unwrap().keep();
return Err(format!(
"Detached launch cleanup failed; inputs retained at {}",
os::path_text(&retained)
));
}
Ok(())
}
}
impl Drop for Launched {
fn drop(&mut self) {
if let Err(message) = self.release() {
if std::thread::panicking() {
eprintln!("{message}");
} else {
panic!("{message}");
}
}
}
}
pub const LAUNCH: &str = r"import json, os, pathlib, sys, time
root = pathlib.Path(sys.argv[1]) if len(sys.argv) > 1 else pathlib.Path.cwd()
def write_marker(name, contents):
pending = root / (name + '.pending')
pending.write_text(contents)
pending.replace(root / name)
if (root / 'withhold-handoff').exists():
write_marker('handoff-ready', '')
while not (root / 'release').exists(): time.sleep(0.01)
raise SystemExit(0)
write_marker('launch-identities', json.dumps(globals().get('OWNED_PIDS', [os.getpid()])))
if (root / 'hold-handoff').exists():
(root / 'handoff-ready').write_text('')
while not (root / 'release-handoff').exists(): time.sleep(0.01)
for name, contents in [('pid', str(os.getpid())), ('marker', globals().get('MARKER') or os.environ.get('ARTIFACTIZE_HUMAN_MARKER', 'launched'))]:
write_marker(name, contents)
while not (root / 'release').exists(): time.sleep(0.01)
";