fix(reporter): filter Prisma P2037 pool-exhaustion as transient (BIT-33537)
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 6m49s
ci / typescript (pull_request) Successful in 13m29s

Prisma surfaces PostgreSQL's 53300 (too many clients) as P2037 with the
message 'Too many database connections opened: sorry, too many clients
already' — no SQLSTATE in the text. The existing isTransientDatabaseError
filter checked for 53300 in the message and P1001/P1017 by code, so P2037
slipped through and was reported to errors.bitreport.pt on every API
request that hit pool exhaustion, tripping the Error Loop.

Add P2037 to the code check and 'too many clients already' to the regex
so the reporter stays quiet for this transient condition, consistent with
the 53300 filter added in BIT-33548.

Paperclip-Issue: BIT-33537
This commit is contained in:
paula-alves committed 2026-12-04 10:18:56 +00:00
1 parent 1e3cee8612
commit 1c5bfce97f
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");