Skip to content

fix(platform): fit the Hyperdrive origin connection limit to the database - #910

Open
Brooooooklyn wants to merge 1 commit into
mainfrom
fix/hyperdrive-connection-limit
Open

Brooooooklyn wants to merge 1 commit into
mainfrom
fix/hyperdrive-connection-limit

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

How it was found

A live end-to-end run of the self-hosted platform, done while testing security audits (PR #886), ran about 15 queued scans on one install. The scans themselves were fine. The database was not. This bug is outside the security-audits feature, so it gets its own PR.

The install's PostgreSQL server had max_connections = 25. The Hyperdrive config the installer created had origin_connection_limit = 60 (the Cloudflare default for that account). pg_stat_activity showed 18+ Hyperdrive connections whenever we could get in.

Repro

  1. Use a PostgreSQL server with a small max_connections (25 here; many hosted starter plans are this size).
  2. void platform install with "Void creates the Hyperdrive".
  3. Put load on the platform: about 15 queued scans plus normal API use.
  4. From your machine, connect with VOID_PLATFORM_DATABASE_URL (psql, or void platform upgrade).
Expected Hyperdrive stays below what the server accepts, so a direct connection still gets a slot.
Actual 53300 remaining connection slots are reserved for roles with the SUPERUSER attribute for 15+ minutes at a time. The API also returned an intermittent 500 ("Failed to read project security", on a deployment_operations query).

Root cause

On main, the installer creates the Hyperdrive with no origin_connection_limit and never changes it later:

  • packages/void/src/cli/platform-installer-resources.ts:287 calls client.createHyperdrive(...).
  • packages/void/src/cli/platform-cloudflare.ts:1009 (createHyperdrive) posts only name, caching and origin.
  • git grep origin_connection_limit finds nothing in packages/*/src or platform/packages/*/src.

So Cloudflare's default limit applies. When that is above the server's non-superuser slots, the pool can take every slot.

Fix

install / repair / upgrade
  └─ verify uncached Hyperdrive (existing)
  └─ fit Hyperdrive to database (new)
       slots = max_connections - superuser_reserved_connections - reserved_connections
       limit = max(5, slots - 5)
       live limit > limit ?
         created by Void → PATCH origin_connection_limit = limit
         adopted         → print a warning only
  • readConnectionSlots runs on the lifecycle-lock connection the installer already holds, so it opens no extra connection (platform-installer-database.ts). reserved_connections exists only on PostgreSQL 16+; older servers count it as 0.
  • getHyperdriveConfig now reads origin_connection_limit, and a new setHyperdriveOriginConnectionLimit sends the PATCH (platform-cloudflare.ts).
  • platform-hyperdrive-connections.ts holds the rule. It only ever lowers the limit, so it never sends a value above the account plan's maximum (Free is about 20). If Cloudflare reports no limit, it leaves the config alone.
  • Called from platform-install-provisioning.ts right after the existing Hyperdrive check, so existing installs are fixed by their next void platform repair or upgrade.
  • With the e2e numbers (25 max, 3 superuser slots) the limit becomes 17, which leaves 5 slots for direct connections.
  • Docs: the database sizing paragraph in docs/guide/platform/installation/prerequisites.md now says what Void does and what is left to the user.

Tests

  • packages/void/test/unit/platform-hyperdrive-connections.test.ts (new): 60 → 17 for a 22-slot database; no change when the limit already fits or is not reported; minimum of 5; adopted Hyperdrive only gets a warning; no calls when the lock cannot read slots.
  • platform-cloudflare.test.ts: parses origin_connection_limit and sends the PATCH body.
  • platform-database-lock.test.ts: readConnectionSlots runs against real PostgreSQL (PGlite) through the lock wrapper.
  • Test fakes for the installer client gained the new method.
  • Ran: the focused files, vp check (only errors are missing generated playground/*/.void/tsconfig.json in a fresh worktree; none in changed code), and vp run test. The 8 failures in that run were timeouts in platform/packages/api integration tests while another worktree ran the same suite; those 6 files pass when rerun alone.

🤖 Generated with Claude Code

…base

The installer created the platform Hyperdrive without an origin
connection limit, so Cloudflare's default (60 on the e2e account) could
exceed a small database's max_connections. Under load the pool took
every non-superuser slot and direct connections (upgrade, repair, psql)
failed with 53300 for minutes at a time.

Install, repair, and upgrade now read max_connections minus the reserved
slots on the held lifecycle lock connection and lower the limit of the
Hyperdrive Void created so five slots stay free. The limit is never
raised, so it stays within the account plan. A separately managed
Hyperdrive only gets a warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-23T22:53:58.443082Z 0a244ba PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

This branch has not been deployed

No deployments
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