Skip to content

fix(usage): reject a pre-cancelled quota wait before returning a cached result #733

Closed

Description

@outlier27-cell

Problem

The shared /usage cancellation boundary can return a fulfilled result after its AbortSignal is already cancelled. waitForUsage() registers both the supplied promise and its cancellation rejection in Promise.race() without checking a pre-aborted signal first, so an already-resolved credential or quota promise wins the race.

Reproduction

On origin/main a18f457687b45c877bc8a15d689a7580c4f0659a, with Node 24.14.1:

node --experimental-strip-types --input-type=module -e "import { waitForUsage } from './extensions/usage/providers/utils.ts'; const controller = new AbortController(); controller.abort(new Error('cancelled fixture')); console.log(await waitForUsage(Promise.resolve('stale quota'), controller.signal));"

It prints stale quota. A cancelled wait should reject with the cancellation reason and must not make a prior result observable through this helper.

Scope

Add an entry guard to preserve cancellation precedence, and cover both a pre-cancelled fulfilled promise and the existing in-flight cancellation behavior. This does not abort or take ownership of Pi credential refresh; it only prevents the caller from observing a result after cancellation.

Deduplication

I checked open/closed Issues and PRs for usage cancel, AbortSignal, and usage-provider cancellation. Existing cancellation work covers provider request abort/fallback, not waitForUsage admitting a pre-cancelled fulfilled result.

Activity

  1. outlier27-cell commented on Oct 11, 2026

    @outlier27-cell
    ContributorAuthor

    I am claiming this narrow cancellation-boundary fix. I will add a deterministic regression, run the required checks, and open a separate PR from a fresh origin/main worktree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions