Skip to content

Commit 80b8e9b

Browse files
outlier27-celltestikun
authored andcommitted
fix(usage): reject pre-cancelled quota waits
1 parent b2a82fc commit 80b8e9b

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎extensions/usage/providers/utils.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export async function waitForUsage<T>(
8282
promise: Promise<T>,
8383
signal: AbortSignal,
8484
): Promise<T> {
85+
signal.throwIfAborted();
8586
let onAbort: (() => void) | undefined;
8687
const aborted = new Promise<never>((_resolve, reject) => {
8788
onAbort = () => reject(signal.reason);

‎tests/extensions/usage/providers.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { DEFAULT_ADAPTERS } from "../../../extensions/usage/providers/registry.t
1111
import {
1212
computeUsageStatus,
1313
parseTimestamp,
14+
waitForUsage,
1415
} from "../../../extensions/usage/providers/utils.ts";
1516

1617
function jwt(payload: Record<string, unknown>) {
@@ -25,6 +26,17 @@ function payloadFetch(payload: unknown): typeof fetch {
2526
return async () => Response.json(payload);
2627
}
2728

29+
test("a pre-cancelled quota wait cannot return an already-fulfilled result", async () => {
30+
const controller = new AbortController();
31+
const reason = new Error("cancelled fixture");
32+
controller.abort(reason);
33+
34+
await assert.rejects(
35+
waitForUsage(Promise.resolve("stale quota"), controller.signal),
36+
(error: unknown) => error === reason,
37+
);
38+
});
39+
2840
test("Cursor uses its session cookie, authoritative percentages and independent account metadata", async () => {
2941
const calls: Array<{ url: string; init?: RequestInit }> = [];
3042
const report = await cursorAdapter.fetchUsage(

0 commit comments

Comments
 (0)