Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): refuse startup when another server owns the state dir #4420

Closedcolonelpanic8(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/4420/headopened View on GitHub

What Changed

  • The server now reads any existing server-runtime.json before binding or writing its own, and refuses startup with a typed ServerStateDirConflictError when the recorded pid is a live process. Missing, corrupt, stale (dead-pid), or own-pid discovery files proceed exactly as before. The decision logic is a pure, unit-tested function; the guard is documented as best-effort (it does not close the simultaneous-start race).
  • Added PRAGMA busy_timeout = 5000 alongside the existing WAL/foreign_keys pragmas.

Why

Nothing prevents two backend processes from sharing one state dir today. The discovery file is clobbered unconditionally by whichever server starts last, and with no busy_timeout, concurrent writers on state.sqlite fail with SQLITE_BUSY immediately instead of waiting out short write locks. Running e.g. a standalone t3 serve and a desktop-spawned backend against the same ~/.t3 silently corrupts shared state; failing loudly with a clear, actionable error (naming the pid, origin, and state dir, and suggesting --base-dir / T3CODE_HOME) is strictly better. The existing readers of server-runtime.json (t3 connect, t3 project) are pure HTTP clients and are unaffected.

Validation:

  • vp test run src/serverRuntimeState.test.ts — 12 passed (7 new: pure decision matrix + typed-error guard behavior)
  • apps/server typecheck (tsgo --noEmit) — clean
  • vp lint on changed files — clean

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (N/A)
  • I included a video for animation/interaction changes (N/A)

Note

Medium Risk Changes early server startup and shared SQLite/discovery behavior; mis-PID or race edge cases could block or briefly delay startup, but scope is limited and well tested.

Overview Prevents two T3 server processes from sharing one state directory by checking server-runtime.json before HTTP bind or discovery writes. A live recorded PID yields a typed ServerStateDirConflictError (pid, port, origin, state dir, --base-dir / T3CODE_HOME hint); missing, corrupt, stale, or own-PID files still allow startup. Decision logic is pure (decideServerRuntimeStartup + isPidAlive); the guard is best-effort for simultaneous starts.

Startup read path fails closed on unreadable discovery files while decode errors stay warn-and-proceed for normal readers. SQLite init adds PRAGMA busy_timeout = 5000 to wait on brief lock contention.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 4e30781c6840f12057e5748da8bb0759179f77ea. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Refuse server startup when another live server owns the state directory

  • Adds ensureExclusiveStateDir in serverRuntimeState.ts, which reads the persisted runtime state on startup and fails with a typed ServerStateDirConflictError (carrying pid, port, origin, stateDir) if another live process owns the state directory.
  • Invokes ensureExclusiveStateDir early in server.ts startup, before HTTP binding, so conflicts are caught before any resources are acquired.
  • Corrupt or missing discovery files are tolerated (logged and ignored); unreadable files propagate as a ServerRuntimeStateError.
  • Sets SQLite busy_timeout to 5000ms in Sqlite.ts to reduce lock contention during startup.
  • Behavioral Change: servers sharing a state directory will now fail fast with a descriptive error instead of silently conflicting.
Macroscope summarized 4e30781.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: 629202ca-0f3f-4203-b9fd-cfbee73450ea

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Needs human review

      1 blocking correctness issue found. This PR introduces new server startup behavior (single-instance guard) that can block startup when a conflict is detected. There's an unresolved review comment about potential false conflicts due to PID reuse after unclean shutdown. Both the behavioral scope and the open correctness concern warrant human review.

      You can customize Macroscope's approvability policy. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).

Sign in to join this conversation.