Stop the realtime error loop on Postgres ECONNREFUSED (BIT-33524)
ci / rust (push) Successful in 6m12s
ci / typescript (push) Successful in 11m49s

The realtime LISTEN/NOTIFY path uses pg.Client/pg.Pool directly (not
Prisma). When Postgres is completely down, Node's TCP layer throws
Error: connect ECONNREFUSED 127.0.0.1:5432 with code ECONNREFUSED.
The realtime listener already retries every 1 s (realtime.ts), but
each failed attempt was logged as 'realtime error' and reported to
errors.bitreport.pt because isTransientDatabaseError only filtered
Prisma codes (P1001/P1017) and SQLSTATE messages (57P01/57P03/53300),
not the raw Node TCP error.

Add ECONNREFUSED to the transient filter so a completely unreachable
Postgres does not trip the Error Loop with one-off incidents from
every realtime reconnect attempt.

Paperclip-Issue: BIT-33524
This commit is contained in:
paula-alves committed 2026-12-04 09:23:08 +00:00
1 parent 465937212c
commit 891c7f23ba
2 files changed
+12 -9

No files matched your search

+9 -8
View File
@@ -12,18 +12,19 @@ import { createHash } from "node:crypto";
/**
* Transient PostgreSQL connection errors during a restart, graceful shutdown, network blip, or pool
* exhaustion. Prisma surfaces these as `PrismaClientKnownRequestError` wrapping the raw SQLSTATE
* (57P01/57P03/53300) or its own P1001/P1017. The pool reconnects on the next attempt, so these are not
* code bugs — reporting them only trips the Error Loop with one-off incidents from every code path that
* touches the database during the brief outage. Individual call sites that need to stop a batch or retry
* still check `isPostgresAdminShutdown` / `isPrismaServerUnreachable` (lib/pg.ts); this filter keeps the
* hub quiet for the paths that just let the error propagate to a generic log.error (BIT-32490 and 11
* sibling clusters, BIT-33548 for 53300).
* (57P01/57P03/53300) or its own P1001/P1017. The realtime LISTEN/NOTIFY path uses `pg` directly and
* surfaces raw Node TCP errors (ECONNREFUSED) when Postgres is not listening at all. The pool reconnects
* on the next attempt, so these are not code bugs — reporting them only trips the Error Loop with one-off
* incidents from every code path that touches the database during the brief outage. Individual call sites
* that need to stop a batch or retry still check `isPostgresAdminShutdown` / `isPrismaServerUnreachable`
* (lib/pg.ts); this filter keeps the hub quiet for the paths that just let the error propagate to a
* generic log.error (BIT-32490 and 11 sibling clusters, BIT-33548 for 53300, BIT-33524 for ECONNREFUSED).
*/
function isTransientDatabaseError(err: unknown): boolean {
if (!(err instanceof Error)) return false;
const code = (err as { code?: unknown }).code;
if (code === "P1001" || code === "P1017") return true;
return /57P01|57P03|53300|remaining connection slots are reserved|the database system is shutting down|the database system is starting up|Can't reach database server|Server has closed the connection/i.test(err.message);
if (code === "P1001" || code === "P1017" || code === "ECONNREFUSED") return true;
return /57P01|57P03|53300|remaining connection slots are reserved|the database system is shutting down|the database system is starting up|Can't reach database server|Server has closed the connection|ECONNREFUSED/i.test(err.message);
}
export type ReportLevel = "critical" | "error" | "warning" | "info";
+3 -1
View File
@@ -91,7 +91,7 @@ describe("errors to errors.bitreport.pt", () => {
expect(events[0]!.context).toMatchObject({ log_message: "archive: image not indexed", fileId: "f2" });
});
it("does not report transient PostgreSQL shutdown/restart errors (57P01/57P03/P1001/P1017)", async () => {
it("does not report transient PostgreSQL shutdown/restart/unreachable errors (57P01/57P03/53300/P1001/P1017/ECONNREFUSED)", async () => {
const hub = fakeHub();
const reporter = reporterWith(hub.fetchImpl);
const sink = new Writable({ write: (_c, _e, cb) => cb() });
@@ -106,7 +106,9 @@ describe("errors to errors.bitreport.pt", () => {
log.error({ err: mk("Can't reach database server at localhost:5432", "P1001") }, "db unreachable");
log.error({ err: mk("Server has closed the connection.", "P1017") }, "connection closed");
log.error({ err: mk("Raw query failed. Code: `53300`. Message: `remaining connection slots are reserved for roles with the SUPERUSER attribute`", "P2010") }, "health: query failed");
log.error({ err: mk("connect ECONNREFUSED 127.0.0.1:5432", "ECONNREFUSED") }, "realtime error");
reporter.reportError(mk("Database error. Code: 57P03. Message: the database system is shutting down"), "worker");
reporter.reportError(mk("connect ECONNREFUSED 127.0.0.1:5432", "ECONNREFUSED"), "api");
log.error({ err: new Error("render crashed") }, "real error");
await reporter.flush();
const events = hub.sent.flatMap((s) => s.events);