* fix(runtime): retain watcher and PTY teardown ownership * fix(runtime): restore watchers after interrupted cleanup * fix(runtime): prevent stale watcher revival * test(runtime): cover watcher shutdown ownership * test(daemon): model physical PTY exit * fix(daemon): keep shutdown terminating when disposal cannot prove exit A rejecting host.dispose() (unreapable child past its exit deadline) left the shutdown RPC without its process.nextTick(shutdown) and skipped socket cleanup in shutdown(), stranding the daemon as an unreachable orphan after the stale-daemon replacement flow unlinks its socket. Log and continue: daemon exit reparents the child to init instead of blocking on it. * fix(runtime): keep local watching alive after an idle-kill deadline miss An idle child that outlived the exit deadline set shutdownRequested on the shared desktop supervisor, which has no retire-and-replace path — every later subscribe rejected supervisor_disposed and the roots were cached unwatchable, silently ending local file watching for the session. The idle path owns zero records, so there is no double-watch hazard; the zombie keeps its capacity reservation until physical exit and the next subscribe gets a fresh child. * fix(renderer): resync replayed paired-web file watches Transparent replay removed the implicit resync the old close-and-rebuild path provided: a replayed files.watch only reports changes from its own native setup, so changes during the reconnect gap were silently lost. Deliver a conservative overflow to consumers once the replayed watch is ready, matching the overflow-after-interruption contract everywhere else. * fix(runtime): address teardown review findings * fix(runtime): retry watches after teardown deadlines * Fix PTY descendant leaks on forced teardown * Fix jitter-sensitive terminal lifecycle test
138 lines
4.9 KiB
TypeScript
138 lines
4.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { connect, createServer, type Server, type Socket } from 'node:net'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { mkdirSync, mkdtempSync, rmSync } from 'node:fs'
|
|
import { DaemonServer } from './daemon-server'
|
|
import { DaemonClient } from './client'
|
|
import { healthCheckDaemon } from './daemon-health'
|
|
import { getDaemonSocketPath } from './daemon-spawner'
|
|
import type { ListSessionsResult } from './types'
|
|
import type { SubprocessHandle } from './session'
|
|
|
|
// Why: terminals were lost after app updates because a busy machine could
|
|
// time out the 3s startup health check against a daemon that was alive and
|
|
// owning sessions, and the unhealthy path killed it. The fix re-verifies
|
|
// with listSessions, which has far larger budgets (5s hello, 30s request).
|
|
// This test reproduces the production asymmetry against a REAL daemon by
|
|
// inserting a response delay that exceeds the health-check budget but fits
|
|
// the verification budgets, and asserts the guard's two inputs disagree the
|
|
// way the fix depends on.
|
|
const RESPONSE_DELAY_MS = 3_500
|
|
|
|
function createMockSubprocess(): SubprocessHandle {
|
|
let onExitCb: ((code: number) => void) | null = null
|
|
return {
|
|
pid: 55555,
|
|
getForegroundProcess: vi.fn(() => null),
|
|
write: vi.fn(),
|
|
resize: vi.fn(),
|
|
kill: vi.fn(() => setTimeout(() => onExitCb?.(0), 5)),
|
|
forceKill: vi.fn(() => setTimeout(() => onExitCb?.(137), 5)),
|
|
signal: vi.fn(),
|
|
onData: vi.fn(),
|
|
onExit: vi.fn((callback: (code: number) => void) => {
|
|
onExitCb = callback
|
|
}),
|
|
dispose: vi.fn()
|
|
}
|
|
}
|
|
|
|
/** Forwards client bytes to the daemon immediately, but delays every daemon
|
|
* response so each round-trip looks like a daemon under heavy load. */
|
|
function startDelayProxy(listenPath: string, upstreamPath: string): Server {
|
|
const proxy = createServer((clientSocket: Socket) => {
|
|
const upstream = connect(upstreamPath)
|
|
clientSocket.on('data', (chunk) => upstream.write(chunk))
|
|
upstream.on('data', (chunk) => {
|
|
setTimeout(() => {
|
|
if (!clientSocket.destroyed) {
|
|
clientSocket.write(chunk)
|
|
}
|
|
}, RESPONSE_DELAY_MS)
|
|
})
|
|
const teardown = (): void => {
|
|
clientSocket.destroy()
|
|
upstream.destroy()
|
|
}
|
|
clientSocket.on('close', teardown)
|
|
clientSocket.on('error', teardown)
|
|
upstream.on('close', () => {
|
|
setTimeout(teardown, RESPONSE_DELAY_MS)
|
|
})
|
|
upstream.on('error', teardown)
|
|
})
|
|
proxy.listen(listenPath)
|
|
return proxy
|
|
}
|
|
|
|
describe('slow daemon session verification', () => {
|
|
let dir: string
|
|
let daemonSocketPath: string
|
|
let proxySocketPath: string
|
|
let tokenPath: string
|
|
let server: DaemonServer
|
|
let proxy: Server
|
|
const clients: DaemonClient[] = []
|
|
|
|
beforeEach(() => {
|
|
// Why: real Unix socket paths have tight platform limits, especially on macOS.
|
|
dir = mkdtempSync(join(tmpdir(), 'ds-'))
|
|
mkdirSync(join(dir, 'daemon'), { recursive: true })
|
|
mkdirSync(join(dir, 'proxy'), { recursive: true })
|
|
daemonSocketPath = getDaemonSocketPath(join(dir, 'daemon'))
|
|
proxySocketPath = getDaemonSocketPath(join(dir, 'proxy'))
|
|
tokenPath = join(dir, 'daemon.token')
|
|
})
|
|
|
|
afterEach(async () => {
|
|
for (const client of clients.splice(0)) {
|
|
client.disconnect()
|
|
}
|
|
if (proxy) {
|
|
await new Promise<void>((resolve) => proxy.close(() => resolve()))
|
|
}
|
|
if (server) {
|
|
await server.shutdown()
|
|
}
|
|
rmSync(dir, { recursive: true, force: true })
|
|
})
|
|
|
|
it(
|
|
'fails the 3s health check against a slow daemon while listSessions still verifies its live session',
|
|
{ timeout: 60_000 },
|
|
async () => {
|
|
server = new DaemonServer({
|
|
socketPath: daemonSocketPath,
|
|
tokenPath,
|
|
spawnSubprocess: () => createMockSubprocess()
|
|
})
|
|
await server.start()
|
|
|
|
const directClient = new DaemonClient({ socketPath: daemonSocketPath, tokenPath })
|
|
clients.push(directClient)
|
|
await directClient.ensureConnected()
|
|
await directClient.request('createOrAttach', {
|
|
sessionId: 'wt-1@@live-session',
|
|
cols: 80,
|
|
rows: 24
|
|
})
|
|
|
|
proxy = startDelayProxy(proxySocketPath, daemonSocketPath)
|
|
|
|
// The exact pre-fix kill trigger: the daemon is alive but too slow for
|
|
// the health-check budget.
|
|
await expect(healthCheckDaemon(proxySocketPath, tokenPath)).resolves.toBe(false)
|
|
|
|
// The fix's re-verification against the SAME slow daemon: the larger
|
|
// client budgets absorb the latency and prove the session is alive.
|
|
const verificationClient = new DaemonClient({ socketPath: proxySocketPath, tokenPath })
|
|
clients.push(verificationClient)
|
|
await verificationClient.ensureConnected()
|
|
const result = await verificationClient.request<ListSessionsResult>('listSessions', undefined)
|
|
const liveSessionCount = result.sessions.filter((session) => session.isAlive).length
|
|
expect(liveSessionCount).toBe(1)
|
|
}
|
|
)
|
|
})
|