fix(desktop): hold the pool-stop fence through SSH teardown
Process-less SSH entries finish child exit immediately. Keep inFlight and the bootstrap drain up until keepalive teardown completes, and stop reading main.ts from tests. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -12737,18 +12737,19 @@ async function runPoolBackendStart(profile, entry, opts: { forceLocal?: boolean;
|
||||
const poolStopper = createPoolStopper({
|
||||
pool: backendPool,
|
||||
stopChild: child => stopBackendChild(child),
|
||||
waitForExit: child => waitForBackendExit(child)
|
||||
waitForExit: child => waitForBackendExit(child),
|
||||
// Remote / SSH-isolated pool entries keep `process: null`. Child exit is
|
||||
// immediate; hold the same in-flight fence through bootstrap drain + SSH
|
||||
// teardown so a reconnect cannot publish into a dying scope (#106935).
|
||||
afterStop: async key => {
|
||||
await sshBootstrapCoordinator.cancelAndWait(key, () => teardownSshConnection(key))
|
||||
}
|
||||
})
|
||||
|
||||
async function stopPoolBackend(profile: string) {
|
||||
const entry = backendPool.get(profile)
|
||||
await poolStopper.stop(profile)
|
||||
releaseLocalBackendSlot(entry)
|
||||
// Remote / SSH-isolated pool entries keep `process: null`. Evicting the
|
||||
// descriptor alone leaves sshConnections + the keep-alive WS armed, so
|
||||
// idle-reaper / LRU retirement would pin the tunnel indefinitely (#106935).
|
||||
await sshBootstrapCoordinator.cancelAndWait(profile)
|
||||
await teardownSshConnection(profile)
|
||||
}
|
||||
|
||||
async function teardownPoolBackendAndWait(profile) {
|
||||
|
||||
@@ -146,6 +146,57 @@ test('stopAll joins a stop whose pool entry was already evicted', async () => {
|
||||
assert.equal(stopper.hasPending(), false)
|
||||
})
|
||||
|
||||
test('afterStop holds inFlight until extra teardown finishes (process-less SSH)', async () => {
|
||||
const pool = new Map<string, PoolStopEntry>()
|
||||
const events: string[] = []
|
||||
let releaseAfter: (() => void) | undefined
|
||||
const afterGate = new Promise<void>(resolve => {
|
||||
releaseAfter = resolve
|
||||
})
|
||||
const stopper = createPoolStopper({
|
||||
pool,
|
||||
stopChild: () => {
|
||||
events.push('stop')
|
||||
},
|
||||
waitForExit: async () => {
|
||||
events.push('exit')
|
||||
},
|
||||
afterStop: async () => {
|
||||
events.push('after-start')
|
||||
await afterGate
|
||||
events.push('after-done')
|
||||
}
|
||||
})
|
||||
|
||||
pool.set('ssh', { process: null })
|
||||
const stop = stopper.stop('ssh')
|
||||
await Promise.resolve()
|
||||
await Promise.resolve()
|
||||
|
||||
assert.equal(stopper.inFlight('ssh'), stop)
|
||||
assert.deepEqual(events, ['stop', 'exit', 'after-start'])
|
||||
|
||||
let spawned = false
|
||||
const respawn = (async () => {
|
||||
const dying = stopper.inFlight('ssh')
|
||||
|
||||
if (dying) {
|
||||
await dying
|
||||
}
|
||||
|
||||
spawned = true
|
||||
})()
|
||||
|
||||
await Promise.resolve()
|
||||
assert.equal(spawned, false, 'reconnect must wait for SSH teardown, not just child exit')
|
||||
releaseAfter?.()
|
||||
await stop
|
||||
await respawn
|
||||
assert.equal(spawned, true)
|
||||
assert.deepEqual(events, ['stop', 'exit', 'after-start', 'after-done'])
|
||||
assert.equal(stopper.inFlight('ssh'), undefined)
|
||||
})
|
||||
|
||||
test('a respawn can await the in-flight stop before reusing the key', async () => {
|
||||
const { addChild, exitResolvers, stopper } = harness()
|
||||
const child = addChild('selena')
|
||||
|
||||
@@ -33,6 +33,11 @@ export interface PoolStopperDeps {
|
||||
stopChild: (child: unknown) => void
|
||||
/** Bounded wait: resolves when the child exits, escalating to SIGKILL. */
|
||||
waitForExit: (child: unknown) => Promise<void>
|
||||
/**
|
||||
* Extra per-key work that must finish before a replacement may spawn.
|
||||
* Held on the same in-flight promise as child exit (SSH teardown, etc.).
|
||||
*/
|
||||
afterStop?: (key: string, entry: PoolStopEntry) => Promise<void>
|
||||
}
|
||||
|
||||
export interface PoolStopper {
|
||||
@@ -69,6 +74,9 @@ export function createPoolStopper(deps: PoolStopperDeps): PoolStopper {
|
||||
const stopping = (async () => {
|
||||
deps.stopChild(entry.process)
|
||||
await deps.waitForExit(entry.process)
|
||||
if (deps.afterStop) {
|
||||
await deps.afterStop(key, entry)
|
||||
}
|
||||
})().finally(() => {
|
||||
stops.delete(key)
|
||||
})
|
||||
|
||||
@@ -208,6 +208,40 @@ test('cancelAndWait force-cleans pending resources before awaiting rollback', as
|
||||
assert.equal(cleaned, 1)
|
||||
})
|
||||
|
||||
test('cancelAndWait keeps the drain up through afterCancel teardown', async () => {
|
||||
const coordinator = createBootstrapCoordinator()
|
||||
const events: string[] = []
|
||||
let releaseAfter: (() => void) | undefined
|
||||
const afterGate = new Promise<void>(resolve => {
|
||||
releaseAfter = resolve
|
||||
})
|
||||
let teardownStarted: (() => void) | undefined
|
||||
const started = new Promise<void>(resolve => {
|
||||
teardownStarted = resolve
|
||||
})
|
||||
|
||||
const drain = coordinator.cancelAndWait('scope', async () => {
|
||||
events.push('teardown-start')
|
||||
teardownStarted?.()
|
||||
await afterGate
|
||||
events.push('teardown-done')
|
||||
})
|
||||
|
||||
await started
|
||||
const next = coordinator.start('scope', 'new', async () => {
|
||||
events.push('new-start')
|
||||
|
||||
return 'new'
|
||||
})
|
||||
|
||||
await Promise.resolve()
|
||||
assert.deepEqual(events, ['teardown-start'])
|
||||
releaseAfter?.()
|
||||
await drain
|
||||
assert.equal(await next, 'new')
|
||||
assert.deepEqual(events, ['teardown-start', 'teardown-done', 'new-start'])
|
||||
})
|
||||
|
||||
test('a generation started during cancelAndWait cannot run before the drain completes', async () => {
|
||||
const coordinator = createBootstrapCoordinator()
|
||||
const oldGate = deferred()
|
||||
|
||||
@@ -93,7 +93,7 @@ function createBootstrapCoordinator() {
|
||||
pending.get(scope)?.controller.abort()
|
||||
}
|
||||
|
||||
async function cancelAndWait(scope) {
|
||||
async function cancelAndWait(scope, afterCancel?: () => Promise<void>) {
|
||||
let release
|
||||
|
||||
const barrier = new Promise<void>(resolve => {
|
||||
@@ -114,6 +114,11 @@ function createBootstrapCoordinator() {
|
||||
// drain barrier still prevents stale resurrection.
|
||||
await Promise.allSettled(entries.flatMap(entry => [...entry.forceCleanups]).map(cleanup => cleanup()))
|
||||
await Promise.allSettled(entries.map(entry => entry.promise))
|
||||
// Keep the drain up through caller teardown (SSH keepalive / tunnel)
|
||||
// so a replacement start() cannot publish before the old scope is gone.
|
||||
if (afterCancel) {
|
||||
await afterCancel()
|
||||
}
|
||||
} finally {
|
||||
if (drains.get(scope) === barrier) {
|
||||
drains.delete(scope)
|
||||
|
||||
@@ -3,16 +3,12 @@
|
||||
* published SSH-isolated backend. Idle-exit (#101626) treats accepted WS as
|
||||
* ownership liveness; renderer sockets can drop while sshConnections still owns
|
||||
* the scope. Sticky artifacts (nonce / token file / lockfile) are NOT liveness.
|
||||
*
|
||||
* Wiring through main.ts is asserted via pool-stop / bootstrap-coordinator
|
||||
* behavior tests — AGENTS.md forbids reading `.ts` source from tests.
|
||||
*/
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const here = path.dirname(fileURLToPath(import.meta.url))
|
||||
const mainSource = fs.readFileSync(path.join(here, 'main.ts'), 'utf8').replace(/\r\n/g, '\n')
|
||||
|
||||
function makeFakeWs(): { FakeWs: new (url: string) => any; instances: any[] } {
|
||||
const instances: any[] = []
|
||||
|
||||
@@ -44,73 +40,6 @@ function makeFakeWs(): { FakeWs: new (url: string) => any; instances: any[] } {
|
||||
return { FakeWs, instances }
|
||||
}
|
||||
|
||||
describe('main.ts wiring for SSH-isolated keep-alive WS (#106935)', () => {
|
||||
it('starts a keep-alive WebSocket after publishing sshConnections', () => {
|
||||
const publishStart = mainSource.indexOf('publish: () => {')
|
||||
expect(publishStart).toBeGreaterThan(-1)
|
||||
|
||||
const publishSlice = mainSource.slice(publishStart, publishStart + 4_000)
|
||||
const setIdx = publishSlice.indexOf('sshConnections.set(scope, {')
|
||||
expect(setIdx).toBeGreaterThan(-1)
|
||||
|
||||
const afterSet = publishSlice.slice(setIdx)
|
||||
expect(afterSet).toMatch(/sshIsolatedKeepalives\.start\(\s*scope/)
|
||||
expect(afterSet).toMatch(/baseUrl:\s*result\.baseUrl/)
|
||||
expect(afterSet).toMatch(/token:\s*result\.token/)
|
||||
})
|
||||
|
||||
it('stops the keep-alive in teardownSshConnection so sockets cannot leak', () => {
|
||||
const fnStart = mainSource.indexOf('async function teardownSshConnection(')
|
||||
expect(fnStart).toBeGreaterThan(-1)
|
||||
|
||||
const nextFn = mainSource.indexOf('\nfunction activeSshTerminalTarget(', fnStart + 1)
|
||||
const body = mainSource.slice(fnStart, nextFn === -1 ? fnStart + 1_200 : nextFn)
|
||||
|
||||
expect(body).toContain('sshConnections.delete(scope)')
|
||||
expect(body).toMatch(/sshIsolatedKeepalives\.stop\(\s*scope/)
|
||||
})
|
||||
|
||||
it('stopPoolBackend tears down the matching SSH lifecycle (idle-reaper / LRU)', () => {
|
||||
const fnStart = mainSource.indexOf('async function stopPoolBackend(')
|
||||
expect(fnStart).toBeGreaterThan(-1)
|
||||
|
||||
const nextFn = mainSource.indexOf('\nasync function teardownPoolBackendAndWait(', fnStart + 1)
|
||||
const body = mainSource.slice(fnStart, nextFn === -1 ? fnStart + 400 : nextFn)
|
||||
|
||||
expect(body).toMatch(/sshBootstrapCoordinator\.cancelAndWait\(\s*profile/)
|
||||
expect(body).toMatch(/teardownSshConnection\(\s*profile/)
|
||||
})
|
||||
|
||||
it('idle reaper retires descriptors only through stopPoolBackend', () => {
|
||||
const fnStart = mainSource.indexOf('function startPoolIdleReaper(')
|
||||
expect(fnStart).toBeGreaterThan(-1)
|
||||
|
||||
const nextFn = mainSource.indexOf('\nfunction releaseLocalBackendSlot(', fnStart + 1)
|
||||
const body = mainSource.slice(fnStart, nextFn === -1 ? fnStart + 800 : nextFn)
|
||||
|
||||
expect(body).toContain('stopPoolBackend(profile)')
|
||||
expect(body).not.toMatch(/backendPool\.delete/)
|
||||
expect(body).not.toMatch(/sshConnections\.delete/)
|
||||
})
|
||||
|
||||
it('imports the keep-alive registry from the electron helper (not inline in main)', () => {
|
||||
expect(mainSource).toMatch(/createSshIsolatedKeepaliveRegistry/)
|
||||
expect(mainSource).toMatch(/from '\.\/ssh-isolated-keepalive'/)
|
||||
})
|
||||
|
||||
it('starts keep-alive with the published scope as-is, including empty v1/global primary', () => {
|
||||
const publishStart = mainSource.indexOf('publish: () => {')
|
||||
const publishSlice = mainSource.slice(publishStart, publishStart + 4_000)
|
||||
const afterSet = publishSlice.slice(publishSlice.indexOf('sshConnections.set(scope, {'))
|
||||
|
||||
// sshScopeKey(null) is '' for the settings/registry primary. Do not
|
||||
// truthiness-guard the scope before arming — that would skip the live WS.
|
||||
expect(afterSet).toMatch(/sshIsolatedKeepalives\.start\(\s*scope,\s*\{\s*baseUrl:\s*result\.baseUrl,\s*token:\s*result\.token\s*\}\)/)
|
||||
expect(afterSet).not.toMatch(/if\s*\(\s*scope\s*\)/)
|
||||
expect(afterSet).not.toMatch(/scope\s*\|\|/)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ssh-isolated keep-alive registry (#106935)', () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
|
||||
Reference in New Issue
Block a user