fix(usage): cap provider usage fetch timeouts

This commit is contained in:
Peter Steinberger
2026-05-29 16:53:07 -04:00
parent a7820b2f54
commit 7f09d6ae48
2 changed files with 18 additions and 1 deletions

View File

@@ -7,6 +7,8 @@ import {
parseFiniteNumber,
} from "./provider-usage.fetch.shared.js";
const MAX_TIMER_TIMEOUT_MS = 2_147_000_000;
function requireFetchCall(
mock: ReturnType<typeof vi.fn>,
): [URL | RequestInfo, RequestInit | undefined] {
@@ -92,6 +94,19 @@ describe("provider usage fetch shared helpers", () => {
}
});
it("caps oversized request timeouts before scheduling", async () => {
const timeoutSpy = vi
.spyOn(globalThis, "setTimeout")
.mockReturnValue(1 as unknown as ReturnType<typeof setTimeout>);
vi.spyOn(globalThis, "clearTimeout").mockImplementation(() => undefined);
const fetchFnMock = vi.fn(async () => new Response("{}", { status: 200 }));
const fetchFn = withFetchPreconnect(fetchFnMock);
await fetchJson("https://example.com/usage", {}, MAX_TIMER_TIMEOUT_MS + 1_000_000, fetchFn);
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
});
it("maps configured status codes to token expired", () => {
const snapshot = buildUsageHttpErrorSnapshot({
provider: "openai-codex",

View File

@@ -1,3 +1,4 @@
import { resolveTimerTimeoutMs } from "../shared/number-coercion.js";
import { parseFiniteNumber as parseFiniteNumberish } from "./parse-finite-number.js";
import { PROVIDER_LABELS } from "./provider-usage.shared.js";
import type { ProviderUsageSnapshot, UsageProviderId } from "./provider-usage.types.js";
@@ -8,8 +9,9 @@ export async function fetchJson(
timeoutMs: number,
fetchFn: typeof fetch,
): Promise<Response> {
const safeTimeoutMs = resolveTimerTimeoutMs(timeoutMs, 1);
const controller = new AbortController();
const timer = setTimeout(controller.abort.bind(controller), timeoutMs);
const timer = setTimeout(controller.abort.bind(controller), safeTimeoutMs);
try {
return await fetchFn(url, { ...init, signal: controller.signal });
} finally {