fix(gateway): clean pending request when send fails

This commit is contained in:
Vincent Koc
2026-06-17 14:38:34 +02:00
parent 731dfcc5f9
commit 47cad606f4
2 changed files with 29 additions and 1 deletions

View File

@@ -1599,7 +1599,14 @@ export class GatewayClient {
});
signal?.addEventListener("abort", abortHandler, { once: true });
});
this.ws.send(JSON.stringify(frame));
try {
this.ws.send(JSON.stringify(frame));
} catch (error) {
const pending = this.pending.get(id);
this.pending.delete(id);
pending?.cleanup?.();
throw error;
}
return p;
}
}

View File

@@ -317,6 +317,27 @@ describe("GatewayClient", () => {
}
});
test("cleans pending request state when websocket send throws", async () => {
const client = new GatewayClient({
requestTimeoutMs: 25,
});
const sendError = new Error("synthetic send failure");
(
client as unknown as {
ws: WebSocket | { readyState: number; send: () => void; close: () => void };
}
).ws = {
readyState: WebSocket.OPEN,
send: vi.fn(() => {
throw sendError;
}),
close: vi.fn(),
};
await expect(client.request("status")).rejects.toThrow("synthetic send failure");
expect(getPendingCount(client)).toBe(0);
});
test("does not auto-timeout expectFinal requests", async () => {
vi.useFakeTimers();
try {