Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): refuse to start a second server against a live data directory #8442

ClosedNoahLinckeScout(GitHub profile, opens in a new tab)wants to merge 7 commits intomain fromrefs/pull/8442/headopened View on GitHub

Showing 3 changed files with 34 additions and 2 deletions (loading 2).

x
0 of 3 viewed, 3 left
34 additions and 2 deletionsapps/server/src/server.tsModified
View file
@@ -77,6 +77,7 @@
import * as EnvironmentTheme from "./environmentTheme.ts";
import * as Keybindings from "./keybindings.ts";
import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
+import * as ServerSingleton from "./serverSingleton.ts";
import { OrchestrationReactorLive } from "./orchestration/Layers/OrchestrationReactor.ts";
import { RuntimeReceiptBusLive } from "./orchestration/Layers/RuntimeReceiptBus.ts";
import { ProviderRuntimeIngestionLive } from "./orchestration/Layers/ProviderRuntimeIngestion.ts";
@@ -221,6 +222,22 @@
}),
​
+/**
+ * Claims the data directory before anything binds a port or opens the database.
+ *
+ * Provided into both `HttpServerLive` and `RuntimeDependenciesLive` (same layer
+ * identity, so one acquire) so the lock precedes the HTTP bind *and* SQLite
+ * open. Providing it only into the HTTP layer left persistence free to
+ * initialize in parallel and write `state.sqlite` before a second process
+ * refused.
+ */
+const ServerSingletonLive = Layer.effectDiscard(
+ Effect.gen(function* () {
+ const config = yield* ServerConfig.ServerConfig;
+ yield* ServerSingleton.acquireServerSingleton(config.stateDir);
+ }),
+);
+​
const HttpServerLive = Layer.unwrap(