Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

Fix a process leak when the daemon is down

Lars Taliand3ed75493e75
diff --git a/Cargo.lock b/Cargo.lock
index f4546f6..10d9e04 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3608,7 +3608,7 @@ dependencies = [
[[package]]
name = "tincan"
-version = "0.7.0"
+version = "0.7.1"
dependencies = [
"anyhow",
"blake3",
diff --git a/Cargo.toml b/Cargo.toml
index 98134a3..af6eb49 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "tincan"
-version = "0.7.0"
+version = "0.7.1"
edition = "2024"
description = "Team directory and message bus for coding agents across machines"
license = "MIT OR Apache-2.0"
diff --git a/install.sh b/install.sh
index c0c49e6..97d4b10 100755
--- a/install.sh
+++ b/install.sh
@@ -64,7 +64,8 @@ esac
# Hand over to tincan: it asks a few questions (name, invite code, which agents) in your terminal. Under
# `curl | sh` our stdin is the script, so read answers from the terminal itself when there is one.
-if [ -z "${TINCAN_NO_PROMPT:-}" ] && { : </dev/tty; } 2>/dev/null; then
+# (In a subshell: in dash, Ubuntu's sh, a failed redirection on `:` would end the whole script.)
+if [ -z "${TINCAN_NO_PROMPT:-}" ] && (exec </dev/tty) 2>/dev/null; then
case "${1:-}" in
"") exec "$bin_dir/tincan" </dev/tty ;;
-*|init|join|setup) exec "$bin_dir/tincan" "$@" </dev/tty ;;
diff --git a/src/client.rs b/src/client.rs
index e9cc95d..b1e268c 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -138,16 +138,24 @@ impl Link {
}
async fn reconnect(link: Weak<Link>) {
+ // Backs off: every agent session runs one of these, and they must not storm a daemon that can't start.
+ let mut wait = Duration::from_secs(1);
loop {
- tokio::time::sleep(Duration::from_secs(1)).await;
+ tokio::time::sleep(wait).await;
let Some(link) = link.upgrade() else { return };
match link.connect().await {
Ok(_) => return,
Err(e) => eprintln!("tincan: reconnecting to daemon: {e:#}"),
}
+ wait = (wait * 2).min(Duration::from_secs(30));
}
}
+/// The daemon that holds the lock, if it is alive (it may still be starting).
+pub fn running_daemon() -> Option<u32> {
+ std::fs::read_to_string(paths::lock()).ok()?.trim().parse().ok().filter(|p| crate::util::pid_alive(*p))
+}
+
async fn connect_or_start(start: bool) -> Result<UnixStream> {
let sock = paths::socket();
if let Ok(s) = UnixStream::connect(&sock).await {
@@ -156,12 +164,17 @@ async fn connect_or_start(start: bool) -> Result<UnixStream> {
if !start {
bail!("the tincan daemon isn't running");
}
- // With the background service installed, the service starts it, so there is only ever its daemon.
- let by_service = crate::setup::service_installed() && std::env::var_os("TINCAN_NO_SERVICE").is_none() && crate::setup::start_service();
- if !by_service {
- start_daemon()?;
+ // One is running (it may be starting or restarting): wait for it rather than start another.
+ if running_daemon().is_none() {
+ // With the background service installed, the service starts it, so there is only ever its daemon.
+ let by_service = crate::setup::service_installed()
+ && std::env::var_os("TINCAN_NO_SERVICE").is_none()
+ && crate::setup::start_service();
+ if !by_service {
+ start_daemon()?;
+ }
}
- for _ in 0..60 {
+ for _ in 0..100 {
tokio::time::sleep(Duration::from_millis(50)).await;
if let Ok(s) = UnixStream::connect(&sock).await {
return Ok(s);
@@ -174,12 +187,14 @@ async fn connect_or_start(start: bool) -> Result<UnixStream> {
fn start_daemon() -> Result<()> {
paths::ensure_home()?;
let log = std::fs::OpenOptions::new().create(true).append(true).open(paths::log())?;
- Command::new(std::env::current_exe()?)
+ let mut child = Command::new(std::env::current_exe()?)
.arg("daemon")
.stdin(Stdio::null())
.stdout(log.try_clone()?)
.stderr(log)
.process_group(0)
.spawn()?;
+ // Reaped when it exits, so a long-lived client (`tincan mcp`) never collects zombies.
+ std::thread::spawn(move || child.wait());
Ok(())
}
diff --git a/src/main.rs b/src/main.rs
index 573d0d3..9f95502 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -409,7 +409,7 @@ async fn cli(cmd: Cmd) -> Result<()> {
/// The running daemon's pid, from its lock file.
fn daemon_pid() -> Option<u32> {
- std::fs::read_to_string(paths::lock()).ok()?.trim().parse().ok().filter(|p| util::pid_alive(*p))
+ client::running_daemon()
}
/// Waits (briefly) for a daemon that is restarting to exit, so the next request reaches the new one. Call it
diff --git a/tests/e2e/hooks.py b/tests/e2e/hooks.py
index 29c7201..285d6fe 100644
--- a/tests/e2e/hooks.py
+++ b/tests/e2e/hooks.py
@@ -184,6 +184,20 @@ out = run("inbox")
check("…and its reply (run from that session) comes from it", "── alex/old-proj-claude" in out and "loud and clear" in out, r.stdout + r.stderr + out)
sleeper.kill()
+# An agent's MCP server outlives daemon restarts without collecting zombies or storming restarts.
+mcp = subprocess.Popen([BIN, "mcp"], stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env=env)
+time.sleep(4)
+for _ in range(3):
+ run("stop")
+ time.sleep(3)
+time.sleep(3)
+zombies = subprocess.run(["ps", "-axo", "ppid=,stat="], capture_output=True, text=True).stdout.split("\n")
+mine = [z for z in zombies if z.split() and z.split()[0] == str(mcp.pid) and "Z" in z.split()[1]]
+check("an MCP server collects no zombies across daemon restarts", not mine, f"{len(mine)} zombies")
+starts = open(os.path.join(env["TINCAN_HOME"], "daemon.log")).read().count("already running")
+check("…and doesn't race to start daemons", starts <= 3, f"{starts} duplicate starts")
+mcp.kill()
+
# The daemon being down never breaks the agent.
run("stop")
bad = dict(env, TINCAN_HOME="/nonexistent/dir/that/cannot/exist")