fix(develop): treat 53300 pool exhaustion as transient in render poll (BIT-33549)
ci / typescript (push) Skipped
ci / rust (push) Skipped
ci / rust (pull_request) Successful in 17m2s
ci / typescript (pull_request) Successful in 21m14s

The RenderWorker tick only checked isPostgresAdminShutdown (57P01) and
logged everything else at warn, so a $queryRaw hitting PostgreSQL's
max_connections limit (P2010 wrapping SQLSTATE 53300 'sorry, too many
clients already') paged the error loop on every tick.

Extend isPrismaTooManyConnections to also match the P2010/53300
raw-query variant, and add a branch in the tick handler that logs at
info and lets the next scheduled tick retry once a slot frees — same
pattern as the 57P01 admin-shutdown handling.

Paperclip-Issue: BIT-33549
This commit is contained in:
nelson-baptista committed 2026-12-04 12:20:55 +00:00
1 parent 3c3a6c084f
commit 2d5f898f60
3 files changed
+47 -6

No files matched your search

+37 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { RenderWorker, type DevelopService } from "./service.js";
import { isPostgresAdminShutdown } from "../lib/pg.js";
import { isPostgresAdminShutdown, isPrismaTooManyConnections } from "../lib/pg.js";
import type { IngestLogger } from "../ingest/service.js";
describe("isPostgresAdminShutdown", () => {
@@ -18,6 +18,25 @@ describe("isPostgresAdminShutdown", () => {
});
});
describe("isPrismaTooManyConnections", () => {
it("matches the P2010 raw-query variant wrapping SQLSTATE 53300 (BIT-33549)", () => {
const err = new Error(
"Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `53300`. Message: `sorry, too many clients already`",
);
expect(isPrismaTooManyConnections(err)).toBe(true);
});
it("matches the P2037 variant", () => {
const err = new Error("Too many database connections opened: sorry, too many clients already");
expect(isPrismaTooManyConnections(err)).toBe(true);
});
it("does not match an unrelated query error", () => {
expect(isPrismaTooManyConnections(new Error("relation \"photos\" does not exist"))).toBe(false);
expect(isPrismaTooManyConnections(null)).toBe(false);
});
});
describe("RenderWorker poll error handling", () => {
type Level = "info" | "warn";
const makeLog = (): IngestLogger & { calls: { level: Level; msg: string }[] } => {
@@ -51,6 +70,23 @@ describe("RenderWorker poll error handling", () => {
expect(log.calls.some((c) => c.level === "warn" && /poll failed/.test(c.msg))).toBe(false);
});
it("logs a 53300 pool-exhaustion error at info, not warn (BIT-33549)", async () => {
const log = makeLog();
const worker = new RenderWorker(
makeService(
new Error(
"Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `53300`. Message: `sorry, too many clients already`",
),
) as DevelopService,
{ concurrency: 1, intervalMs: 60_000, log },
);
worker.start();
await new Promise((r) => setTimeout(r, 20));
await worker.stop();
expect(log.calls.some((c) => c.level === "info" && /too many database connections/.test(c.msg))).toBe(true);
expect(log.calls.some((c) => c.level === "warn" && /poll failed/.test(c.msg))).toBe(false);
});
it("still warns on a real poll failure", async () => {
const log = makeLog();
const worker = new RenderWorker(makeService(new Error("relation photos does not exist")) as DevelopService, {
+4 -1
View File
@@ -3,7 +3,7 @@ import type { Db } from "../db.js";
import { Prisma, type PhotoEdit } from "../generated/prisma/client.js";
import type { IngestLogger } from "../ingest/service.js";
import { waitUntil } from "../lib/idle.js";
import { isPostgresAdminShutdown } from "../lib/pg.js";
import { isPostgresAdminShutdown, isPrismaTooManyConnections } from "../lib/pg.js";
import { photoUrls } from "../library/serialize.js";
import type { Broadcaster } from "../realtime.js";
import { type LocalStorage, type UrlSigner, VARIANT_SIZE } from "../storage/storage.js";
@@ -223,6 +223,9 @@ export class RenderWorker {
// A DB restart or `pg_terminate_backend` (57P01) takes the pooled client mid-`$queryRaw`; the next tick
// re-claims a fresh one, so log at info and let the loop continue instead of paging the error loop (BIT-32449).
if (isPostgresAdminShutdown(err)) this.opts.log.info({ err: (err as Error).message }, "develop: poll skipped, db connection was reset");
// Pool exhaustion (P2037 or P2010 wrapping SQLSTATE 53300 "too many clients already"): every remaining
// query would fail the same way, so skip this tick and let the next one retry once a slot frees (BIT-33549).
else if (isPrismaTooManyConnections(err)) this.opts.log.info({ err: (err as Error).message }, "develop: poll skipped, too many database connections");
else this.opts.log.warn({ err }, "develop: poll failed");
} finally {
this.ticking = false;
+6 -4
View File
@@ -42,14 +42,16 @@ export function isPrismaServerUnreachable(err: unknown): boolean {
/**
* The Postgres server has no free connection slots for non-superuser roles — every slot is in use and the
* remaining ones are reserved for SUPERUSER. Prisma surfaces this as `PrismaClientKnownRequestError` with
* code `P2037` and message "Too many database connections opened: …". This is pool exhaustion under concurrent
* worker load; the next scheduled tick reclaims a freed slot, so it is transient and must not page the error
* loop — the caller should stop the current batch and let the next run retry (BIT-33538).
* code `P2037` and message "Too many database connections opened: …", or as `P2010` wrapping the raw
* SQLSTATE `53300` ("sorry, too many clients already") when a `$queryRaw` hits the limit. This is pool
* exhaustion under concurrent worker load; the next scheduled tick reclaims a freed slot, so it is
* transient and must not page the error loop — the caller should stop the current batch and let the next
* run retry (BIT-33538, BIT-33549 for the P2010/53300 raw-query variant).
*/
export function isPrismaTooManyConnections(err: unknown): boolean {
if (!(err instanceof Error)) return false;
const code = (err as { code?: unknown }).code;
return code === "P2037" || /Too many database connections opened/i.test(err.message);
return code === "P2037" || /Too many database connections opened|sorry, too many clients already|\b53300\b/i.test(err.message);
}
/**