From ea29bd9b6ebea542264f92ffcf29ca5e31007624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alves?= Date: Fri, 4 Dec 2026 10:43:49 +0000 Subject: [PATCH] fix(reporter): filter Prisma P2037 pool-exhaustion as transient (BIT-33537) (#325) --- apps/api/src/lib/reporter.ts | 11 +++++++---- apps/api/test/reporter.test.ts | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/apps/api/src/lib/reporter.ts b/apps/api/src/lib/reporter.ts index cc229fa..24c2cd2 100644 --- a/apps/api/src/lib/reporter.ts +++ b/apps/api/src/lib/reporter.ts @@ -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"; diff --git a/apps/api/test/reporter.test.ts b/apps/api/test/reporter.test.ts index 0e8e7c0..9d085fe 100644 --- a/apps/api/test/reporter.test.ts +++ b/apps/api/test/reporter.test.ts @@ -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");