Skip to content

The agent that grows with you

fix(desktop): release profile activation lease on dial failure

echoes6664b21f970aaaa
diff --git a/apps/desktop/src/store/gateway.test.ts b/apps/desktop/src/store/gateway.test.ts
index 6848f6cc13e..b6b7198d7e9 100644
--- a/apps/desktop/src/store/gateway.test.ts
+++ b/apps/desktop/src/store/gateway.test.ts
@@ -45,8 +45,14 @@ vi.mock('@/store/session', () => ({
}))
vi.mock('@/store/notify-baseline', () => ({ markNativeNotifyBaseline: vi.fn() }))
-const { activeGateway, closeSecondaryGateways, configureGatewayRegistry, ensureGatewayForProfile, setPrimaryGateway } =
- await import('./gateway')
+const {
+ activeGateway,
+ closeSecondaryGateways,
+ configureGatewayRegistry,
+ ensureGatewayForProfile,
+ pruneSecondaryGateways,
+ setPrimaryGateway
+} = await import('./gateway')
function installDesktop(stub: Record<string, unknown>): void {
;(window as unknown as { hermesDesktop: unknown }).hermesDesktop = stub
@@ -101,6 +107,26 @@ describe('ensureGatewayForProfile — secondary connect failure surfaces (#81094
expect(gatewayMocks.instances).toHaveLength(1)
})
+ it('releases the activation lease when the first dial is rejected so pruning disposes it', async () => {
+ const getConnection = vi.fn(async ({ profile }: { profile: string }) => ({
+ authMode: 'token',
+ baseUrl: `https://${profile}.invalid`,
+ mode: 'local',
+ profile,
+ token: 'fake-test-token',
+ wsUrl: `wss://${profile}.invalid/ws`
+ }))
+
+ installDesktop({ getConnection })
+ gatewayMocks.connect.mockRejectedValue(new Error('backend unreachable'))
+
+ await expect(ensureGatewayForProfile('work')).rejects.toThrow('backend unreachable')
+
+ pruneSecondaryGateways(new Set())
+
+ expect(gatewayMocks.instances[0].close).toHaveBeenCalledTimes(1)
+ })
+
it('keeps the reconnect schedule armed so transient failures still self-heal', async () => {
vi.useFakeTimers()
diff --git a/apps/desktop/src/store/gateway.ts b/apps/desktop/src/store/gateway.ts
index 33ac7683384..2478d64a23f 100644
--- a/apps/desktop/src/store/gateway.ts
+++ b/apps/desktop/src/store/gateway.ts
@@ -1086,28 +1086,30 @@ export async function ensureGatewayForProfile(profile: string): Promise<void> {
// profile-door twin of the agent path's lease above (#89622).
entry.activationLeaseUntil = Date.now() + ACTIVATION_LEASE_MS
- if (!isOpen(entry.gateway)) {
- clearTimer(entry)
- entry.reconnectAttempt = 0
+ try {
+ if (!isOpen(entry.gateway)) {
+ clearTimer(entry)
+ entry.reconnectAttempt = 0
- try {
- await openSecondary(entry)
- } catch (error) {
- // #81094: a failed secondary dial must NOT fall through to setActive()
- // with a closed socket — that silently routes the user's messages to the
- // primary backend (cross-profile session writes). Keep the reconnect
- // schedule (transient failures still self-heal via the backoff below)
- // but RE-THROW so the profile-door caller surfaces the failure and skips
- // the activation. The agent-door twin (ensureGatewayForAgent) keeps its
- // boolean contract and is guarded by the activeGateway() null invariant.
- scheduleReconnect(entry)
- throw error
+ try {
+ await openSecondary(entry)
+ } catch (error) {
+ // #81094: a failed secondary dial must NOT fall through to setActive()
+ // with a closed socket — that silently routes the user's messages to the
+ // primary backend (cross-profile session writes). Keep the reconnect
+ // schedule (transient failures still self-heal via the backoff below)
+ // but RE-THROW so the profile-door caller surfaces the failure and skips
+ // the activation. The agent-door twin (ensureGatewayForAgent) keeps its
+ // boolean contract and is guarded by the activeGateway() null invariant.
+ scheduleReconnect(entry)
+ throw error
+ }
}
+ } finally {
+ // The activation is settling either way — release the prune lease.
+ entry.activationLeaseUntil = 0
}
- // The activation is settling either way — release the prune lease.
- entry.activationLeaseUntil = 0
-
if (entry.wantOpen && g.secondaries.get(key) === entry && applyActive(key, activationEpoch) && entry.connection) {
publishActiveConnection(entry.connection)
}