Skip to content
Merged
Show file tree
Hide file tree
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
215 changes: 209 additions & 6 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,156 @@ fn gc_codex_homes() {
});
}

/// The `--plugin-dir` values of every live process, read once per sweep and
/// again just before a tree is retired.
///
/// Claude Code records a plugin by the absolute path it was launched with, and
/// `--plugin-dir <runtime>/<plugin>` stays bound to that runtime for the life
/// of the process. If its `clauth start` dies (killed, OOM) while the `claude`
/// keeps running, the marker lock dies with it and the config-dir liveness
/// reads no longer hold the tree, yet `/reload-plugins` still resolves the
/// plugin through it. Sparing on the launch-time path keeps that reload working.
pub(crate) enum LivePluginDirs {
/// Every readable live process's `--plugin-dir` values.
Named(Vec<PathBuf>),
/// The process table could not be listed: spare every tree this sweep, the
/// fail-closed direction the job-store snapshot takes.
Unreadable,
/// Plugin dirs play no part (the delete path, where the profile dir goes
/// either way and a spared tree would read as unrescued).
Off,
}

impl LivePluginDirs {
/// The snapshot to re-take before retiring a tree: a fresh read, except
/// that [`LivePluginDirs::Off`] stays off.
fn refreshed(&self) -> LivePluginDirs {
match self {
LivePluginDirs::Off => LivePluginDirs::Off,
_ => live_plugin_dirs(),
}
}

/// A snapshot holding exactly `dirs`.
#[cfg(test)]
fn named(dirs: Vec<PathBuf>) -> Self {
LivePluginDirs::Named(dirs)
}

/// Whether a live process's `--plugin-dir` lies inside `tree`, or the
/// snapshot is unreadable.
fn spares(&self, tree: &Path) -> bool {
match self {
LivePluginDirs::Unreadable => true,
LivePluginDirs::Off => false,
LivePluginDirs::Named(dirs) => dirs.iter().any(|dir| plugin_dir_inside_tree(dir, tree)),
}
}
}

/// Every value an argv gives `--plugin-dir`, in both the `--plugin-dir X` and
/// `--plugin-dir=X` spellings. PURE.
fn argv_plugin_dirs(argv: &[&[u8]]) -> Vec<PathBuf> {
let mut dirs = Vec::new();
let mut args = argv.iter();
while let Some(arg) = args.next() {
let value = if *arg == b"--plugin-dir" {
args.next().copied()
} else {
arg.strip_prefix(b"--plugin-dir=")
};
if let Some(value) = value.filter(|v| !v.is_empty()) {
dirs.push(bytes_to_path(value));
}
}
dirs
}

#[cfg(unix)]
fn bytes_to_path(bytes: &[u8]) -> PathBuf {
use std::os::unix::ffi::OsStrExt as _;
PathBuf::from(std::ffi::OsStr::from_bytes(bytes))
}

#[cfg(not(unix))]
fn bytes_to_path(bytes: &[u8]) -> PathBuf {
PathBuf::from(String::from_utf8_lossy(bytes).into_owned())
}

/// Whether `dir` lies inside `tree`, by path prefix in the tree's walked or
/// canonical spelling. Never canonicalizes `dir`: the plugin entry is a symlink
/// out of the tree, and following it would hide the very reference this check
/// exists to see. PURE apart from the tree's own canonicalize.
fn plugin_dir_inside_tree(dir: &Path, tree: &Path) -> bool {
dir.starts_with(tree)
|| crate::canonical::identity(tree).is_ok_and(|canonical| dir.starts_with(canonical))
}

/// One snapshot of every live process's `--plugin-dir` values. Linux reads
/// `/proc/<pid>/cmdline`, macOS the `KERN_PROCARGS2` argv; a process whose argv
/// the kernel withholds (another user's) is skipped, and elsewhere nothing is
/// inspected, so behaviour there is unchanged.
fn live_plugin_dirs() -> LivePluginDirs {
#[cfg(test)]
if let Some(dirs) = PLUGIN_DIRS_OVERRIDE.with(|slot| slot.borrow().clone()) {
return LivePluginDirs::Named(dirs);
}
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let Some(pids) = live_process_pids() else {
return LivePluginDirs::Unreadable;
};
LivePluginDirs::Named(
pids.into_iter()
.filter(|pid| process_is_ours(*pid))
.filter_map(process_argv)
Comment thread
uwuclxdy marked this conversation as resolved.
Resolved
.flat_map(|argv| {
let argv: Vec<&[u8]> = argv.iter().map(Vec::as_slice).collect();
argv_plugin_dirs(&argv)
})
.collect(),
)
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
LivePluginDirs::Named(Vec::new())
}
}

/// Whether `/proc/<pid>` belongs to the user running clauth. Linux serves
/// `cmdline` of every user's process, so another user's argv must not keep a
/// tree on disk; macOS already withholds those argvs. A pid that vanished
/// reads as not ours.
#[cfg(any(target_os = "linux", target_os = "macos"))]
fn process_is_ours(pid: u32) -> bool {
#[cfg(target_os = "linux")]
{
path_owned_by_euid(&PathBuf::from(format!("/proc/{pid}")))
}
#[cfg(target_os = "macos")]
{
let _ = pid;
true
}
}

/// Whether `path` is owned by the effective uid of this process.
#[cfg(target_os = "linux")]
fn path_owned_by_euid(path: &Path) -> bool {
use std::os::unix::fs::MetadataExt as _;
// SAFETY: `geteuid` takes no arguments and cannot fail.
#[allow(unsafe_code)]
let euid = unsafe { libc::geteuid() };
std::fs::metadata(path).is_ok_and(|meta| meta.uid() == euid)
}

#[cfg(test)]
thread_local! {
/// Test seam: when set, [`live_plugin_dirs`] returns exactly these dirs.
static PLUGIN_DIRS_OVERRIDE: std::cell::RefCell<Option<Vec<PathBuf>>> =
const { std::cell::RefCell::new(None) };
}

fn gc_runtime_trees() {
// One snapshot of Claude Code's job store per sweep, before the pair loop,
// so the fast path never pays the walk per pair. A dead pair the snapshot
Expand Down Expand Up @@ -2139,16 +2289,23 @@ fn gc_runtime_trees() {
return;
};
let _rescue_wait = crate::sessions::RescueLockBudget::arm();
let plugin_dirs = live_plugin_dirs();
if matches!(plugin_dirs, LivePluginDirs::Unreadable) {
logline!(
"clauth: spared every dead runtime tree this sweep: the live \
process table could not be read for --plugin-dir"
);
}
for profile in entries.flatten() {
gc_profile_trees(&profile.path(), &job_trees);
gc_profile_trees(&profile.path(), &job_trees, &plugin_dirs);
}
}

/// One profile's half of [`gc_runtime_trees`]: every kept rescue tombstone
/// first, then every dead runtime tree, retired and rescued. Tombstones go
/// first because a kept `runtime.rescuing` blocks the rename that would retire
/// a dead `runtime` beside it.
fn gc_profile_trees(profile: &Path, job_trees: &JobTrees) {
fn gc_profile_trees(profile: &Path, job_trees: &JobTrees, plugin_dirs: &LivePluginDirs) {
// Strict predicates, not the loose pairing split: these walks hand
// `remove_dir_all` whatever they match, so a future `runtime_state` or
// `sessions.json` under a profile must fall through untouched.
Expand All @@ -2175,6 +2332,7 @@ fn gc_profile_trees(profile: &Path, job_trees: &JobTrees) {
&profile.join(&child_name),
&profile.join(sessions),
job_trees,
plugin_dirs,
);
} else if is_sessions_dir_name(&child_name)
&& let Some(runtime) = paired_runtime_name(&child_name)
Expand All @@ -2185,7 +2343,7 @@ fn gc_profile_trees(profile: &Path, job_trees: &JobTrees) {
// rather than a reused one.
let runtime = profile.join(runtime);
if runtime.symlink_metadata().is_err() {
let _ = gc_one_pair(&runtime, &profile.join(&child_name), job_trees);
let _ = gc_one_pair(&runtime, &profile.join(&child_name), job_trees, plugin_dirs);
}
}
}
Expand Down Expand Up @@ -2218,7 +2376,14 @@ pub(crate) fn rescue_profile_trees(profile: &Path) -> Option<PathBuf> {
#[cfg(target_os = "macos")]
let _item_budget = crate::lock::SharedSubprocessBudget::arm(crate::lock::SUBPROCESS_BUDGET);
let _rescue_wait = crate::sessions::RescueLockBudget::arm();
gc_profile_trees(profile, &crate::claude_jobs::job_trees());
gc_profile_trees(
profile,
&crate::claude_jobs::job_trees(),
// The profile dir goes either way: a tree spared here would count as
// unrescued, so `delete` would refuse and `delete --force` would drop
// its transcripts unrescued.
&LivePluginDirs::Off,
);
let children = match std::fs::read_dir(profile) {
Ok(children) => children,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return None,
Expand Down Expand Up @@ -3829,7 +3994,12 @@ fn rescue_claimed_tombstone(tombstone: &Path) {
/// it is renamed to a tombstone under the lock and rescued outside it
/// ([`retire_tree`]), and the flock is never held across a tree-sized walk; a
/// runtime path that is itself a link is unlinked alone.
fn gc_one_pair(runtime: &Path, sessions: &Path, job_trees: &JobTrees) -> Result<()> {
fn gc_one_pair(
runtime: &Path,
sessions: &Path,
job_trees: &JobTrees,
plugin_dirs: &LivePluginDirs,
) -> Result<()> {
// The job check sits here as a FAST PATH, before the state flock
// `gc_one_pair_synced` takes: the store was read once above and the check
// is a canonicalize + set lookup, not a walk. It is not the decision — a
Expand All @@ -3851,7 +4021,20 @@ fn gc_one_pair(runtime: &Path, sessions: &Path, job_trees: &JobTrees) -> Result<
}
return Ok(());
}
gc_one_pair_synced(runtime, sessions, || {})
// A live process's launch-time `--plugin-dir` into this tree spares it even
// when nothing else holds the tree: its `clauth start` died (killed, OOM)
// and took the marker lock with it while `claude` keeps running. This is
// the fast path off the sweep's snapshot; `gc_one_pair_synced_with`
// re-reads before it retires the tree.
if plugin_dirs.spares(runtime) {
Comment thread
uwuclxdy marked this conversation as resolved.
Resolved
logline!(
"clauth: spared runtime tree {}: a live process still loads a plugin from it \
(--plugin-dir)",
runtime.display()
);
return Ok(());
}
gc_one_pair_synced_with(runtime, sessions, plugin_dirs, || {})
}

#[cfg(test)]
Expand Down Expand Up @@ -3900,10 +4083,22 @@ fn gc_held_marker_set(file: File) {
/// The removal hold renames the tree to its tombstone ([`retire_tree`]), and
/// the rescue runs once the lock drops, because nothing but that tree may hold
/// what its dead session wrote.
#[cfg(test)]
fn gc_one_pair_synced(
runtime: &Path,
sessions: &Path,
remint_interleave: impl FnOnce(),
) -> Result<()> {
gc_one_pair_synced_with(runtime, sessions, &LivePluginDirs::Off, remint_interleave)
}

/// [`gc_one_pair_synced`] with the plugin-dir mode: [`LivePluginDirs::Off`]
/// skips the plugin-dir re-read, anything else re-reads it fresh.
fn gc_one_pair_synced_with(
runtime: &Path,
sessions: &Path,
plugin_dirs: &LivePluginDirs,
remint_interleave: impl FnOnce(),
) -> Result<()> {
// macOS: the tree's namespaced Keychain service, derived while the dir
// still exists — the derivation canonicalizes it, so it cannot run once
Expand Down Expand Up @@ -3969,13 +4164,21 @@ fn gc_one_pair_synced(
#[cfg(test)]
on_gc_recheck_hold(runtime, sessions);

// Re-read the live `--plugin-dir`s: a `claude` launched with one into this
// tree after the sweep's snapshot must still spare it. Taken here, just
// before the hold, because on macOS the process listing shells out to
// `ps` and no subprocess may span the state flock; the hold below then
// acts on this read the way it acts on the marker and job re-reads.
let plugin_spare = plugin_dirs.refreshed().spares(runtime);

// Re-check under a FRESH lock hold, then collect: between the decide above
// and this hold a concurrently starting session can re-mint this same path,
// so the removal keys on the world it runs in — markers, the job store AND
// the lock file, all re-read fresh — the same shape the macOS Keychain
// delete below takes.
let renamed = with_state_lock(|_held| {
let spares = prune_stale_sessions(sessions).unwrap_or(1) != 0
|| plugin_spare
|| crate::claude_jobs::job_trees().spares(runtime)
|| !matches!(
supervisor_state(runtime),
Expand Down
Loading
Loading