Skip to content

fix(platform): survive a dropped lifecycle lock connection during upgrade - #909

Open
Brooooooklyn wants to merge 1 commit into
mainfrom
fix/upgrade-pg-error
Open

Brooooooklyn wants to merge 1 commit into
mainfrom
fix/upgrade-pg-error

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

How it was found

During a live end-to-end run of the self-hosted platform while testing the security audits work (PR #886), void platform upgrade crashed halfway through and left the platform paused. The crash is not related to security audits. It is in the upgrade command's PostgreSQL lifecycle lock, which every install, upgrade, repair, enable and disable uses.

Repro

  1. Have a self-hosted platform whose database closes idle connections (seen with PlanetScale Postgres).
  2. Run void platform upgrade --runtime <path> --yes.
  3. While the CLI is in "Deploying the dispatch service" (minutes with no database traffic), the server resets the idle lock connection.

Expected vs actual

Expected The upgrade fails with a clear message, the normal failure handler runs and records the failed operation, and the operator reruns the command.
Actual The process dies with ▲ Something went wrong / node:events Unhandled 'error' event / Error: read ECONNRESET ... Emitted 'error' event on Client instance. No failure handling runs. Operator API calls return 503 "the platform is paused for an upgrade or repair" and platform status shows "Saved operation: upgrade — deploying" until the upgrade is rerun.

Root cause

packages/void/src/cli/platform-installer-database.ts:207-237 (acquirePlatformDatabaseLifecycleLock) checks out one pg client with pool.connect() and holds it for the whole lifecycle command, because the lock is a session advisory lock. It never adds an 'error' listener to the client or the pool.

pg removes the pool's own error listener from a client while it is checked out. When the socket is reset, pg emits 'error' on the client. With no listener, Node throws it as an unhandled 'error' event and the process exits.

pool.connect() ──► client held for whole upgrade
                     │
   deploy Workers (idle minutes)
                     │
   server resets socket ──► client.emit('error')  ──► no listener ──► process exits

Fix

In acquirePlatformDatabaseLifecycleLock:

  • Add pool.on('error') and client.on('error'). The client listener keeps the first error.
  • Run lock queries through a small QueryConnection wrapper. After a lost connection, each lock call (checkpoint, verify, migrate, reads) fails with platform: The PostgreSQL connection that holds the lifecycle lock was closed (read ECONNRESET). Rerun the command to continue. That error goes through the normal upgrade failure path.
  • On release after a lost connection, skip pg_advisory_unlock, because the server already ended the session lock with the connection. Release the client with the error so pg destroys it instead of pooling it. Then end the pool. Release no longer throws a second error that would hide the first.

Keeping traffic paused after a failed cutover is intended: the failure handler keeps the routing gate closed and the next run continues from there. This PR does not change that. It only replaces the crash with the normal failure path and a clear rerun message.

Not changed: platform-auth-recovery.ts opens its own short-lived lock connection. It is a separate command and is left for a follow-up if needed.

Tests

  • New test in packages/void/test/unit/platform-database-lock.test.ts: a lock client and pool that are EventEmitters. Emitting 'error' (ECONNRESET) no longer throws. Later verifyDatabase and readAdministratorSetup reject with the clear platform: message. Release resolves, sends no SQL, and destroys the client. The test fails on main with the unhandled 'error' event.
  • Updated the pg mocks in platform-database-lock.test.ts and platform-database-ssl.test.ts to accept on().
  • Ran the exact test file, vp check (no errors outside playground .void/tsconfig.json files that only exist after a playground build), and vp run test. All packages/* groups pass. The platform groups passed or timed out only while the test machine was put to sleep (about 980 s gaps in the power log); the platform integration tests that use this lock (zero-trust-uninstall, platform-bootstrap) pass when run alone. CI runs the full suite.

🤖 Generated with Claude Code

…rade

The PostgreSQL lifecycle lock holds one pg client for the whole lifecycle
command. It had no 'error' listener, so a server reset while Workers
deployed (read ECONNRESET) became an unhandled 'error' event and killed
the CLI before its failure handler ran.

Listen for errors on the pool and the lock client. Later lock operations
fail with a clear "connection was closed, rerun" message, and release
skips the unlock (the server already ended the session lock) and
destroys the broken client.

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:52:56.853486Z 076ea8a 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.

@cloudflare-workers-and-pages

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-upgrade-pg-error-void-cloud.voidzero-docs.workers.dev (commit 076ea8a)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://1d1ba1b2-void-cloud.voidzero-docs.workers.dev 076ea8a 2026-11-23T22:52:56.349Z Visit the dashboard ↗

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