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 into
main fromrefs/pull/4420/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/serverRuntimeState.ts line 149Resolvedby colonelpanic8(GitHub profile, opens in a new tab)@@ -102,0 +129,21 @@129+ */130+export const isPidAlive = (pid: number): boolean => {131+ try {132+ process.kill(pid, 0);133+ return true;134+ } catch (error) {135+ return (error as NodeJS.ErrnoException).code === "EPERM";136+ }137+};138+139+export type ServerRuntimeStartupDecision =140+ | { readonly _tag: "proceed" }141+ | { readonly _tag: "conflict"; readonly state: PersistedServerRuntimeState };142+143+/**144+ * Pure decision for whether it is safe to start a server against a state directory145+ * given whatever discovery file it currently holds. Proceed when the file is absent,146+ * when it records our own pid (a same-process restart path must not self-block), or147+ * when the recorded pid is dead (stale file). Otherwise report a conflict.148+ */149+export const decideServerRuntimeStartup = (input: {apps/server/src/serverRuntimeState.ts line 177Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -102,0 +157,21 @@157+ const state = input.existing.value;158+ if (state.pid === input.ownPid) {159+ return { _tag: "proceed" };160+ }161+ return input.isPidAlive(state.pid) ? { _tag: "conflict", state } : { _tag: "proceed" };162+};163+164+/**165+ * Best-effort single-instance guard on a state directory. Reads any existing166+ * discovery file and fails startup when a live process already owns it. This does167+ * NOT close the race between two servers that start simultaneously (both may read an168+ * empty/stale file before either writes) — it only rejects a startup when a169+ * previously-recorded, still-live server already owns the directory.170+ */171+export const ensureExclusiveStateDir = (input: {172+ readonly statePath: string;173+ readonly stateDir: string;174+ readonly ownPid?: number;175+ readonly isPidAlive?: (pid: number) => boolean;176+}) =>177+ Effect.gen(function* () {
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
629202ca-0f3f-4203-b9fd-cfbee73450eaYou can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
✨ Finishing Touches
🧪 Generate unit tests (beta)
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