Skip to content

fix(billing): let a slow ledger read finish instead of blocking execution at the singleflight default - #8146

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/usage-gate-settle-deadline
Sep 22, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/usage-gate-settle-deadline

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Execution admission goes through the usage-gate cache, which coalesces concurrent misses with coalesceLocally. That helper's default settle deadline is 30 s and exists to bound a hung producer. The gate's producer is a ledger sum over the payer's billing period; for a large organization that is millions of rows and, from a cold cache or under heavy I/O, legitimately takes longer than 30 s
  • When the deadline fired, every caller coalesced on that key got CoalesceSettleTimeoutError, the preprocessing step could not determine limits and blocked the run ("Unable to determine usage limits. Execution blocked for security"), while the timed-out read kept running detached and the next caller started a second read of the same ledger alongside it
  • The gate now passes its own settle deadline, USAGE_GATE_SETTLE_TIMEOUT_MS (120 s), above any statement ceiling the deployment applies: a slow read waits and finishes, the database's own statement timeout still ends a stuck one and that failure is reported as before, and only a connection that never answers is given up on by the gate
  • No change to the cache policy: admissions cached for the TTL, refusals re-read, failed reads never cached, writes on the value the caller received

Type of Change

  • Bug fix

Testing

  • Two new tests: a ledger read that takes 45 s resolves instead of rejecting (fails on the previous default), and a read that never answers is rejected at the gate deadline and re-read fresh afterwards
  • bun run lint, check:audits (47 audits), docs-manifest:check, type-check pass

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 22, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
Image docs Skipped Skipped Sep 22, 2026 6:36pm UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the previous deadline and delay-helper findings are resolved, and no new actionable issue was found.

Summary

This PR prevents legitimate slow billing-ledger reads from being abandoned by the usage gate while ensuring that PostgreSQL terminates reads that exceed the configured ceiling.

  • Adds a transaction-local 60-second statement timeout around billing-period ledger sums.
  • Derives the usage-gate coalescing deadline from that database timeout with a 15-second margin.
  • Tests slow, hung, and statement-bounded reads and updates affected provisioning fixtures.
  • Moves the shared timeout constant into the billing constants module.
Diagram
sequenceDiagram
    participant Caller
    participant Gate as Usage gate
    participant Flight as Local singleflight
    participant DB as PostgreSQL

    Caller->>Gate: Check usage limits
    Gate->>Flight: Coalesce cache miss (75s deadline)
    Flight->>DB: Begin transaction
    Flight->>DB: "SET LOCAL statement_timeout = 60s"
    Flight->>DB: Sum billing-period ledger
    alt Query completes within 60s
        DB-->>Flight: Usage total
        Flight-->>Gate: Limit result
        Gate-->>Caller: Admission decision
    else Query exceeds 60s
        DB-->>Flight: Statement timeout error
        Flight-->>Gate: Read failure
        Gate-->>Caller: Fail closed
    else Connection never responds
        Flight-->>Gate: Coalescing timeout after 75s
        Gate-->>Caller: Read failure
    end
Loading

Reviews (4) · Last reviewed commit: "fix(billing): keep the ledger statement ..."

Comment thread apps/sim/lib/billing/core/usage-gate-cache.ts Outdated
Comment thread apps/sim/lib/billing/core/usage-gate-cache.test.ts Outdated

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/billing/core/usage-gate-cache.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 5 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 6 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit cbd2603 into staging Sep 22, 2026
25 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/usage-gate-settle-deadline branch September 22, 2026 18:42

This branch was previously deployed

1 inactive deployment
Preview 5b6a4a28 Deployed Sep 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant