fix(desktop): bound the onActiveConnectionInvalidated fallback getConnection() call

The registry's active-connection-invalidated fallback re-dial was the one
getConnection() await in this file the #93454 bound-every-IPC-round-trip
sweep never reached. A wedged main-process round-trip during an eviction
fallback (idle reap, connection removal, profile delete) left $connection
latched on a promise that never settles instead of rejecting into the
existing catch/publish(null) path.
This commit is contained in:
nftpoetrist
2026-08-26 17:05:32 +03:00
committed by Teknium
parent 2a41f9b635
commit fbe51fa734
2 changed files with 69 additions and 2 deletions
@@ -12,6 +12,7 @@ import {
import {
activeGateway,
closeSecondaryGateways,
disposeSecondariesForConnection,
ensureGatewayForAgent,
isActivePrimary,
requestGatewayForAgent
@@ -1144,6 +1145,65 @@ describe('useGatewayBoot remote reconnect loop (real hook, fake socket)', () =>
expect($gatewayState.get()).toBe('open')
})
it('onActiveConnectionInvalidated: a fallback getConnection() that hangs rejects on its own instead of latching $connection forever (#93454 sibling)', async () => {
// Repro: the active connection is a registered secondary (e.g. a Bots-pane
// source). It gets removed/invalidated (disposeSecondariesForConnection),
// which falls back to redialing the primary profile via
// desktop.getConnection(fallbackProfile) — the one getConnection() await
// in this file the #93454 bound-every-IPC-round-trip sweep never reached.
// A wedged main-process round-trip here must reject instead of leaving
// $connection pointed at a promise that never settles.
const desktop = fakeDesktop() as ReturnType<typeof fakeDesktop> & {
getConnectionFor: ReturnType<typeof vi.fn>
}
desktop.getConnectionFor = vi.fn(async ({ connectionId, profile }: { connectionId: string; profile: string }) => ({
...coderConn,
connectionId,
profile
}))
;(window as { hermesDesktop?: unknown }).hermesDesktop = desktop
render(<Harness />)
await flushAsync()
expect($gatewayState.get()).toBe('open')
expect($connection.get()).not.toBeNull()
let opening!: Promise<boolean>
act(() => {
opening = ensureGatewayForAgent('cloud', 'default')
})
await flushAsync()
await opening
expect(isActivePrimary()).toBe(false)
// The active secondary is about to be evicted; the fallback re-dial for
// the primary profile hangs indefinitely.
desktop.getConnection.mockImplementation(() => new Promise(() => undefined))
act(() => {
disposeSecondariesForConnection('cloud')
})
await flushAsync()
expect(isActivePrimary()).toBe(true)
expect(desktop.getConnection).toHaveBeenCalledWith('default')
// Still stuck behind the hung fallback dial — the invalidation handler's
// .then()/.catch() has not run yet.
expect($connection.get()).not.toBeNull()
// Advance past the internal reconnect-attempt timeout (20s) — the stalled
// fallback getConnection() must reject so the handler's catch publishes
// null, instead of latching $connection on a connection that will never
// resolve.
await act(async () => {
await vi.advanceTimersByTimeAsync(20_000)
})
expect($connection.get()).toBeNull()
})
it('a getConnection() that hangs on INITIAL boot rejects on its own after the reconnect-attempt timeout, not only when main eventually gives up (#93454)', async () => {
// boot()'s getConnection() had no bound of its own — only main's own
// eventual timeout (e.g. waitForHermes, ~45s) ever settled it. A wedge
@@ -715,8 +715,15 @@ export function useGatewayBoot({
},
onActiveConnectionInvalidated: (fallbackProfile, invalidationEpoch) => {
$activeGatewayProfile.set(fallbackProfile)
void desktop
.getConnection(fallbackProfile)
// Bounded like every other getConnection() call in this file (#93454):
// an eviction fallback (idle reap, connection removal, profile delete)
// must not latch the profile atom to a connection that never resolves
// if the main-process IPC round-trip wedges.
void withTimeout(
desktop.getConnection(fallbackProfile),
RECONNECT_ATTEMPT_TIMEOUT_MS,
'Timed out resolving the fallback gateway connection'
)
.then(connection => {
if (!cancelled && gatewayActivationEpoch() === invalidationEpoch) {
publish(connection)