Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(local-installation): publish the native PostgreSQL data only after initdb succeeds

jeffreydd2681229bb2
diff --git a/packages/local-installation/src/native-database.test.ts b/packages/local-installation/src/native-database.test.ts
index 5aee4f45..ece62d81 100644
--- a/packages/local-installation/src/native-database.test.ts
+++ b/packages/local-installation/src/native-database.test.ts
@@ -5,6 +5,11 @@ vi.mock("./native-process.js", () => ({
inspectNativeProcess: vi.fn(async () => ({ running: true })),
startNativeProcess: vi.fn(async () => {}),
}));
+import { removeRetrying, renameRetrying } from "./fs-retry.js";
+vi.mock("./fs-retry.js", () => ({
+ removeRetrying: vi.fn(async () => {}),
+ renameRetrying: vi.fn(async () => {}),
+}));
vi.mock("./instance.js", () => ({
privateDirectory: vi.fn(async () => {}),
atomicPrivate: vi.fn(async () => {}),
@@ -122,3 +127,38 @@ describe("prepareNativeDatabase on Windows", () => {
expect(startNativeProcess).not.toHaveBeenCalled();
});
});
+
+describe("prepareNativeDatabase initdb", () => {
+ const data = path.join(instance.root, "native/postgres-data");
+ const pending = `${data}.partial`;
+ const pgdata = () =>
+ run.mock.calls
+ .filter(([command]) => path.basename(command) === "initdb")
+ .map(([, args]) => args[args.indexOf("--pgdata") + 1]);
+
+ it("builds the data directory aside and moves it in once initdb succeeds", async () => {
+ run.mockResolvedValue(ok());
+ await prepareNativeDatabase(options);
+ expect(pgdata()).toEqual([pending]);
+ expect(vi.mocked(removeRetrying).mock.calls).toEqual([[pending], [data]]);
+ expect(vi.mocked(renameRetrying)).toHaveBeenCalledWith(pending, data, {
+ signal: options.signal,
+ });
+ });
+
+ // A killed initdb has already written PG_VERSION; publishing it would make
+ // every later attempt skip initdb and fail on an incomplete cluster.
+ it("leaves the data directory alone when initdb fails", async () => {
+ run.mockImplementation(async (command) =>
+ path.basename(command) === "initdb"
+ ? { code: 1, stdout: "", stderr: "" }
+ : ok(),
+ );
+ await expect(prepareNativeDatabase(options)).rejects.toThrow(
+ /initdb failed/,
+ );
+ expect(vi.mocked(removeRetrying).mock.calls).toEqual([[pending]]);
+ expect(renameRetrying).not.toHaveBeenCalled();
+ expect(startNativeProcess).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/local-installation/src/native-database.ts b/packages/local-installation/src/native-database.ts
index 255c957a..a830e44a 100644
--- a/packages/local-installation/src/native-database.ts
+++ b/packages/local-installation/src/native-database.ts
@@ -7,6 +7,7 @@ import {
readPrivate,
type Instance,
} from "./instance.js";
+import { removeRetrying, renameRetrying } from "./fs-retry.js";
import { inspectNativeProcess, startNativeProcess } from "./native-process.js";
import { runProcess } from "./process.js";
import { parseEnv } from "node:util";
@@ -95,22 +96,37 @@ export async function prepareNativeDatabase(options: {
);
return result;
};
- if (!initialized)
- await command("initdb", [
- "--pgdata",
- data,
- "--username",
- "simplexai",
- "--pwfile",
- passwordFile,
- "--auth-host",
- "scram-sha-256",
- "--auth-local",
- "scram-sha-256",
- "--encoding",
- "UTF8",
- "--no-locale",
- ]);
+ if (!initialized) {
+ // initdb writes PG_VERSION long before it finishes, and a forced kill
+ // (Windows ends the tree on timeout or cancel) leaves no cleanup: such a
+ // directory would pass the check above on every retry. It is built aside
+ // and becomes the data directory only once initdb succeeded.
+ const pending = `${data}.partial`;
+ await removeRetrying(pending);
+ await privateDirectory(pending);
+ await command(
+ "initdb",
+ [
+ "--pgdata",
+ pending,
+ "--username",
+ "simplexai",
+ "--pwfile",
+ passwordFile,
+ "--auth-host",
+ "scram-sha-256",
+ "--auth-local",
+ "scram-sha-256",
+ "--encoding",
+ "UTF8",
+ "--no-locale",
+ ],
+ // Antivirus scanning of the new files can make it take minutes.
+ 10 * 60_000,
+ );
+ await removeRetrying(data);
+ await renameRetrying(pending, data, { signal });
+ }
await startNativeProcess({
root: instance.root,
name: "postgres",