fix(reporter): filter Prisma P2037 pool-exhaustion as transient (BIT-33537) (#325)
ci / rust (push) Failing after 6s
ci / typescript (push) Successful in 9m50s

This commit was merged in pull request #325.
This commit is contained in:
jos-alves committed 2026-12-04 10:43:49 +00:00
1 parent 1e3cee8612
commit ea29bd9b6e
2 files changed
+9 -5

No files matched your search

+7 -4
View File
@@ -12,19 +12,22 @@ 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 realtime LISTEN/NOTIFY path uses `pg` directly and
* (57P01/57P03/53300) or its own P1001/P1017, and pool exhaustion (SQLSTATE 53300) as P2037 with the
* message "Too many database connections opened: sorry, too many clients already" — no SQLSTATE in the
* text, so the code check is what catches it. 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).
* generic log.error (BIT-32490 and 11 sibling clusters, BIT-33548 for 53300, BIT-33537 for P2037,
* 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" || 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);
if (code === "P1001" || code === "P1017" || code === "P2037" || code === "ECONNREFUSED") return true;
return /57P01|57P03|53300|remaining connection slots are reserved|too many clients already|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";
+2 -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/unreachable errors (57P01/57P03/53300/P1001/P1017/ECONNREFUSED)", async () => {
it("does not report transient PostgreSQL shutdown/restart/unreachable errors (57P01/57P03/53300/P1001/P1017/P2037/ECONNREFUSED)", async () => {
const hub = fakeHub();
const reporter = reporterWith(hub.fetchImpl);
const sink = new Writable({ write: (_c, _e, cb) => cb() });
@@ -106,6 +106,7 @@ 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("Invalid `prisma.cloudSource.findUnique()` invocation:\n\nToo many database connections opened: sorry, too many clients already", "P2037") }, "archive: viewer source lookup 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");