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:
@@ -16,9 +16,11 @@ import {
|
||||
makePreviewCard,
|
||||
updatePreviewCard,
|
||||
loadPreviewInto,
|
||||
disposeCard,
|
||||
fetchLiveSessions,
|
||||
fetchOrphanSessions,
|
||||
fetchOrphanPreview,
|
||||
countIdleOrphans,
|
||||
renderPreview,
|
||||
orphanAsLive,
|
||||
killOrphanReq,
|
||||
@@ -82,7 +84,16 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
|
||||
const cards = new Map<string, PreviewCard>()
|
||||
const orphanCards = new Map<string, PreviewCard>()
|
||||
/** Ids whose preview has actually RENDERED. Latching on "card exists" instead
|
||||
* left a card that lost its one request (429, or a 404 racing the list) blank
|
||||
* forever, because nothing ever asked again. */
|
||||
const orphanPreviewed = new Set<string>()
|
||||
let timer: ReturnType<typeof setInterval> | null = null
|
||||
/** Bumped by every teardown and every new refresh. An in-flight refresh compares
|
||||
* it after each await and bails if it is stale — otherwise a slow poll could
|
||||
* resurrect cards for sessions already killed, or re-mount them into a root that
|
||||
* setVisible(false) has just cleared. */
|
||||
let generation = 0
|
||||
|
||||
const CLEANUP_IDLE_DAYS = 7
|
||||
/** Thumbnails are xterm instances; a host with dozens of stale tmux sessions
|
||||
@@ -130,8 +141,7 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
}
|
||||
for (const [id, card] of cards) {
|
||||
if (!seen.has(id)) {
|
||||
card.term.dispose()
|
||||
card.el.remove()
|
||||
disposeCard(card)
|
||||
cards.delete(id)
|
||||
}
|
||||
}
|
||||
@@ -139,9 +149,12 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
await refreshOrphans()
|
||||
}
|
||||
|
||||
/** Kill one orphan, then refresh. */
|
||||
/** Kill one orphan, then refresh. A refusal (403 from the Origin guard behind a
|
||||
* proxy, 404 from a lost race) must be visible — silently doing nothing reads as
|
||||
* a broken button. */
|
||||
async function killOrphan(id: string): Promise<void> {
|
||||
await killOrphanReq(id)
|
||||
const ok = await killOrphanReq(id)
|
||||
if (!ok) orphanSub.textContent = `Could not end session ${id.slice(0, 8)} — it may already be gone.`
|
||||
void refresh()
|
||||
}
|
||||
|
||||
@@ -158,15 +171,19 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
}
|
||||
|
||||
async function refreshOrphans(): Promise<void> {
|
||||
const myGen = generation
|
||||
const orphans = await fetchOrphanSessions()
|
||||
if (myGen !== generation) return // torn down or superseded while fetching
|
||||
|
||||
// null = the request failed. Leave the section exactly as it is: reporting
|
||||
// "none" would tear down every mounted xterm and rebuild it on recovery.
|
||||
if (orphans === null) return
|
||||
|
||||
if (orphans.length === 0) {
|
||||
orphanSection.style.display = 'none'
|
||||
for (const card of orphanCards.values()) {
|
||||
card.term.dispose()
|
||||
card.el.remove()
|
||||
}
|
||||
for (const card of orphanCards.values()) disposeCard(card)
|
||||
orphanCards.clear()
|
||||
orphanPreviewed.clear()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -185,7 +202,6 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
for (const o of shown) {
|
||||
seen.add(o.id)
|
||||
let card = orphanCards.get(o.id)
|
||||
const isNew = card === undefined
|
||||
if (card === undefined) {
|
||||
card = makeOrphanCard(o)
|
||||
orphanCards.set(o.id, card)
|
||||
@@ -199,18 +215,21 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
// capture-pane, and re-capturing every card every tick would mean dozens of
|
||||
// processes a minute for screens that are, by definition, not being driven
|
||||
// from here. Fire-and-forget so a slow capture never stalls the next card.
|
||||
if (isNew) {
|
||||
if (!orphanPreviewed.has(o.id)) {
|
||||
const target = card
|
||||
void fetchOrphanPreview(o.id).then((p) => {
|
||||
if (p) renderPreview(target, p, THUMB_W, THUMB_MAX_H)
|
||||
const targetId = o.id
|
||||
void fetchOrphanPreview(targetId).then((p) => {
|
||||
if (p === null || myGen !== generation) return
|
||||
renderPreview(target, p, THUMB_W, THUMB_MAX_H)
|
||||
orphanPreviewed.add(targetId) // only now stop asking
|
||||
})
|
||||
}
|
||||
}
|
||||
for (const [id, card] of orphanCards) {
|
||||
if (!seen.has(id)) {
|
||||
card.term.dispose()
|
||||
card.el.remove()
|
||||
disposeCard(card)
|
||||
orphanCards.delete(id)
|
||||
orphanPreviewed.delete(id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,17 +242,33 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
}
|
||||
|
||||
cleanupBtn.addEventListener('click', () => {
|
||||
const n = orphanCards.size
|
||||
if (
|
||||
!window.confirm(
|
||||
`Kill unattached recoverable sessions with no activity for ${CLEANUP_IDLE_DAYS} days?\n\n` +
|
||||
`This ends their shells. Sessions someone is attached to are never touched. ` +
|
||||
`(${n} recoverable session${n === 1 ? '' : 's'} listed.)`,
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
void cleanupOrphansReq(CLEANUP_IDLE_DAYS).then(() => refresh())
|
||||
// Ask the SERVER how many it would end. orphanCards is capped at
|
||||
// MAX_ORPHAN_CARDS while the cleanup matches across every session the server
|
||||
// enumerates, so counting cards would understate the blast radius — on a host
|
||||
// with 69 orphans the dialog would have said 24.
|
||||
void countIdleOrphans(CLEANUP_IDLE_DAYS).then((n) => {
|
||||
if (n === null) {
|
||||
orphanSub.textContent = 'Could not check how many sessions are idle — not cleaning up.'
|
||||
return
|
||||
}
|
||||
if (n === 0) {
|
||||
orphanSub.textContent = `Nothing to clean up: no unattended session has been idle ${CLEANUP_IDLE_DAYS} days.`
|
||||
return
|
||||
}
|
||||
if (
|
||||
!window.confirm(
|
||||
`End ${n} session${n === 1 ? '' : 's'}?\n\n` +
|
||||
`These are recoverable sessions with no tmux activity for ${CLEANUP_IDLE_DAYS} days. ` +
|
||||
`Their shells will be terminated. Sessions someone is attached to are never touched.`,
|
||||
)
|
||||
) {
|
||||
return
|
||||
}
|
||||
void cleanupOrphansReq(CLEANUP_IDLE_DAYS).then((ids) => {
|
||||
if (ids.length === 0) orphanSub.textContent = 'Cleanup ended no sessions.'
|
||||
void refresh()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return {
|
||||
@@ -251,11 +286,13 @@ export function mountLauncher(host: HTMLElement, hooks: LauncherHooks): Launcher
|
||||
clearInterval(timer)
|
||||
timer = null
|
||||
}
|
||||
for (const card of cards.values()) card.term.dispose()
|
||||
for (const card of cards.values()) disposeCard(card)
|
||||
cards.clear()
|
||||
grid.replaceChildren()
|
||||
for (const card of orphanCards.values()) card.term.dispose()
|
||||
generation += 1 // invalidate any refresh still in flight
|
||||
for (const card of orphanCards.values()) disposeCard(card)
|
||||
orphanCards.clear()
|
||||
orphanPreviewed.clear()
|
||||
orphanGrid.replaceChildren()
|
||||
orphanSection.style.display = 'none'
|
||||
}
|
||||
|
||||
@@ -67,6 +67,10 @@ export function sessionName(s: LiveSessionInfo): string {
|
||||
export interface PreviewCard {
|
||||
el: HTMLElement
|
||||
term: Terminal
|
||||
/** Set by disposeCard. A preview is fetched fire-and-forget, so a card can be
|
||||
* torn down while its request is in flight, and xterm throws "Object has been
|
||||
* disposed" on a disposed terminal. Checked by renderPreview. */
|
||||
disposed: boolean
|
||||
inner: HTMLElement
|
||||
thumb: HTMLElement
|
||||
watch: HTMLElement
|
||||
@@ -125,7 +129,7 @@ export function makePreviewCard(s: LiveSessionInfo, opts: MakeCardOpts): Preview
|
||||
term.open(inner)
|
||||
|
||||
cardEl.append(head, thumb, meta, actions)
|
||||
return { el: cardEl, term, inner, thumb, watch, status, meta }
|
||||
return { el: cardEl, term, inner, thumb, watch, status, meta, disposed: false }
|
||||
}
|
||||
|
||||
/** Re-apply a session's live status/watch/meta fields to its card in place. */
|
||||
@@ -136,6 +140,14 @@ export function updatePreviewCard(card: PreviewCard, s: LiveSessionInfo): void {
|
||||
card.watch.textContent = `👁 ${s.clientCount}`
|
||||
}
|
||||
|
||||
/** Tear a card down: mark it, dispose its terminal, detach its element. One place,
|
||||
* so no caller can dispose the terminal and forget to set the flag. */
|
||||
export function disposeCard(card: PreviewCard): void {
|
||||
card.disposed = true
|
||||
card.term.dispose()
|
||||
card.el.remove()
|
||||
}
|
||||
|
||||
/** Scale the rendered xterm down so the full screen fits `thumbW` px wide. */
|
||||
export function fitThumb(card: PreviewCard, thumbW: number, thumbMaxH: number): void {
|
||||
const w = card.inner.offsetWidth
|
||||
@@ -162,6 +174,11 @@ export async function fetchPreview(id: string): Promise<SessionPreview | null> {
|
||||
* orphan preview omits them because its dimensions came with the list, and
|
||||
* resizing to Math.max(2, 0) would collapse the thumbnail to 2×2. */
|
||||
export function renderPreview(card: PreviewCard, p: SessionPreview, thumbW: number, thumbMaxH: number): void {
|
||||
// A preview is fetched fire-and-forget, so the card can be torn down while its
|
||||
// request is in flight, and xterm throws "Object has been disposed". An explicit
|
||||
// flag, not el.isConnected: a card that has not been appended YET is alive, and
|
||||
// conflating the two would drop the first render.
|
||||
if (card.disposed) return
|
||||
if (p.cols > 0 && p.rows > 0 && (card.term.cols !== p.cols || card.term.rows !== p.rows)) {
|
||||
card.term.resize(Math.max(2, p.cols), Math.max(2, p.rows))
|
||||
}
|
||||
@@ -196,14 +213,32 @@ export async function fetchLiveSessions(): Promise<LiveSessionInfo[]> {
|
||||
card machinery above through orphanAsLive, so there is one thumbnail
|
||||
implementation rather than two that drift. */
|
||||
|
||||
/** Fetch untracked tmux sessions, returning [] on failure. */
|
||||
export async function fetchOrphanSessions(): Promise<OrphanSessionInfo[]> {
|
||||
/**
|
||||
* Fetch untracked tmux sessions. Returns null on FAILURE, distinct from [] meaning
|
||||
* "there are none" — collapsing the two would let a 429, a 500 or a dropped request
|
||||
* read as "all recovered sessions are gone", tearing the grid down and re-mounting
|
||||
* every xterm on the next success.
|
||||
*/
|
||||
export async function fetchOrphanSessions(): Promise<OrphanSessionInfo[] | null> {
|
||||
try {
|
||||
const res = await fetch('/orphan-sessions')
|
||||
if (!res.ok) return null
|
||||
const data: unknown = await res.json()
|
||||
return Array.isArray(data) ? (data as OrphanSessionInfo[]) : []
|
||||
return Array.isArray(data) ? (data as OrphanSessionInfo[]) : null
|
||||
} catch {
|
||||
return []
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** How many sessions the bulk cleanup would actually end. null on failure. */
|
||||
export async function countIdleOrphans(days: number): Promise<number | null> {
|
||||
try {
|
||||
const res = await fetch(`/orphan-sessions/count-idle?idleDays=${encodeURIComponent(String(days))}`)
|
||||
if (!res.ok) return null
|
||||
const body = (await res.json()) as { count?: unknown }
|
||||
return typeof body.count === 'number' ? body.count : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user