fix(server): enforce one owner per state directory #8960
Closedmaxibotstef(GitHub profile, opens in a new tab)wants to merge 11 commits into
main fromrefs/pull/8960/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/serverSingleton.ts line 328Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +293,21 @@293+ const holder = yield* readHolder(lockPath);294+ if (holder === undefined || holder.pid !== process.pid) return;295+ yield* writeFileStringAtomically({296+ filePath: lockPath,297+ contents: encodeHolder({ ...holder, port }),298+ }).pipe(Effect.ignore);299+});300+301+/** Holds the state directory until the caller's scope closes. */302+export const acquireServerSingleton = Effect.fn("serverSingleton.acquire")(function* (303+ stateDir: string,304+) {305+ const legacyRuntimeStatePath = yield* legacyServerRuntimeStatePath(stateDir);306+ return yield* Effect.acquireRelease(307+ acquireLock(stateDir).pipe(308+ Effect.catchTags({309+ LiveLegacyServerRuntime: (legacy) =>310+ Effect.fail(311+ new ServerAlreadyRunningError({312+ stateDir,313+ lockPath: legacyRuntimeStatePath,
- @@ -0,0 +225,21 @@225+) {226+ const path = yield* Path.Path;227+ return path.join(stateDir, SERVER_LOCK_DATABASE_FILENAME);228+});229+230+export const legacyServerRuntimeStatePath = Effect.fn("serverSingleton.legacyStatePath")(function* (231+ stateDir: string,232+) {233+ const path = yield* Path.Path;234+ return path.join(stateDir, SERVER_RUNTIME_STATE_FILENAME);235+});236+237+const acquireLock = Effect.fn("serverSingleton.acquireLock")(function* (stateDir: string) {238+ const fs = yield* FileSystem.FileSystem;239+ const path = yield* Path.Path;240+ const crypto = yield* Crypto.Crypto;241+ const lockPath = yield* serverLockPath(stateDir);242+ const databasePath = yield* serverLockDatabasePath(stateDir);243+ const legacyRuntimeStatePath = yield* legacyServerRuntimeStatePath(stateDir);244+245+ const legacyState = yield* readPersistedServerRuntimeState(legacyRuntimeStatePath);@@ -0,0 +226,21 @@226+ const path = yield* Path.Path;227+ return path.join(stateDir, SERVER_LOCK_DATABASE_FILENAME);228+});229+230+export const legacyServerRuntimeStatePath = Effect.fn("serverSingleton.legacyStatePath")(function* (231+ stateDir: string,232+) {233+ const path = yield* Path.Path;234+ return path.join(stateDir, SERVER_RUNTIME_STATE_FILENAME);235+});236+237+const acquireLock = Effect.fn("serverSingleton.acquireLock")(function* (stateDir: string) {238+ const fs = yield* FileSystem.FileSystem;239+ const path = yield* Path.Path;240+ const crypto = yield* Crypto.Crypto;241+ const lockPath = yield* serverLockPath(stateDir);242+ const databasePath = yield* serverLockDatabasePath(stateDir);243+ const legacyRuntimeStatePath = yield* legacyServerRuntimeStatePath(stateDir);244+245+ const legacyState = yield* readPersistedServerRuntimeState(legacyRuntimeStatePath);246+ if (Option.isSome(legacyState) && processIsAlive(legacyState.value.pid)) {
- apps/server/src/serverSingleton.ts line 263Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +228,21 @@228+});229+230+export const legacyServerRuntimeStatePath = Effect.fn("serverSingleton.legacyStatePath")(function* (231+ stateDir: string,232+) {233+ const path = yield* Path.Path;234+ return path.join(stateDir, SERVER_RUNTIME_STATE_FILENAME);235+});236+237+const acquireLock = Effect.fn("serverSingleton.acquireLock")(function* (stateDir: string) {238+ const fs = yield* FileSystem.FileSystem;239+ const path = yield* Path.Path;240+ const crypto = yield* Crypto.Crypto;241+ const lockPath = yield* serverLockPath(stateDir);242+ const databasePath = yield* serverLockDatabasePath(stateDir);243+ const legacyRuntimeStatePath = yield* legacyServerRuntimeStatePath(stateDir);244+245+ const legacyState = yield* readPersistedServerRuntimeState(legacyRuntimeStatePath);246+ if (Option.isSome(legacyState) && processIsAlive(legacyState.value.pid)) {247+ return yield* new LiveLegacyServerRuntime({ state: legacyState.value });248+ }
- @@ -0,0 +227,21 @@227+ return path.join(stateDir, SERVER_LOCK_DATABASE_FILENAME);228+});229+230+export const legacyServerRuntimeStatePath = Effect.fn("serverSingleton.legacyStatePath")(function* (231+ stateDir: string,232+) {233+ const path = yield* Path.Path;234+ return path.join(stateDir, SERVER_RUNTIME_STATE_FILENAME);235+});236+237+const acquireLock = Effect.fn("serverSingleton.acquireLock")(function* (stateDir: string) {238+ const fs = yield* FileSystem.FileSystem;239+ const path = yield* Path.Path;240+ const crypto = yield* Crypto.Crypto;241+ const lockPath = yield* serverLockPath(stateDir);242+ const databasePath = yield* serverLockDatabasePath(stateDir);243+ const legacyRuntimeStatePath = yield* legacyServerRuntimeStatePath(stateDir);244+245+ const legacyState = yield* readPersistedServerRuntimeState(legacyRuntimeStatePath);246+ if (Option.isSome(legacyState) && processIsAlive(legacyState.value.pid)) {247+ return yield* new LiveLegacyServerRuntime({ state: legacyState.value });
- @@ -0,0 +64,21 @@64+ ? `pid ${this.holderPid}`65+ : `pid ${this.holderPid}, listening on port ${this.holderPort}`;66+ const common = [67+ "Another T3 Code server is already using this data directory.",68+ "",69+ ` data directory: ${this.stateDir}`,70+ ` held by: ${holder}`,71+ ...(this.holderStartedAt === undefined ? [] : [` since: ${this.holderStartedAt}`]),72+ "",73+ "Two servers sharing one data directory overwrite each other's state.sqlite",74+ "and settings.json. Connect to the running server, stop it cleanly, or use",75+ "a different --base-dir.",76+ ];77+ if (this.legacyRuntimeStatePath !== undefined) {78+ return [79+ ...common,80+ "",81+ `${this.legacyRuntimeStatePath} is the compatibility guard for this older`,82+ "server. Do not remove it while the recorded process is live.",83+ ].join("\n");84+ }
maxibotstef(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Team
Run ID:
a8b51200-506e-4700-a494-e84148b1db19📥 Commits
Reviewing files that changed from the base of the PR and between 0197d8e88d1382e0915063e76fce5059b4a837e9 and 70b3f7d08f1c5fff810621d34e56d5d6be778163.
📒 Files selected for processing (2)
apps/server/src/serverSingleton.test.tsapps/server/src/serverSingleton.ts🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
Changes#
The server now claims its configured data directory with a SQLite-backed singleton before opening persistence or binding HTTP. It records the bound port in lock metadata and rejects competing or live legacy server processes.
Server data-directory ownership
apps/server/src/serverSingleton.tsapps/server/src/server.tsapps/server/src/serverSingleton.test.tsEstimated code review effort: 4 (Complex) | ~45 minutes
Merge Risk: ⚪ Minimal · up to
70b3fThe change enforces single ownership of each state directory and no actionable merge-blocking risk remains based on the supplied evidence; it is merge-ready after normal checks and review.
Sequence Diagram(s)#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#6097] by claiming state-directory ownership before HTTP binding or persistent-state access, refusing concurrent owners, handling stale metadata, and supp…#6097] and do not introduce unrelated functiona…Full details: Linked Issues check
Explanation
The changes satisfy the primary objective in [
#6097] by claiming state-directory ownership before HTTP binding or persistent-state access, refusing concurrent owners, handling stale metadata, and supporting live pre-lock server detection. Desktop attach behavior and fallback-port handling are explicitly outside this PR's scope.Full details: Out of Scope Changes check
Explanation
The changes are focused on server singleton ownership, compatibility detection, metadata handling, and regression tests. These changes directly support [
#6097] and do not introduce unrelated functionality.Full details: Docstring Coverage
Explanation
No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 3 files.
Full details: Description check
Explanation
The description clearly explains the problem, the SQLite-based fix, compatibility behavior, verification steps, and scope. It does not use the template headings exactly and omits the checkbox checklist, but it provides the required information and covers UI applicability.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.