Skip to content

The agent that grows with you

fix(desktop): complete quit through one bounded teardown barrier

brooklyn!7b9808e43b41
diff --git a/apps/desktop/electron/connection-apply.test.ts b/apps/desktop/electron/connection-apply.test.ts
index 2fe3a57ce6e..e19e90b4a24 100644
--- a/apps/desktop/electron/connection-apply.test.ts
+++ b/apps/desktop/electron/connection-apply.test.ts
@@ -4,7 +4,6 @@ import {
applyConnectionChange,
commitConnectionFailure,
resolveTerminalConnection,
- sshQuitShouldBlock,
teardownSshState
} from './connection-apply'
@@ -92,48 +91,6 @@ describe('resolveTerminalConnection', () => {
})
})
-describe('sshQuitShouldBlock', () => {
- it('waits when connections exist and teardown has not finished', () => {
- expect(sshQuitShouldBlock({ teardownDone: false, connectionCount: 1, bootstrapPending: 0, inFlight: null })).toBe(
- true
- )
- })
-
- it('waits when bootstrap is still running', () => {
- expect(sshQuitShouldBlock({ teardownDone: false, connectionCount: 0, bootstrapPending: 1, inFlight: null })).toBe(
- true
- )
- })
-
- it('waits when the map is empty but a remote kill is already in flight', () => {
- expect(
- sshQuitShouldBlock({
- teardownDone: false,
- connectionCount: 0,
- bootstrapPending: 0,
- inFlight: Promise.resolve()
- })
- ).toBe(true)
- })
-
- it('does not block a second quit after teardown finished', () => {
- expect(
- sshQuitShouldBlock({
- teardownDone: true,
- connectionCount: 1,
- bootstrapPending: 1,
- inFlight: Promise.resolve()
- })
- ).toBe(false)
- })
-
- it('does not block quit when there is nothing to tear down', () => {
- expect(sshQuitShouldBlock({ teardownDone: false, connectionCount: 0, bootstrapPending: 0, inFlight: null })).toBe(
- false
- )
- })
-})
-
describe('teardownSshState', () => {
it('terminates the owned remote backend before closing its tunnel and SSH transport', async () => {
const events: string[] = []
diff --git a/apps/desktop/electron/connection-apply.ts b/apps/desktop/electron/connection-apply.ts
index ebb478b7098..03ad29741bb 100644
--- a/apps/desktop/electron/connection-apply.ts
+++ b/apps/desktop/electron/connection-apply.ts
@@ -61,21 +61,6 @@ async function resolveTerminalConnectionForSender(webContentsId, getTarget, ensu
)
}
-/** A second before-quit must still wait for an in-flight remote kill.
- *
- * teardownSshConnection deletes the sshConnections entry first, then
- * SSH-execs kill. backendShutdown's finally() calls app.quit() and
- * re-enters before-quit with an empty map. Without `inFlight`, Electron
- * exits while disconnect is running and the detached serve --isolated
- * stays at pid 1 (post-#95085 leftover on #91668: window X on Windows). */
-function sshQuitShouldBlock({ teardownDone, connectionCount, bootstrapPending, inFlight }) {
- if (teardownDone) {
- return false
- }
-
- return connectionCount > 0 || bootstrapPending > 0 || Boolean(inFlight)
-}
-
async function teardownSshState(state, { cleanupRemote }) {
// Remote process first, while the SSH channel can still exec kill.
// Then drop the local forward and close the transport. Each step is
@@ -106,6 +91,5 @@ export {
commitConnectionFailure,
resolveTerminalConnection,
resolveTerminalConnectionForSender,
- sshQuitShouldBlock,
teardownSshState
}
diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts
index 40036eedf33..deb5ed49fd4 100644
--- a/apps/desktop/electron/main.ts
+++ b/apps/desktop/electron/main.ts
@@ -39,7 +39,11 @@ import {
withRetry
} from './api-transport'
import { appIconCandidates, resolveAppIcon } from './app-icon'
-import { stopBackendChild as stopBackendChildImpl, stopBackendTreesForUpdate } from './backend-child'
+import {
+ stopBackendChild as stopBackendChildImpl,
+ stopBackendTreesForUpdate,
+ waitForBackendExit as waitForBackendExitImpl
+} from './backend-child'
import {
type BackendOutputTail,
claimDecision,
@@ -93,7 +97,7 @@ import {
import { detectBundleSkew } from './bundle-skew'
import { detectBundleSwap } from './bundle-swap'
import { registerChatOnboardingWindow } from './chat-onboarding-window'
-import { applyConnectionChange, sshQuitShouldBlock, teardownSshState } from './connection-apply'
+import { applyConnectionChange, teardownSshState } from './connection-apply'
import {
apiRequestRegistryConnectionId,
authModeFromStatus,
@@ -237,6 +241,7 @@ import { buildHudWindowUrl } from './hud-url'
import { resolveHudWindowing } from './hud-windowing'
import { createIntroRevealWindowController } from './intro-reveal-window'
import { createLinkTitleWindow, guardLinkTitleSession, readLinkTitleWindowTitle } from './link-title-window'
+import { createLocalBackendLifecycle, waitForTeardown } from './local-backend-lifecycle'
import { ensureMainWindow } from './main-window-lifecycle'
import {
assertManagedUpdatePreflightClear,
@@ -333,6 +338,7 @@ import {
} from './profile-session-routing'
import { createQuickEntryShortcut, quickEntryWindowBounds, sanitizeQuickEntrySettings } from './quick-entry'
import { type ActiveWork, mergeActiveWork, normalizeActiveWork, quitPromptFor } from './quit-guard'
+import { backendQuitNeedsWait, createQuitTeardownCoordinator } from './quit-teardown'
import * as remoteLifecycle from './remote-lifecycle'
import {
attachPowerResumeRemoteRevalidation,
@@ -373,6 +379,7 @@ import { ensureLoginShellPath } from './shell-path'
import { createBootstrapCoordinator, sshConfigFingerprint } from './ssh-bootstrap-coordinator'
import { collectSshConfigHosts, parseSshGOutput } from './ssh-config'
import { createSshProbeConnection, pickLocalPort, redactSecrets, SshConnection } from './ssh-connection'
+import { createSshTeardownTracker } from './ssh-teardown'
import { createStreamThrottle } from './stream-throttle'
import { registerTerminalIpc } from './terminal-ipc'
import { nativeOverlayWidth as computeNativeOverlayWidth, macTitleBarOverlayHeight } from './titlebar-overlay-width'
@@ -1409,6 +1416,32 @@ function registerMediaProtocol() {
let mainWindow = null
const backendConnectionState = createBackendConnectionState<ReturnType<typeof spawn>, any>()
+
+const localBackendLifecycle = createLocalBackendLifecycle<ReturnType<typeof spawn>>({
+ stopChild: child => {
+ if (child.exitCode === null && child.signalCode === null) {
+ stopBackendChildImpl(child, { forceKillProcessTree, isWindows: IS_WINDOWS })
+ }
+ },
+ waitForExit: child => waitForBackendExit(child),
+ cancelSetup: () => {
+ firstRunSetupGate?.resetForRetry()
+ bootstrapAbortController?.abort()
+ }
+})
+
+function spawnOwnedBackend(...args: Parameters<typeof spawn>) {
+ const child = localBackendLifecycle.spawn(() => spawn(...args))
+ child.once('exit', () => localBackendLifecycle.release(child))
+ child.once('error', () => {
+ if (!child.pid) {
+ localBackendLifecycle.release(child)
+ }
+ })
+
+ return child
+}
+
const remoteLiveness = new RemoteLivenessTracker()
const remoteRevalidation = new RemoteRevalidationCoordinator()
const registryDispatchRevalidation = new RemoteRevalidationCoordinator()
@@ -2407,6 +2440,7 @@ async function waitForUpdateToFinish() {
let announced = false
const outcome = await waitForUpdateClearance(updateGateDeps(), {
+ signal: localBackendLifecycle.signal,
onWaitTick: async reason => {
if (!announced) {
announced = true
@@ -2456,6 +2490,10 @@ async function waitForUpdateToFinish() {
rememberLog(`[updates] could not read hand-off result: ${err.message}`)
}
+ if (outcome === 'cancelled') {
+ localBackendLifecycle.assertCanStart()
+ }
+
if (outcome === 'clear') {
return false
}
@@ -4389,6 +4427,9 @@ async function handOffWindowsBootstrapRecovery(reason) {
await releaseBackendLockForUpdate(updateRoot)
+ // The recovery resolver may have awaited while quit sealed local startup.
+ localBackendLifecycle.assertCanStart()
+
const child = spawnUpdaterProcess(updater, updaterArgs, {
cwd: HERMES_HOME,
env: {
@@ -5183,7 +5224,13 @@ function resolveHermesBackend(backendArgs) {
}
}
-async function ensureRuntime(backend) {
+function ensureRuntime(backend: any): Promise<any> {
+ return localBackendLifecycle.start(() => runEnsureRuntime(backend))
+}
+
+async function runEnsureRuntime(backend: any): Promise<any> {
+ localBackendLifecycle.assertCanStart()
+
if (!backend.bootstrap) {
await advanceBootProgress('runtime.external', `Using ${backend.label}`, 32)
@@ -5229,6 +5276,7 @@ async function ensureRuntime(backend) {
void 0
}
+ localBackendLifecycle.assertCanStart()
bootstrapAbortController = new AbortController()
// The repair request has been honoured by reaching the installer; clear it
@@ -10234,10 +10282,7 @@ function clearManagedSshRecovery(connectionId, correlationId) {
}
const sshBootstrapCoordinator = createBootstrapCoordinator()
-
-let sshQuitTeardownDone = false
-let sshQuitTeardownPromise: Promise<void> | null = null
-let backendQuitTeardownDone = false
+const sshTeardowns = createSshTeardownTracker()
function sshScopeKey(profile) {
return connectionScopeKey(profile) || ''
@@ -10282,22 +10327,24 @@ async function teardownSshConnection(profile) {
// alone leaves the backend at pid 1 holding state.db (#91668).
// Windows remotes use a different lifecycle (connectWindowsRemote) and
// are left to a follow-up; POSIX is the leak that OOM'd gateways.
- await teardownSshState(
- {
- ...state,
- ownershipId: state.ownershipId || sshOwnershipKey(profile)
- },
- {
- cleanupRemote:
- state.remotePlatform === 'Windows'
- ? async () => {
- // connectWindowsRemote does not share POSIX lock/kill. Stay
- // silent on the kill path, but leave a log so quit is not a
- // mysterious no-op on Windows remotes.
- sshRememberLog('[ssh] skip remote serve teardown on Windows remotes; POSIX disconnect does not apply')
- }
- : remoteLifecycle.disconnect
- }
+ await sshTeardowns.track(state.ssh, () =>
+ teardownSshState(
+ {
+ ...state,
+ ownershipId: state.ownershipId || sshOwnershipKey(profile)
+ },
+ {
+ cleanupRemote:
+ state.remotePlatform === 'Windows'
+ ? async () => {
+ // connectWindowsRemote does not share POSIX lock/kill. Stay
+ // silent on the kill path, but leave a log so quit is not a
+ // mysterious no-op on Windows remotes.
+ sshRememberLog('[ssh] skip remote serve teardown on Windows remotes; POSIX disconnect does not apply')
+ }
+ : remoteLifecycle.disconnect
+ }
+ )
)
}
@@ -11199,7 +11246,7 @@ function resetBootProgressForReconnect() {
}
function stopBackendChild(child) {
- stopBackendChildImpl(child, { forceKillProcessTree, isWindows: IS_WINDOWS })
+ void localBackendLifecycle.stop(child).catch(error => rememberLog(`Backend teardown failed: ${error.message}`))
}
// Soft gateway-mode apply: tear down the primary without resetting boot UI or
@@ -11270,53 +11317,23 @@ function broadcastConnectionsChanged(payload: { connectionId: string; reason: 'r
}
}
-async function waitForBackendExit(child, timeoutMs = 5000) {
- if (!child || child.exitCode !== null || child.signalCode !== null) {
- return
- }
-
- const exited = () => child.exitCode !== null || child.signalCode !== null
-
- const wait = delay =>
- new Promise<void>(resolve => {
- if (exited()) {
- resolve()
-
- return
- }
-
- const timer = setTimeout(resolve, delay)
- child.once('exit', () => {
- clearTimeout(timer)
- resolve()
- })
- })
+const backendExitWaits = new Map<any, Promise<void>>()
- await wait(timeoutMs)
+function waitForBackendExit(child, timeoutMs = 5000) {
+ const existing = backendExitWaits.get(child)
- if (exited()) {
- return
+ if (existing) {
+ return existing
}
- try {
- if (IS_WINDOWS && Number.isInteger(child.pid)) {
- forceKillProcessTree(child.pid)
- } else if (Number.isInteger(child.pid)) {
- try {
- process.kill(-child.pid, 'SIGKILL')
- } catch {
- child.kill('SIGKILL')
- }
- } else {
- child.kill('SIGKILL')
- }
- } catch {
- return
- }
+ const waiting = waitForBackendExitImpl(child, { forceKillProcessTree, isWindows: IS_WINDOWS }, timeoutMs)
+ backendExitWaits.set(child, waiting)
+ void waiting.then(
+ () => backendExitWaits.delete(child),
+ () => backendExitWaits.delete(child)
+ )
- // Await the escalation as well; do not let shutdown or failed adoption race
- // a still-running backend.
- await wait(1000)
+ return waiting
}
// The profile the primary (window) backend runs as. readActiveDesktopProfile()
@@ -11355,6 +11372,7 @@ function profileRouteOptions(profile, request?) {
// resolveProfileBackendRoute(). An empty / unknown profile resolves to the
// primary, so legacy callers are unchanged.
async function ensureBackend(profile, opts: { passive?: boolean; spawnPriority?: LocalBackendSpawnPriority } = {}) {
+ localBackendLifecycle.assertCanStart()
const key = profile && String(profile).trim() ? String(profile).trim() : primaryProfileKey()
const spawnPriority = spawnPriorityFrom(opts.spawnPriority)
const passive = Boolean(opts.passive)
@@ -12361,7 +12379,7 @@ function releaseLocalBackendSlot(entry: any) {
}
function assertPoolEntryStillOwned(poolKey: string, entry: any) {
- if (backendPool.get(poolKey) !== entry) {
+ if (localBackendLifecycle.signal.aborted || backendPool.get(poolKey) !== entry) {
releaseLocalBackendSlot(entry)
throw new Error(`Profile backend start for "${poolKey}" was cancelled before spawn.`)
}
@@ -12410,7 +12428,11 @@ function teardownFailedLocalBackend(poolKey: string, entry: any): Promise<void>
// entry means THIS machine regardless of the v1 routing table); `opts.poolKey`
// is the backendPool key when it differs from the profile name (composite
// registry scopes) so the exit/error cleanup evicts the right entry.
-async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; poolKey?: string } = {}) {
+function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; poolKey?: string } = {}) {
+ return localBackendLifecycle.start(() => runPoolBackendStart(profile, entry, opts))
+}
+
+async function runPoolBackendStart(profile, entry, opts: { forceLocal?: boolean; poolKey?: string } = {}) {
const poolKey = opts.poolKey || profile
await reapOrphanedBackendsOnce()
@@ -12453,6 +12475,8 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po
const spawnPriority: LocalBackendSpawnPriority = spawnPriorityFrom(entry.spawnPriority)
+ assertPoolEntryStillOwned(poolKey, entry)
+
const spawnRequest = localBackendSpawnCoordinator.request(poolKey, {
timeoutMs: POOL_SLOT_WAIT_MS,
priority: spawnPriority
@@ -12467,7 +12491,14 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po
)
}
- entry.releaseLocalBackendSlot = await spawnRequest.acquired
+ const cancelRequest = () => spawnRequest.cancel()
+ localBackendLifecycle.signal.addEventListener('abort', cancelRequest, { once: true })
+
+ try {
+ entry.releaseLocalBackendSlot = await spawnRequest.acquired
+ } finally {
+ localBackendLifecycle.signal.removeEventListener('abort', cancelRequest)
+ }
if (entry.localBackendSpawnRequest === spawnRequest) {
entry.localBackendSpawnRequest = null
@@ -12485,7 +12516,9 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po
{
let poolAnnounced = false
- await waitForUpdateClearance(updateGateDeps(), {
+ const clearance = await waitForUpdateClearance(updateGateDeps(), {
+ signal: localBackendLifecycle.signal,
+ isCancelled: () => backendPool.get(poolKey) !== entry,
onWaitTick: reason => {
if (!poolAnnounced) {
poolAnnounced = true
@@ -12495,9 +12528,14 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po
pollMs: UPDATE_WAIT_POLL_MS,
timeoutMs: UPDATE_WAIT_TIMEOUT_MS
})
+
+ if (clearance === 'cancelled') {
+ assertPoolEntryStillOwned(poolKey, entry)
+ }
}
profileDeletionGate.assertCanStart(profile)
+ assertPoolEntryStillOwned(poolKey, entry)
// --profile wins over the inherited HERMES_HOME env (see _apply_profile_override
// step 3 in hermes_cli/main.py), so the child re-homes to this profile.
@@ -12525,7 +12563,7 @@ async function spawnPoolBackend(profile, entry, opts: { forceLocal?: boolean; po
const parentIdentityEnv = parentWatchdogEnv(process.pid, parentStartMarker, backendNonce)
assertPoolEntryStillOwned(poolKey, entry)
- const child = spawn(
+ const child = spawnOwnedBackend(
backend.command,
backend.args,
hiddenWindowsChildOptions({
@@ -12694,6 +12732,7 @@ async function stopAllPoolBackends() {
}
const backendShutdown = createBackendShutdownCoordinator(async () => {
+ const localShutdown = localBackendLifecycle.shutdown()
const primary = backendConnectionState.invalidate()
stopBackendChild(primary)
@@ -12704,9 +12743,21 @@ const backendShutdown = createBackendShutdownCoordinator(async () => {
poolIdleReaper = null
}
- await Promise.all([waitForBackendExit(primary), pooledStops])
+ await waitForTeardown([localShutdown, waitForBackendExit(primary), pooledStops], 7_000)
})
+const quitTeardown = createQuitTeardownCoordinator(() => app.quit())
+
+async function teardownSshForQuit() {
+ const scopes = [...sshConnections.keys()]
+
+ for (const scope of scopes) {
+ void teardownSshConnection(scope || null).catch(error => rememberLog(`SSH teardown failed: ${error.message}`))
+ }
+
+ await sshTeardowns.finish(sshBootstrapCoordinator.promises(), () => sshBootstrapCoordinator.forceCleanupAll())
+}
+
async function exitAfterBackendShutdown(code) {
await backendShutdown.run()
app.exit(code)
@@ -12764,7 +12815,11 @@ async function prepareProfileRenameRequest(request) {
})
}
-async function startHermes() {
+function startHermes() {
+ return localBackendLifecycle.start(runHermesStart)
+}
+
+async function runHermesStart() {
// Only the single-instance lock holder may reap/spawn/claim the desktop
// backend. A lock-losing instance must stay inert even if some path reaches
// here (e.g. the deferred-quit window before `ready`): its reapOrphans()
@@ -12776,6 +12831,9 @@ async function startHermes() {
await reapOrphanedBackendsOnce()
+ // Shutdown may have started while the orphan sweep was awaiting probes.
+ localBackendLifecycle.assertCanStart()
+
// Latched-failure short-circuit: once bootstrap has failed in this
// process, every subsequent startHermes() call re-throws the same error
// without re-running install.ps1. This prevents the renderer's
@@ -12897,6 +12955,7 @@ async function startHermes() {
}
const setup = await runPrimaryBackendStartup({
+ signal: localBackendLifecycle.signal,
connectRemote,
ensureLocalRuntime: ensureRuntime,
prepareLocalBackend: async () => {
@@ -12945,7 +13004,11 @@ async function startHermes() {
const backendNonce = crypto.randomBytes(16).toString('hex')
const parentIdentityEnv = parentWatchdogEnv(process.pid, parentStartMarker, backendNonce)
- const hermesProcess = spawn(
+ if (!backendConnectionState.isCurrentAttempt(connectionAttempt)) {
+ throw new Error('Hermes backend start was superseded by a newer connection attempt.')
+ }
+
+ const hermesProcess = spawnOwnedBackend(
backend.command,
backend.args,
hiddenWindowsChildOptions({
@@ -18184,50 +18247,24 @@ app.on('before-quit', event => {
// already quitting (#91668).
sshBootstrapCoordinator.shutdown()
- if (!backendQuitTeardownDone) {
- event.preventDefault()
- void backendShutdown.run().finally(() => {
- backendQuitTeardownDone = true
- app.quit()
- })
- }
-
- // backendShutdown.finally() re-enters before-quit. teardownSshConnection
- // already deleted the map entries, so size===0 would skip the kill wait
- // and let window-X quit finish while SSH exec is still running (#91668).
- if (
- sshQuitShouldBlock({
- teardownDone: sshQuitTeardownDone,
- connectionCount: sshConnections.size,
- bootstrapPending: sshBootstrapCoordinator.promises().length,
- inFlight: sshQuitTeardownPromise
- })
- ) {
- event.preventDefault()
+ const backendNeedsWait = backendQuitNeedsWait({
+ connectionPending: backendConnectionState.getPendingPromise() !== null || localBackendLifecycle.hasPending(),
+ poolPending: poolStopper.hasPending(),
+ processAttached: backendConnectionState.getProcess() !== null,
+ shutdownPending: backendShutdown.isPending()
+ })
- if (!sshQuitTeardownPromise) {
- const scopes = [...sshConnections.keys()]
+ const sshNeedsWait =
+ sshConnections.size > 0 || sshBootstrapCoordinator.promises().length > 0 || sshTeardowns.hasPending()
- const pending = Promise.allSettled([
- ...scopes.map(scope => teardownSshConnection(scope || null)),
- ...sshBootstrapCoordinator.promises()
- ])
+ const teardownTasks = [{ run: () => backendShutdown.run(), waitForCompletion: backendNeedsWait }]
- // cleanupStale waits up to 5s for the owned pid to exit (50 * 100ms).
- // The previous 4s race could close SSH first and leave serve --isolated
- // reparented to pid 1. Latch this promise BEFORE those deletes land so
- // a re-entrant quit still waits.
- sshQuitTeardownPromise = Promise.race([pending, new Promise<void>(resolve => setTimeout(resolve, 6_000))]).then(
- async () => {
- await sshBootstrapCoordinator.forceCleanupAll()
- }
- )
- }
+ if (sshNeedsWait) {
+ teardownTasks.push({ run: teardownSshForQuit, waitForCompletion: true })
+ }
- void sshQuitTeardownPromise.then(() => {
- sshQuitTeardownDone = true
- app.quit()
- })
+ if (quitTeardown.begin(teardownTasks)) {
+ event.preventDefault()
}
// Clean quit mid-boot should not trip next-launch --no-sandbox (#38216).