Skip to content

Commit cb82379

Browse files
ZJKungLi-Chieh Kungclaude
authored
Handle Postgres pool errors instead of crashing (#19)
An idle client disconnected by a database restart or failover makes pg.Pool emit `error`; with no listener, Node throws and the API and worker processes exit. Register a listener that logs via backgroundFailure (error name only, no connection details). Co-authored-by: Li-Chieh Kung <lichieh@patagoniahealth.net> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent bb7ce4e commit cb82379

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

‎apps/server/src/db.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { mkdir } from "node:fs/promises";
22
import { dirname } from "node:path";
33
import { PGlite } from "@electric-sql/pglite";
44
import pg from "pg";
5+
import { backgroundFailure } from "./log.ts";
56

67
type Row = { data: Record<string, unknown> };
78
interface Database {
@@ -111,12 +112,19 @@ export class Store {
111112
}
112113
}
113114

115+
/** Idle clients can be disconnected by a database restart; without a listener pg's `error` event crashes the process. */
116+
export function createPool(connectionString: string) {
117+
const pool = new pg.Pool({ connectionString, max: 5 });
118+
pool.on("error", (error) => backgroundFailure("postgres pool", error));
119+
return pool;
120+
}
121+
114122
export async function createStore(
115123
options: { dataDir?: string; databaseUrl?: string } = {},
116124
): Promise<Store> {
117125
let database: Database;
118126
if (options.databaseUrl) {
119-
const pool = new pg.Pool({ connectionString: options.databaseUrl, max: 5 });
127+
const pool = createPool(options.databaseUrl);
120128
database = { query: async (sql, params) => pool.query(sql, params), close: () => pool.end() };
121129
} else {
122130
if (options.dataDir) await mkdir(dirname(options.dataDir), { recursive: true, mode: 0o700 });

‎tests/persistence.test.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { mkdtemp, rm } from "node:fs/promises";
33
import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { test } from "node:test";
6-
import { createStore } from "../apps/server/src/db.ts";
6+
import { createPool, createStore } from "../apps/server/src/db.ts";
77

88
test("fresh nested data directory starts and survives a database restart", async () => {
99
const root = await mkdtemp(join(tmpdir(), "openmuse-db-"));
@@ -20,3 +20,14 @@ test("fresh nested data directory starts and survives a database restart", async
2020
await rm(root, { recursive: true, force: true });
2121
}
2222
});
23+
24+
test("idle Postgres client errors are logged instead of crashing the process", async (t) => {
25+
const logged = t.mock.method(console, "error", () => {});
26+
const pool = createPool("postgres://127.0.0.1:1/openmuse");
27+
try {
28+
assert.doesNotThrow(() => pool.emit("error", new Error("terminating connection")));
29+
assert.equal(logged.mock.callCount(), 1);
30+
} finally {
31+
await pool.end();
32+
}
33+
});

0 commit comments

Comments
 (0)