fix(sessions): orphan cleanup was reading the wrong tmux clock

Adversarial review of the previous commit found ten defects. The critical one
would have destroyed exactly the work this feature exists to protect.

CRITICAL — `#{session_activity}` is a CLIENT clock, not an output clock.
It advances on attach and on keypresses, and never moves for a detached session
however much its shell is printing. Measured on tmux 3.6a: a detached session
running `while true; do echo tick; sleep 1; done` still reported
session_activity == session_created after 6 s, while `#{window_activity}` tracked
the output exactly. So "idle for 7 days" was really "nobody has typed for 7 days",
and `Clean up idle 7d+` would have killed long-running unattended sessions that
were busy working — the walk-away case the whole app is built around.
Measured against this host's 69 sessions: the old clock condemned 36, the new one
condemns 33, so three sessions doing real work were one click from deletion.
Now reads window_activity, with a test that fails if the format string regresses.

HIGH — no timeout on any tmux call. maxBuffer bounds a flood but nothing bounded
the wait, and a synchronous exec cannot be interrupted by the event loop, so a
tmux server stopped mid-syscall hung the whole server permanently. All five calls
(including the three that predate this feature) now carry one.

HIGH — listSessions and capturePane were synchronous on a POLLED path. Measured
73 ms and 58 ms per call on this host; every home screen on every device refreshes
on a 5 s timer, and each of those was 73 ms with the event loop stopped, i.e.
73 ms in which no PTY byte reached any terminal anywhere. Both are async now, so
the manager's listOrphans/captureOrphan/killOrphansIdleSince are too. The server
is a byte-shuttle first: nothing polled may block it.

HIGH — GET /orphan-sessions had no rate limiter although, unlike every other read
route, it spawns a subprocess. It shares the preview bucket now.

HIGH — the cleanup confirmation counted CARDS, and the grid is capped at 24 while
the cleanup matches across every session the server enumerates. On this host it
would have said "24" and ended 33 shells. New GET /orphan-sessions/count-idle
answers the real question, and the dialog states that number, or refuses to
proceed if it cannot be determined.

MEDIUM — fetchOrphanSessions collapsed every failure into [], so one 429 or 500
read as "all recovered sessions are gone", tearing down every mounted xterm and
rebuilding it on the next success. It returns null for failure now, and the
refresh leaves the section untouched.

MEDIUM — the preview latch was "card was created", not "preview succeeded", so a
card that lost its single request stayed blank forever. Latches on success.

MEDIUM — overlapping refreshes. refresh() is entered from a 5 s timer, from
killOrphan and from cleanup, with awaits inside and no guard, so a slow poll could
resurrect cards for sessions already killed or re-mount them into a root that
setVisible(false) had just cleared. A generation counter, bumped on teardown and
checked after every await, invalidates stale runs.

MEDIUM — renderPreview could write to a disposed xterm (it throws "Object has
been disposed"). PreviewCard carries an explicit `disposed` flag set by a new
disposeCard() helper, so no caller can dispose a terminal and forget the flag.
Deliberately NOT el.isConnected: a card not yet appended is alive, and two
existing preview-grid tests correctly caught that conflation.

LOW — kill and cleanup discarded their results, so a refusal (403 behind a proxy
whose Origin differs, 404 on a lost race) looked like a dead button. Both report.

Tests: 2209 unit, 27 e2e, 8 orphan integration. Verified live against all 69 real
sessions with none harmed; the window_activity fix confirmed on an isolated tmux
socket (-L) so the developer's sessions were never involved.
This commit is contained in:
Yaojia Wang
2026-07-30 11:28:39 +02:00
parent 4892fa7b49
commit f6ef19ebf6
8 changed files with 330 additions and 120 deletions

View File

@@ -599,13 +599,34 @@ export function startServer(cfg: Config): { close(): Promise<void> } {
// it: an orphan supports none of the live-session operations (no replay, status,
// telemetry or inject queue), and the Android/iOS clients decode
// /live-sessions — adding a variant shape there would break them.
app.get('/orphan-sessions', (_req, res) => {
res.json(manager.listOrphans())
// Rate-limited even though it is read-only: unlike the other read routes this one
// spawns a subprocess, so an unbounded caller is a spawn loop against the host.
app.get('/orphan-sessions', async (req, res) => {
if (!orphanLimiter(req.socket.remoteAddress ?? '', Date.now())) {
res.status(429).end()
return
}
res.json(await manager.listOrphans())
})
// How many the bulk cleanup would kill, so the client can confirm with a real
// number instead of counting the cards it happens to be showing.
app.get('/orphan-sessions/count-idle', async (req, res) => {
if (!orphanLimiter(req.socket.remoteAddress ?? '', Date.now())) {
res.status(429).end()
return
}
const days = Number(req.query['idleDays'])
if (!Number.isFinite(days) || days < 1) {
res.status(400).json({ error: 'idleDays must be a number >= 1' })
return
}
res.json({ count: await manager.countOrphansIdleSince(Date.now() - days * 86_400_000) })
})
// Each call spawns a short-lived `tmux capture-pane`, so it gets the same per-IP
// bucket discipline as the other process-spawning routes.
app.get('/orphan-sessions/:id/preview', (req, res) => {
app.get('/orphan-sessions/:id/preview', async (req, res) => {
if (!orphanLimiter(req.socket.remoteAddress ?? '', Date.now())) {
res.status(429).end()
return
@@ -615,7 +636,7 @@ export function startServer(cfg: Config): { close(): Promise<void> } {
res.status(400).json({ error: 'invalid session id' })
return
}
const data = manager.captureOrphan(id)
const data = await manager.captureOrphan(id)
if (data === null) {
res.status(404).end()
return
@@ -627,14 +648,14 @@ export function startServer(cfg: Config): { close(): Promise<void> } {
// Bulk cleanup — kill UNATTACHED orphans idle longer than `idleDays`. Registered
// BEFORE /orphan-sessions/:id so it is not captured as an :id. `idleDays` is
// required and must be >= 1: there is no "delete everything" form of this route.
app.delete('/orphan-sessions', (req, res) => {
app.delete('/orphan-sessions', async (req, res) => {
if (!requireAllowedOrigin(req, res)) return
const days = Number(req.query['idleDays'])
if (!Number.isFinite(days) || days < 1) {
res.status(400).json({ error: 'idleDays must be a number >= 1' })
return
}
const killed = manager.killOrphansIdleSince(Date.now() - days * 86_400_000)
const killed = await manager.killOrphansIdleSince(Date.now() - days * 86_400_000)
res.json({ killed: killed.length, ids: killed })
})

View File

@@ -439,9 +439,10 @@ export function createSessionManager(
return hasSession(tmuxName(id));
}
function listOrphans(): OrphanSessionInfo[] {
async function listOrphans(): Promise<OrphanSessionInfo[]> {
if (!cfg.useTmux) return [];
return listSessions()
const all = await listSessions();
return all
.filter((t) => !sessions.has(t.id))
.map((t) => ({
id: t.id,
@@ -454,7 +455,7 @@ export function createSessionManager(
}
/** The orphan's current screen, or null. Read-only: never attaches. */
function captureOrphan(id: string): string | null {
async function captureOrphan(id: string): Promise<string | null> {
if (!isActionableOrphan(id)) return null;
return capturePane(tmuxName(id));
}
@@ -474,9 +475,9 @@ export function createSessionManager(
* someone is in there from a real terminal or another server process, and
* "this server does not track it" is not evidence that it is abandoned.
*/
function killOrphansIdleSince(cutoffMs: number): string[] {
async function killOrphansIdleSince(cutoffMs: number): Promise<string[]> {
const killed: string[] = [];
for (const o of listOrphans()) {
for (const o of await listOrphans()) {
if (o.attached) continue;
if (o.lastActivityAt >= cutoffMs) continue;
if (killOrphan(o.id)) killed.push(o.id);
@@ -484,6 +485,17 @@ export function createSessionManager(
return killed;
}
/** How many orphans `killOrphansIdleSince(cutoffMs)` would kill, without killing
* anything. The UI needs this to state a truthful number in its confirmation:
* the card grid is capped, so counting cards understates the blast radius. */
async function countOrphansIdleSince(cutoffMs: number): Promise<number> {
let n = 0;
for (const o of await listOrphans()) {
if (!o.attached && o.lastActivityAt < cutoffMs) n += 1;
}
return n;
}
return {
handleAttach,
get,
@@ -492,6 +504,7 @@ export function createSessionManager(
captureOrphan,
killOrphan,
killOrphansIdleSince,
countOrphansIdleSince,
killById,
handleHookEvent,
handleStatusLine,

View File

@@ -9,18 +9,38 @@
* treated as "false"/no-op).
*/
import { execFileSync } from 'node:child_process'
import { execFile, execFileSync } from 'node:child_process'
import { promisify } from 'node:util'
import { SESSION_ID_RE } from '../protocol.js'
const execFileAsync = promisify(execFile)
/** Upper bound on one `capture-pane` payload. A pane is one screen, so this is
* generous; it exists so a wedged tmux can never hand us unbounded output. */
const CAPTURE_MAX_BUFFER = 512 * 1024
/** Tab-separated, machine-readable. tmux reports the two clocks in unix SECONDS. */
/** Every tmux call is bounded. maxBuffer caps a flood but nothing caps the WAIT,
* and a tmux server stopped mid-syscall (SIGSTOP, a hung filesystem) would
* otherwise block forever — fatally so for the synchronous calls, which the event
* loop cannot interrupt. */
const TMUX_TIMEOUT_MS = 3000
/**
* Tab-separated, machine-readable. tmux reports both clocks in unix SECONDS.
*
* The activity clock is `window_activity`, NOT `session_activity`. That is not a
* detail: `session_activity` tracks CLIENT activity — it advances on attach and on
* keypresses — and never moves for a detached session no matter how much its shell
* is printing. Measured on tmux 3.6a with a detached session in a `while true; do
* echo tick; sleep 1; done` loop: after 6s, session_activity was still equal to
* session_created while window_activity had advanced. Using session_activity as the
* liveness signal would make the idle-cleanup kill precisely the long-running,
* unattended, actively-working sessions this whole feature exists to protect.
*/
const LIST_FORMAT = [
'#{session_name}',
'#{session_created}',
'#{session_activity}',
'#{window_activity}',
'#{session_attached}',
'#{window_width}',
'#{window_height}',
@@ -46,7 +66,7 @@ export interface TmuxSessionSummary {
/** Is the tmux binary available on PATH? */
export function tmuxAvailable(): boolean {
try {
execFileSync('tmux', ['-V'], { stdio: 'ignore' })
execFileSync('tmux', ['-V'], { stdio: 'ignore', timeout: TMUX_TIMEOUT_MS })
return true
} catch {
return false
@@ -61,7 +81,10 @@ export function tmuxName(sessionId: string): string {
/** Does a tmux session with this name already exist (e.g. after a restart)? */
export function hasSession(name: string): boolean {
try {
execFileSync('tmux', ['has-session', '-t', name], { stdio: 'ignore' })
execFileSync('tmux', ['has-session', '-t', name], {
stdio: 'ignore',
timeout: TMUX_TIMEOUT_MS,
})
return true
} catch {
return false
@@ -71,7 +94,10 @@ export function hasSession(name: string): boolean {
/** Kill a tmux session (ends the shell). No-op if it's already gone. */
export function killSession(name: string): void {
try {
execFileSync('tmux', ['kill-session', '-t', name], { stdio: 'ignore' })
execFileSync('tmux', ['kill-session', '-t', name], {
stdio: 'ignore',
timeout: TMUX_TIMEOUT_MS,
})
} catch {
// already gone — fine
}
@@ -133,16 +159,24 @@ export function parseSessionList(stdout: string): TmuxSessionSummary[] {
return rows.sort((a, b) => b.lastActivityAtMs - a.lastActivityAtMs)
}
/** Every `web_*` tmux session on the host, most recently active first. Never
* throws; [] when tmux is missing or no tmux server is running. */
export function listSessions(): TmuxSessionSummary[] {
/**
* Every `web_*` tmux session on the host, most recently active first. Never
* throws; [] when tmux is missing or no tmux server is running.
*
* ASYNC on purpose. This is polled — every device's home screen refreshes on a
* timer — and the spawn costs ~73 ms on a host with 69 sessions. Done
* synchronously that is 73 ms with the event loop stopped, i.e. 73 ms during which
* no PTY byte reaches any terminal on any device. The server is a byte-shuttle
* first; nothing on a polling path may block it.
*/
export async function listSessions(): Promise<TmuxSessionSummary[]> {
try {
const out = execFileSync('tmux', ['list-sessions', '-F', LIST_FORMAT], {
const { stdout } = await execFileAsync('tmux', ['list-sessions', '-F', LIST_FORMAT], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
maxBuffer: CAPTURE_MAX_BUFFER,
timeout: TMUX_TIMEOUT_MS,
})
return parseSessionList(out)
return parseSessionList(stdout)
} catch {
return []
}
@@ -156,13 +190,14 @@ export function listSessions(): TmuxSessionSummary[] {
* `attach-session`: attaching would make tmux resize the window to the new
* client's dimensions and SIGWINCH whatever is running inside it.
*/
export function capturePane(name: string): string | null {
export async function capturePane(name: string): Promise<string | null> {
try {
return execFileSync('tmux', ['capture-pane', '-p', '-e', '-t', name], {
const { stdout } = await execFileAsync('tmux', ['capture-pane', '-p', '-e', '-t', name], {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
maxBuffer: CAPTURE_MAX_BUFFER,
timeout: TMUX_TIMEOUT_MS,
})
return stdout
} catch {
return null
}

View File

@@ -478,15 +478,21 @@ export interface SessionManager {
reject an id that is not a UUID v4 or that the table already owns — a
tracked session must go through get()/killById so its PTY and table entry
stay in step. None of them adopt: nothing is attached, so a running TUI is
never resized. */
never resized.
The tmux-spawning ones are ASYNC: they sit on a polled path and each spawn
costs tens of milliseconds, which done synchronously is time the event loop
is stopped and no PTY byte reaches any terminal. */
/** Untracked `web_*` tmux sessions, most recently active first. */
listOrphans(): OrphanSessionInfo[];
listOrphans(): Promise<OrphanSessionInfo[]>;
/** An orphan's current screen via `tmux capture-pane` (no attach). */
captureOrphan(id: string): string | null;
captureOrphan(id: string): Promise<string | null>;
/** End an orphan's shell. False when it is not ours, gone, or tracked. */
killOrphan(id: string): boolean;
/** Kill every UNATTACHED orphan last active before `cutoffMs`; returns the ids. */
killOrphansIdleSince(cutoffMs: number): string[];
killOrphansIdleSince(cutoffMs: number): Promise<string[]>;
/** How many killOrphansIdleSince would kill — for a truthful confirmation. */
countOrphansIdleSince(cutoffMs: number): Promise<number>;
/** Kill a session by id (manage page): close its clients, kill the PTY, drop it.
* Returns true if a session was found and killed. */
killById(id: string): boolean;