use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use lean_ctx::ipc::process::{self, ProcessIdentity};
const DIRECTORY: &str = "mcp-instances";
const MAX_RECORD_BYTES: u64 = 4096;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct Instance {
pid: u32,
parent_pid: u32,
identity: ProcessIdentity,
}
pub(super) struct Registration {
path: PathBuf,
}
impl Drop for Registration {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.path);
}
}
fn directory() -> Option<PathBuf> {
lean_ctx::core::data_dir::lean_ctx_data_dir()
.ok()
.map(|data| data.join(DIRECTORY))
}
fn record_path(directory: &Path, pid: u32) -> PathBuf {
directory.join(format!("{pid}.json"))
}
pub(super) fn register() -> Option<Registration> {
let pid = std::process::id();
let directory = directory()?;
std::fs::create_dir_all(&directory).ok()?;
let path = record_path(&directory, pid);
write(
&path,
&Instance {
pid,
parent_pid: std::os::unix::process::parent_id(),
identity: process::identity(pid)?,
},
)?;
Some(Registration { path })
}
fn write(path: &Path, instance: &Instance) -> Option<()> {
let bytes = serde_json::to_vec(instance).ok()?;
lean_ctx::core::atomic_fs::try_atomic_write(path, &bytes, None).ok()
}
fn read(path: &Path) -> Option<Instance> {
let metadata = std::fs::symlink_metadata(path).ok()?;
if !metadata.is_file() || metadata.len() > MAX_RECORD_BYTES {
return None;
}
let instance: Instance = serde_json::from_slice(&std::fs::read(path).ok()?).ok()?;
(path.file_name()?.to_str()? == format!("{}.json", instance.pid)).then_some(instance)
}
pub(super) fn terminate_own_orphans(lost_parent: impl Fn(u32) -> bool) {
if let Some(own) = process::identity(std::process::id()) {
terminate_orphans_of(&own.executable, lost_parent);
}
}
fn terminate_orphans_of(executable: &str, lost_parent: impl Fn(u32) -> bool) {
let Some(directory) = directory() else {
return;
};
let Ok(entries) = std::fs::read_dir(&directory) else {
return;
};
let me = std::process::id();
for path in entries.flatten().map(|entry| entry.path()) {
let Some(instance) = read(&path) else {
continue;
};
if instance.pid == me {
continue;
}
if !process::is_alive(instance.pid) {
let _ = std::fs::remove_file(&path);
continue;
}
match process::identity(instance.pid) {
None => continue,
Some(live) if live != instance.identity => {
let _ = std::fs::remove_file(&path);
continue;
}
Some(_) => {}
}
if instance.identity.executable != executable {
continue;
}
if instance.parent_pid <= 1 || !lost_parent(instance.pid) {
continue;
}
if process::signal_same_process(
instance.pid,
&instance.identity,
process::StopSignal::Terminate,
) {
tracing::info!(
"[orphan-cleanup] terminated own orphaned MCP server {}",
instance.pid
);
}
let _ = std::fs::remove_file(&path);
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};
fn sleeper() -> Child {
long_running("sleep", &["60"])
}
fn long_running(program: &str, arguments: &[&str]) -> Child {
let child = Command::new(program)
.args(arguments)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.expect("process starts");
let own = process::identity(std::process::id()).map(|identity| identity.executable);
let deadline = Instant::now() + Duration::from_secs(10);
while Instant::now() < deadline {
match process::identity(child.id()) {
Some(identity) if Some(&identity.executable) != own.as_ref() => break,
_ => std::thread::sleep(Duration::from_millis(10)),
}
}
child
}
fn record(directory: &Path, child: &Child, change: impl FnOnce(&mut Instance)) -> PathBuf {
let mut instance = Instance {
pid: child.id(),
parent_pid: std::process::id(),
identity: process::identity(child.id()).expect("child identity"),
};
change(&mut instance);
let path = record_path(directory, child.id());
write(&path, &instance).expect("record written");
path
}
fn exited(child: &mut Child) -> bool {
let deadline = Instant::now() + Duration::from_secs(30);
while Instant::now() < deadline {
if child.try_wait().expect("child state").is_some() {
return true;
}
std::thread::sleep(Duration::from_millis(20));
}
false
}
#[test]
fn only_recorded_identity_proven_parented_servers_are_terminated() {
let _data = lean_ctx::core::data_dir::isolated_data_dir();
let directory = directory().expect("data directory");
std::fs::create_dir_all(&directory).unwrap();
let mut unrecorded = sleeper();
let mut reused_pid = sleeper();
let mut started_without_parent = sleeper();
let mut other_binary = long_running("tail", &["-f", "/dev/null"]);
let mut own_orphan = sleeper();
let ours = process::identity(own_orphan.id())
.expect("child identity")
.executable;
let stale = record(&directory, &reused_pid, |instance| {
instance.identity.start_marker = instance.identity.start_marker.wrapping_add(1);
});
let kept = record(&directory, &started_without_parent, |instance| {
instance.parent_pid = 1;
});
let foreign = record(&directory, &other_binary, |_| {});
let orphan = record(&directory, &own_orphan, |_| {});
let own = register().expect("own record");
terminate_orphans_of(&ours, |_| true);
assert!(exited(&mut own_orphan), "the proven own orphan is ended");
assert!(!orphan.exists());
for (name, child) in [
("no record", &mut unrecorded),
("record of another process generation", &mut reused_pid),
(
"recorded as started without a parent",
&mut started_without_parent,
),
("recorded orphan of another executable", &mut other_binary),
] {
assert!(
child.try_wait().unwrap().is_none(),
"{name}: must not be signalled"
);
}
assert!(!stale.exists(), "a record for a reused PID is forgotten");
assert!(kept.exists() && foreign.exists());
assert!(own.path.exists(), "the running server keeps its own record");
let sessions = [
record(&directory, &unrecorded, |_| {}),
record(&directory, &reused_pid, |_| {}),
];
terminate_orphans_of(&ours, |_| false);
assert!(unrecorded.try_wait().unwrap().is_none());
assert!(reused_pid.try_wait().unwrap().is_none());
assert!(sessions.iter().all(|path| path.exists()));
terminate_own_orphans(|_| true);
assert!(unrecorded.try_wait().unwrap().is_none());
assert!(other_binary.try_wait().unwrap().is_none());
drop(own);
for child in [
&mut unrecorded,
&mut reused_pid,
&mut started_without_parent,
&mut other_binary,
] {
child.kill().unwrap();
child.wait().unwrap();
}
}
}