fix(skill-mcp-manager): drop superseded stale clients

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
YeonGyu-Kim
2026-03-12 02:24:29 +09:00
parent 7997606892
commit cbb378265e
2 changed files with 19 additions and 0 deletions

View File

@@ -230,4 +230,22 @@ describe("getOrCreateClient multi-key disconnect race", () => {
expect(state.clients.has(clientKey2)).toBe(false)
expect(state.disconnectedSessions.has("session-a")).toBe(false)
})
it("#given a superseded pending connection #when the old connection completes #then the stale client is removed from state.clients", async () => {
const state = createState()
const info = createClientInfo("session-a")
const clientKey = createClientKey(info)
const pendingConnect = createDeferred<void>()
const supersedingConnection = createDeferred<Awaited<ReturnType<typeof getOrCreateClient>>>()
pendingConnects.push(pendingConnect)
const clientPromise = getOrCreateClient({ state, clientKey, info, config: stdioConfig })
state.pendingConnections.set(clientKey, supersedingConnection.promise)
pendingConnect.resolve(undefined)
await expect(clientPromise).rejects.toThrow(/superseded by a newer connection attempt/)
expect(state.clients.has(clientKey)).toBe(false)
expect(createdClients[0]?.close).toHaveBeenCalledTimes(1)
})
})

View File

@@ -42,6 +42,7 @@ export async function getOrCreateClient(params: {
const isStale = state.pendingConnections.has(clientKey) && state.pendingConnections.get(clientKey) !== currentConnectionPromise
if (isStale) {
state.clients.delete(clientKey)
try { await client.close() } catch {}
throw new Error(`Connection for "${info.sessionID}" was superseded by a newer connection attempt.`)
}