Skip to content

armgit / muxin

Public Imported from GitHub
0

All your machines, one app. For you and your AI agents: agent sessions, desktops, files, logs and GitHub across your Macs, Linux and Windows machines, with an MCP server.

Linux: started from a terminal, Muxin no longer gets stopped

Adib Hanna8f638d624111
diff --git a/Cargo.lock b/Cargo.lock
index 842be53..47b4a8b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3611,6 +3611,7 @@ dependencies = [
"futures",
"gpui",
"image",
+ "libc",
"mdns-sd",
"objc",
"quinn",
diff --git a/Cargo.toml b/Cargo.toml
index 174c016..a5dfcff 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -51,6 +51,10 @@ raw-window-handle = { version = "0.6", optional = true }
# Already in the tree (through reqwest/native roots): the Keychain, for cloud sign-ins.
security-framework = "3"
+# Already in the tree: to start this computer's agent in a session of its own.
+[target.'cfg(unix)'.dependencies]
+libc = "0.2"
+
# The icon and name in the Windows executable (build.rs).
[target.'cfg(windows)'.build-dependencies]
winresource = "0.1"
diff --git a/agent/src/detect.rs b/agent/src/detect.rs
index 144d264..0500031 100644
--- a/agent/src/detect.rs
+++ b/agent/src/detect.rs
@@ -90,20 +90,21 @@ fn runnable(f: &std::path::Path) -> bool {
/// Asks an interactive login shell for its PATH, giving up after a few seconds.
#[cfg(unix)]
fn interactive_path() -> Option<String> {
- let mut child = Command::new(user_shell())
- .args(["-l", "-i", "-c", "printf '__OR_PATH=%s\\n' \"$PATH\""])
- .stdin(Stdio::null())
- .stdout(Stdio::piped())
- .stderr(Stdio::null())
- .spawn()
- .ok()?;
+ let mut cmd = Command::new(user_shell());
+ cmd.args(["-l", "-i", "-c", "printf '__OR_PATH=%s\\n' \"$PATH\""]).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::null());
+ // In a session of its own, without a terminal: an interactive shell takes
+ // over the terminal it finds (and stops its process group with SIGTTIN when
+ // that's not in front), which is the one Muxin was started from when the
+ // agent runs for this computer.
+ crate::sys::own_group(&mut cmd);
+ let mut child = cmd.spawn().ok()?;
let started = Instant::now();
loop {
match child.try_wait() {
Ok(Some(_)) => break,
Ok(None) if started.elapsed() < Duration::from_secs(6) => std::thread::sleep(Duration::from_millis(25)),
_ => {
- let _ = child.kill();
+ crate::sys::kill_tree(child.id(), false);
let _ = child.wait();
return None;
}
diff --git a/src/fleet/link.rs b/src/fleet/link.rs
index 265017e..f3f1f15 100644
--- a/src/fleet/link.rs
+++ b/src/fleet/link.rs
@@ -318,6 +318,15 @@ async fn connect_ssh(spec: &MachineSpec) -> Result<Arc<Link>, String> {
let mut cmd = if spec.is_local() {
let mut c = Command::new(local_agent());
c.arg("link");
+ // Without the terminal Muxin may have been started from: nothing the
+ // agent runs (an interactive shell, say) can take it over and stop us.
+ #[cfg(unix)]
+ unsafe {
+ c.pre_exec(|| {
+ libc::setsid();
+ Ok(())
+ });
+ }
c
} else {
let mut c = Command::new("ssh");