The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): keep SSH serve teardown across re-entrant quit
686f6c6137fc92a3d6e1
diff --git a/apps/desktop/electron/connection-apply.test.ts b/apps/desktop/electron/connection-apply.test.tsindex e19e90b4a24..11401326610 100644--- a/apps/desktop/electron/connection-apply.test.ts+++ b/apps/desktop/electron/connection-apply.test.ts@@ -4,6 +4,7 @@ import {applyConnectionChange,commitConnectionFailure,resolveTerminalConnection,+ sshQuitShouldBlock,teardownSshState} from './connection-apply'@@ -91,6 +92,48 @@ 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.tsindex 03ad29741bb..ebb478b7098 100644--- a/apps/desktop/electron/connection-apply.ts+++ b/apps/desktop/electron/connection-apply.ts@@ -61,6 +61,21 @@ 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@@ -91,5 +106,6 @@ export {commitConnectionFailure,resolveTerminalConnection,resolveTerminalConnectionForSender,+ sshQuitShouldBlock,teardownSshState}diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex bc8a5408b04..5b5b5747c85 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -87,7 +87,7 @@ import {buildBrowserWindowUrl} from './browser-windows'import { detectBundleSkew } from './bundle-skew'-import { applyConnectionChange, teardownSshState } from './connection-apply'+import { applyConnectionChange, sshQuitShouldBlock, teardownSshState } from './connection-apply'import {apiRequestRegistryConnectionId,authModeFromStatus,@@ -9851,6 +9851,7 @@ function clearManagedSshRecovery(connectionId, correlationId) {const sshBootstrapCoordinator = createBootstrapCoordinator()let sshQuitTeardownDone = false+let sshQuitTeardownPromise: Promise<void> | null = nulllet backendQuitTeardownDone = falsefunction sshScopeKey(profile) {@@ -17540,20 +17541,39 @@ app.on('before-quit', event => {})}- if ((sshConnections.size > 0 || sshBootstrapCoordinator.promises().length > 0) && !sshQuitTeardownDone) {+ // 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 scopes = [...sshConnections.keys()]- const pending = Promise.allSettled([- ...scopes.map(scope => teardownSshConnection(scope || null)),- ...sshBootstrapCoordinator.promises()- ])+ if (!sshQuitTeardownPromise) {+ const scopes = [...sshConnections.keys()]+ const pending = Promise.allSettled([+ ...scopes.map(scope => teardownSshConnection(scope || null)),+ ...sshBootstrapCoordinator.promises()+ ])++ // 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()+ })+ }- // 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.- void Promise.race([pending, new Promise(resolve => setTimeout(resolve, 6_000))]).then(async () => {- await sshBootstrapCoordinator.forceCleanupAll()+ void sshQuitTeardownPromise.then(() => {sshQuitTeardownDone = trueapp.quit()})