fix(develop): treat 53300 pool exhaustion as transient in render poll (BIT-33549) #329
No files matched your search
@@ -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, {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user