fix(auth): cancel WHAM probe error bodies

This commit is contained in:
Vincent Koc
2026-06-19 11:02:59 +02:00
parent 6467c1962a
commit 8aaf937bc0
2 changed files with 20 additions and 0 deletions

View File

@@ -1091,6 +1091,19 @@ describe("markAuthProfileFailure — WHAM-aware Codex cooldowns", () => {
expect(store.usageStats?.["openai:default"]?.cooldownUntil).toBe(now + 300_000);
});
it("cancels WHAM HTTP error response bodies", async () => {
const now = 1_700_000_000_000;
const store = makeStore({});
const response = new Response("server busy", { status: 500 });
const cancel = vi.spyOn(response.body!, "cancel").mockResolvedValue(undefined);
fetchMock.mockResolvedValueOnce(response);
await markCodexFailureAt({ store, now });
expect(cancel).toHaveBeenCalledOnce();
expect(store.usageStats?.["openai:default"]?.cooldownUntil).toBe(now + 300_000);
});
it("preserves a longer existing cooldown via max semantics", async () => {
const now = 1_700_000_000_000;
const existingCooldownUntil = now + 6 * 60 * 60 * 1000;

View File

@@ -210,6 +210,12 @@ function applyWhamCooldownResult(params: {
};
}
async function cancelUnreadResponseBody(response: Response): Promise<void> {
if (!response.bodyUsed) {
await response.body?.cancel().catch(() => undefined);
}
}
async function probeWhamForCooldown(
store: AuthProfileStore,
profileId: string,
@@ -249,6 +255,7 @@ async function probeWhamForCooldown(
});
if (!res.ok) {
await cancelUnreadResponseBody(res);
if (res.status === 401) {
return { cooldownMs: WHAM_TOKEN_EXPIRED_COOLDOWN_MS, reason: "wham_token_expired" };
}