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): initialize again a native PostgreSQL that never started

jeffreycc18e080ae0f
diff --git a/packages/local-installation/AGENTS.md b/packages/local-installation/AGENTS.md
index f4678edb..86f39680 100644
--- a/packages/local-installation/AGENTS.md
+++ b/packages/local-installation/AGENTS.md
@@ -43,6 +43,12 @@ error naming each failed instance, and the installer does not start. The
cache markers `.simplex-files.json` and `.simplex-model-package.json` keep
their names.
+Native PostgreSQL runs `initdb` in `native/postgres-data.partial` and moves
+it to `native/postgres-data` only once it succeeds, so a killed `initdb`
+never leaves a directory that later starts skip. A `postgres-data` with
+`PG_VERSION` but no `postmaster.opts` (which only a started server writes)
+never served a connection, holds no data, and is initialized again.
+
Move a freshly downloaded or unpacked file or folder into place with
`renameRetrying` from `fs-retry.ts`: on Windows antivirus and indexing hold
new files for a moment, and a plain rename then fails with EPERM.
diff --git a/packages/local-installation/src/native-database.test.ts b/packages/local-installation/src/native-database.test.ts
index ece62d81..87916436 100644
--- a/packages/local-installation/src/native-database.test.ts
+++ b/packages/local-installation/src/native-database.test.ts
@@ -15,8 +15,10 @@ vi.mock("./instance.js", () => ({
atomicPrivate: vi.fn(async () => {}),
readPrivate: vi.fn(async () => "LINGER_POSTGRES_PASSWORD=secret\n"),
}));
+import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
import path from "node:path";
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
postgresProgram,
prepareNativeDatabase,
@@ -144,6 +146,12 @@ describe("prepareNativeDatabase initdb", () => {
expect(vi.mocked(renameRetrying)).toHaveBeenCalledWith(pending, data, {
signal: options.signal,
});
+ const initdb = run.mock.calls.findIndex(
+ ([command]) => path.basename(command) === "initdb",
+ );
+ expect(
+ vi.mocked(removeRetrying).mock.invocationCallOrder[1],
+ ).toBeGreaterThan(run.mock.invocationCallOrder[initdb]!);
});
// A killed initdb has already written PG_VERSION; publishing it would make
@@ -161,4 +169,40 @@ describe("prepareNativeDatabase initdb", () => {
expect(renameRetrying).not.toHaveBeenCalled();
expect(startNativeProcess).not.toHaveBeenCalled();
});
+
+ describe("with a PG_VERSION already present", () => {
+ let root: string;
+ beforeEach(async () => {
+ root = await mkdtemp(path.join(tmpdir(), "native-database-"));
+ await mkdir(path.join(root, "native/postgres-data"), { recursive: true });
+ await writeFile(
+ path.join(root, "native/postgres-data/PG_VERSION"),
+ "17\n",
+ );
+ run.mockResolvedValue(ok());
+ });
+ afterEach(() => rm(root, { recursive: true, force: true }));
+ const prepare = () =>
+ prepareNativeDatabase({ ...options, instance: { ...instance, root } });
+
+ it("keeps a cluster that has served", async () => {
+ await writeFile(
+ path.join(root, "native/postgres-data/postmaster.opts"),
+ "",
+ );
+ await prepare();
+ expect(pgdata()).toEqual([]);
+ expect(renameRetrying).not.toHaveBeenCalled();
+ });
+
+ // A cluster no postmaster ever started holds no data, and one left by a
+ // killed initdb would otherwise fail every start.
+ it("makes again one that never started", async () => {
+ await prepare();
+ expect(pgdata()).toEqual([
+ path.join(root, "native/postgres-data.partial"),
+ ]);
+ expect(renameRetrying).toHaveBeenCalled();
+ });
+ });
});
diff --git a/packages/local-installation/src/native-database.ts b/packages/local-installation/src/native-database.ts
index a830e44a..b07b3f86 100644
--- a/packages/local-installation/src/native-database.ts
+++ b/packages/local-installation/src/native-database.ts
@@ -78,7 +78,17 @@ export async function prepareNativeDatabase(options: {
throw new Error(
"This native instance contains an unsupported PostgreSQL data directory.",
);
- initialized = true;
+ // Only a started postmaster writes postmaster.opts; initdb does not. A
+ // directory without it never served a connection and holds no data, as
+ // when an earlier initdb was killed after writing PG_VERSION, so it is
+ // made again rather than left to fail every start.
+ initialized = await lstat(path.join(data, "postmaster.opts")).then(
+ () => true,
+ (error: NodeJS.ErrnoException) => {
+ if (error.code !== "ENOENT") throw error;
+ return false;
+ },
+ );
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
}
@@ -97,10 +107,9 @@ export async function prepareNativeDatabase(options: {
return result;
};
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.
+ // A forced kill (Windows ends the tree on timeout or cancel) leaves
+ // initdb no time to clean up, so it builds aside and its directory
+ // becomes the data directory only once it succeeded.
const pending = `${data}.partial`;
await removeRetrying(pending);
await privateDirectory(pending);