Skip to content
Draft
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
1 change: 1 addition & 0 deletions desktop/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tauri-build = { version = "2", features = [] }

[target.'cfg(unix)'.dependencies]
libc = "0.2"
nix = { version = "0.31", default-features = false, features = ["signal", "process"] }
ctrlc = { version = "3", features = ["termination"] }

[target.'cfg(target_os = "linux")'.dependencies]
Expand All @@ -62,7 +63,7 @@ user-idle = { version = "0.6", default-features = false }
plist = "1"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.61", features = ["Win32_Security", "Win32_Storage_FileSystem", "Win32_System_JobObjects", "Win32_System_Registry", "Win32_System_Threading", "Win32_Foundation"] }
windows-sys = { version = "0.61", features = ["Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Diagnostics_ToolHelp", "Win32_System_JobObjects", "Win32_System_Registry", "Win32_System_Threading", "Win32_Foundation"] }
keyring = { version = "3.6.3", default-features = false, features = ["windows-native", "vendored"], optional = true }
user-idle = { version = "0.6", default-features = false }

Expand Down
7 changes: 6 additions & 1 deletion desktop/src-tauri/src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::HashMap,
collections::{HashMap, HashSet},
io::Write,
sync::{
atomic::{AtomicBool, AtomicU16, AtomicU8},
Expand Down Expand Up @@ -52,6 +52,10 @@ pub struct AppState {
pub managed_agents_store_lock: Mutex<()>,
pub channel_templates_store_lock: Mutex<()>,
pub managed_agent_processes: Mutex<HashMap<ManagedAgentRuntimeKey, ManagedAgentPairRuntime>>,
/// Per-pair intent tokens spanning the unlocked process-preparation/spawn
/// phase. The mutex is held only to insert/remove a key; no filesystem,
/// readiness, or child-process work may run while it is held.
pub managed_agent_start_reservations: Mutex<HashSet<ManagedAgentRuntimeKey>>,
pub huddle_state: Mutex<HuddleState>,
pub huddle_audio: crate::huddle::tts_settings::HuddleAudioSettingsState,
/// Tauri app handle — stored after setup so huddle commands can emit
Expand Down Expand Up @@ -215,6 +219,7 @@ pub fn build_app_state() -> AppState {
managed_agents_store_lock: Mutex::new(()),
channel_templates_store_lock: Mutex::new(()),
managed_agent_processes: Mutex::new(HashMap::new()),
managed_agent_start_reservations: Mutex::new(HashSet::new()),
session_config_cache: Mutex::new(HashMap::new()),
huddle_state: Mutex::new(HuddleState::default()),
huddle_audio: Default::default(),
Expand Down
26 changes: 20 additions & 6 deletions desktop/src-tauri/src/commands/agent_discovery/install_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ fn await_install_child(
// Save the PID before moving `child` into the wait thread so we can
// kill the process on timeout.
let child_pid = child.id();
#[cfg(windows)]
let child_identity = crate::managed_agents::child_process_identity(&child).ok();

std::thread::spawn(move || {
let _ = events_tx.send(Settled::Exited(child.wait()));
Expand All @@ -257,6 +259,11 @@ fn await_install_child(
// was still running and this is a timeout — the status the kill produces
// moments later describes the kill, not the install, so it is discarded.
let install_finished = settle.status.is_some();
#[cfg(unix)]
terminate_install_group(child_pid);
#[cfg(windows)]
terminate_install_group(child_pid, child_identity);
#[cfg(not(any(unix, windows)))]
terminate_install_group(child_pid);
// Reaping the child and finishing the drains share one bound. Both
// normally complete within microseconds of the kill, which closes the
Expand Down Expand Up @@ -427,14 +434,21 @@ fn signal_reaches(target: i32) -> bool {
std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH)
}

/// Windows has no process groups on this path: `terminate_process` runs
/// `taskkill /T /F`, which is already tree-wide and unconditional, so there is
/// no escalation to get wrong.
#[cfg(not(unix))]
fn terminate_install_group(pid: u32) {
let _ = crate::managed_agents::terminate_process(pid);
/// Windows binds cleanup to the creation identity captured from the spawned
/// child's stable handle. If metadata could not be read, teardown fails closed
/// rather than acting on a potentially recycled PID.
#[cfg(windows)]
fn terminate_install_group(pid: u32, process_identity: Option<u64>) {
if let Err(error) =
crate::managed_agents::terminate_process_with_identity(pid, process_identity)
{
eprintln!("buzz-desktop: bounded Windows installer cleanup failed: {error}");
}
}

#[cfg(not(any(unix, windows)))]
fn terminate_install_group(_pid: u32) {}

/// A failure carrying whatever the drains captured, with `reason` leading
/// stderr so the surfaced message names the failure before the install's own
/// output.
Expand Down
57 changes: 46 additions & 11 deletions desktop/src-tauri/src/commands/agent_discovery/managed_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ pub(super) fn managed_node_runtime_ready() -> bool {
///
/// Cleanup: the child runs in its own process group on Unix (`process_group(0)`)
/// so an unconditional group SIGKILL on every exit path terminates all
/// descendants. On Windows, `terminate_process` issues `taskkill /T /F` for
/// tree-wide cleanup. SIGKILL to an already-dead group returns ESRCH (no-op).
/// descendants. On Windows, it is spawned suspended, assigned to a dedicated
/// kill-on-close Job Object, and only then resumed. SIGKILL to an already-dead
/// Unix group returns ESRCH (no-op).
pub(super) fn probe_node(
executable: &std::path::Path,
expected_version: &str,
Expand All @@ -148,8 +149,15 @@ pub(super) fn probe_node(
use std::os::unix::process::CommandExt;
cmd.process_group(0);
}
let Ok(mut child) = cmd.spawn() else {
return false;
#[cfg(windows)]
let (mut child, mut probe_job) = match crate::managed_agents::spawn_probe_in_job(&mut cmd) {
Ok(contained) => contained,
Err(_) => return false,
};
#[cfg(not(windows))]
let mut child = match cmd.spawn() {
Ok(child) => child,
Err(_) => return false,
};

let deadline = std::time::Instant::now() + timeout;
Expand All @@ -158,22 +166,36 @@ pub(super) fn probe_node(
Ok(Some(status)) => break status,
Ok(None) => {
if std::time::Instant::now() >= deadline {
kill_probe_group(child.id());
let _ = terminate_probe_group(
&mut child,
#[cfg(windows)]
&mut probe_job,
);
let _ = child.wait();
return false;
}
std::thread::sleep(Duration::from_millis(50));
}
Err(_) => {
kill_probe_group(child.id());
let _ = terminate_probe_group(
&mut child,
#[cfg(windows)]
&mut probe_job,
);
let _ = child.wait();
return false;
}
}
};

// Group-kill unconditionally: SIGKILL to a dead group is ESRCH (no-op).
kill_probe_group(child.id());
if !terminate_probe_group(
&mut child,
#[cfg(windows)]
&mut probe_job,
) {
return false;
}

if !exit_status.success() {
return false;
Expand All @@ -188,18 +210,31 @@ pub(super) fn probe_node(

/// Kill the probe's process group/tree unconditionally (no TERM grace — this
/// is a probe, not an agent session). ESRCH on a dead group is fine.
fn kill_probe_group(pid: u32) {
fn terminate_probe_group(
child: &mut std::process::Child,
#[cfg(windows)] probe_job: &mut crate::managed_agents::JobHandle,
) -> bool {
#[cfg(unix)]
unsafe {
libc::kill(-(pid as i32), libc::SIGKILL);
libc::kill(-(child.id() as i32), libc::SIGKILL);
true
}
#[cfg(windows)]
{
let _ = crate::managed_agents::terminate_process(pid);
let cleaned = match probe_job.terminate_and_wait(Duration::from_secs(1)) {
Ok(()) => true,
Err(error) => {
eprintln!("buzz-desktop: managed Node readiness cleanup failed: {error}");
false
}
};
let _ = child.kill();
cleaned
}
#[cfg(not(any(unix, windows)))]
{
let _ = pid;
let _ = child;
true
}
}

Expand Down
Loading