fix: race condition marks newly spawned sessions as 'killed' before tmux initializes
Bug
Newly spawned sessions intermittently get marked as killed in metadata even though the tmux session is alive and running. Once killed is written to metadata, it becomes sticky — the session never recovers because enrichSessionWithRuntimeState short-circuits for terminal statuses.
Root Cause
A race condition between session spawn and lifecycle polling:
T=0ms ao spawn → runtime.create() → tmux new-session → writes status=spawning
T=50ms Lifecycle poll fires → list() → enrichSessionWithRuntimeState()
T=50ms runtime.isAlive() → tmux has-session → FAILS (tmux not fully initialized)
T=50ms status set to "killed" in memory + written to metadata
T=200ms tmux session is actually alive and running
T=∞ "killed" is sticky — line 879 short-circuits all future liveness checks
Two code paths that set killed:
-
enrichSessionWithRuntimeState()(session-manager.ts:890-894) — called duringlist():const alive = await plugins.runtime.isAlive(session.runtimeHandle); if (!alive) { session.status = "killed"; session.activity = "exited"; return; } -
determineStatus()(lifecycle-manager.ts:373-374) — called during lifecycle polling:const alive = await runtime.isAlive(session.runtimeHandle).catch(() => true); if (!alive) return "killed";
The sticky problem (session-manager.ts:879):
const TERMINAL_SESSION_STATUSES = new Set(["killed", "done", "merged", "terminated", "cleanup"]);
if (TERMINAL_SESSION_STATUSES.has(session.status)) {
session.activity = "exited";
return; // Never checks isAlive again
}
Reproduction
- Run
ao spawn <issue>— sometimes the session immediately showsstatus=killedin metadata tmux list-sessionsconfirms the tmux session is alive and attached- The session stays stuck as
killedforever
Suggested Fix
Two changes. Minimal, surgical.
Fix 1 — Skip liveness checks on spawning sessions (the real fix)
enrichSessionWithRuntimeState (session-manager.ts ~line 893):
// BEFORE
if (handleFromMetadata && session.runtimeHandle && plugins.runtime) {
const alive = await plugins.runtime.isAlive(session.runtimeHandle);
if (!alive) {
session.status = "killed";
...
}
}
// AFTER
if (
handleFromMetadata &&
session.runtimeHandle &&
plugins.runtime &&
session.status !== "spawning" // ← don't check sessions still being born
) {
const alive = await plugins.runtime.isAlive(session.runtimeHandle);
if (!alive) {
session.status = "killed";
...
}
}
determineStatus (lifecycle-manager.ts ~line 374):
// BEFORE
if (session.runtimeHandle) {
const alive = await runtime.isAlive(session.runtimeHandle).catch(() => true);
if (!alive) return "killed";
}
// AFTER
if (session.runtimeHandle && currentStatus !== "spawning") {
const alive = await runtime.isAlive(session.runtimeHandle).catch(() => true);
if (!alive) return "killed";
}
That's it. The spawning status already exists as a signal — just respect it. While a session is spawning, tmux is being created. Don't question its liveness until it's had a chance to finish.
Fix 2 (optional safety net) — Timestamp-based grace period
If you want belt-and-suspenders, add a createdAt timestamp to sessions and skip isAlive() for the first 30 seconds regardless of status:
const SPAWN_GRACE_PERIOD_MS = 30_000;
const age = Date.now() - new Date(session.createdAt).getTime();
if (age < SPAWN_GRACE_PERIOD_MS) return; // too young to check
But honestly, Fix 1 alone handles it. The spawning → active transition is already managed by the spawn flow. Once status moves past spawning, liveness checks are valid.
What NOT to fix
Don't touch the sticky gate (line 884). That gate is correct behavior — once a session is genuinely killed, it should stay killed. The problem isn't the gate, it's what walks through it. Fix the false kill upstream, not the gate downstream.
Impact
- Sessions appear dead on the dashboard immediately after spawn
- Users have to manually kill + restore to recover
- Wastes agent compute time if the session is killed by cleanup