Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/lib/gateway-chat-pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,23 @@ describe("getGatewayClientForRuntime", () => {
expect(mockState.clients[0].close).toHaveBeenCalledTimes(1);
expect(getGatewayPoolDiagnostics()).toMatchObject({ poolSize: 1 });
});

it("replaces disconnected held clients instead of returning them", async () => {
addRuntime("runtime-a");

const disconnectedClient = await getGatewayClientForRuntime("runtime-a");
holdClient(disconnectedClient);
mockState.clients[0].isConnected = false;

const freshClient = await getGatewayClientForRuntime("runtime-a");

expect(freshClient).not.toBe(disconnectedClient);
expect(mockState.clients).toHaveLength(2);
expect(mockState.clients[0].close).toHaveBeenCalledTimes(1);
expect(getGatewayPoolDiagnostics()).toMatchObject({
activeClients: 0,
activeHolds: 0,
poolSize: 1,
});
});
});
3 changes: 2 additions & 1 deletion src/lib/gateway-chat-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ async function getClientForRuntime(runtime: GatewayRuntimeConnection | null | un

// Close stale connection — but only if no active request is using it
if (existing) {
if (activeClientHolds.has(existing.client)) {
if (activeClientHolds.has(existing.client) && existing.client.isConnected) {
// Client is mid-request, return it anyway (don't recycle under its feet)
publishAgentModeDiagnostic({
scope: "gateway-pool",
Expand All @@ -519,6 +519,7 @@ async function getClientForRuntime(runtime: GatewayRuntimeConnection | null | un
});
return existing.client;
}
activeClientHolds.delete(existing.client);
publishAgentModeDiagnostic({
scope: "gateway-pool",
event: "client.recycle.close-stale",
Expand Down
Loading