From 4e093af85d0b723614f95440d80e94e064902574 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Wed, 18 Nov 2026 18:00:19 +0000 Subject: [PATCH 1/2] fix(develop): downgrade 57P01 admin-shutdown poll errors to info so they stop paging the error loop (BIT-32449) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A Postgres restart or `pg_terminate_backend` (error 57P01) takes the render worker's pooled client mid-`$queryRaw` in `DevelopService.claimRenders`. Prisma surfaces it as P2010 wrapping the server message, and the catch in `RenderWorker.start` logged it at warn as "develop: poll failed" — which fed the error loop on every tick during a DB restart. The next tick re-claims a fresh client, so this is a transient blip: detect 57P01 and log at info instead, keeping the warn path for real poll failures. Extract `isPostgresAdminShutdown` into `lib/pg.ts` so develop and autopilot share one definition. Paperclip-Issue: BIT-32449 Paperclip-Cluster: 8d12f6208533 --- apps/api/src/autopilot/service.ts | 12 ++--- apps/api/src/develop/service.test.ts | 66 ++++++++++++++++++++++++++++ apps/api/src/develop/service.ts | 6 ++- apps/api/src/lib/pg.ts | 9 ++++ 4 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 apps/api/src/develop/service.test.ts create mode 100644 apps/api/src/lib/pg.ts diff --git a/apps/api/src/autopilot/service.ts b/apps/api/src/autopilot/service.ts index f8f38d3..eb01fea 100644 --- a/apps/api/src/autopilot/service.ts +++ b/apps/api/src/autopilot/service.ts @@ -548,15 +548,9 @@ function distinctPlan(a: Box, b: Box): boolean { return Math.abs(ah - bh) / Math.max(ah, bh, 1e-6) > 0.12; } -/** - * Postgres killed an idle pooled connection (restart, `pg_terminate_backend`, error 57P01). Prisma surfaces the - * raw-query failure as P2010 wrapping the server message; the next tick re-claims a fresh client, so this is a - * transient blip and must not surface as a worker error (BIT-32448). - */ -export function isPostgresAdminShutdown(err: unknown): boolean { - if (!(err instanceof Error)) return false; - return /57P01|terminating connection due to administrator command/i.test(err.message); -} +import { isPostgresAdminShutdown } from "../lib/pg.js"; + +export { isPostgresAdminShutdown }; /** Polls the autopilot queue and settles bursts. */ export class AutopilotWorker { diff --git a/apps/api/src/develop/service.test.ts b/apps/api/src/develop/service.test.ts new file mode 100644 index 0000000..9da9b6c --- /dev/null +++ b/apps/api/src/develop/service.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, it } from "vitest"; +import { RenderWorker, type DevelopService } from "./service.js"; +import { isPostgresAdminShutdown } from "../lib/pg.js"; +import type { IngestLogger } from "../ingest/service.js"; + +describe("isPostgresAdminShutdown", () => { + it("matches the 57P01 admin-shutdown message Prisma surfaces from a raw query (BIT-32449)", () => { + const err = new Error( + "Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `57P01`. Message: `terminating connection due to administrator command`", + ); + expect(isPostgresAdminShutdown(err)).toBe(true); + }); + + it("does not match an unrelated query error", () => { + expect(isPostgresAdminShutdown(new Error("relation \"photos\" does not exist"))).toBe(false); + expect(isPostgresAdminShutdown(null)).toBe(false); + expect(isPostgresAdminShutdown("string")).toBe(false); + }); +}); + +describe("RenderWorker poll error handling", () => { + type Level = "info" | "warn"; + const makeLog = (): IngestLogger & { calls: { level: Level; msg: string }[] } => { + const calls: { level: Level; msg: string }[] = []; + return { + calls, + info: (obj, msg) => calls.push({ level: "info", msg }), + warn: (obj, msg) => calls.push({ level: "warn", msg }), + }; + }; + + const makeService = (claimErr: unknown): Pick => ({ + requeueStale: async () => 0, + claimRenders: async () => { + throw claimErr; + }, + render: async () => undefined, + }); + + it("logs a 57P01 admin shutdown at info, not warn, and keeps looping (BIT-32449)", async () => { + const log = makeLog(); + const worker = new RenderWorker(makeService(new Error("terminating connection due to administrator command (57P01)")) 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" && /db connection was reset/.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, { + 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 === "warn" && /poll failed/.test(c.msg))).toBe(true); + }); +}); diff --git a/apps/api/src/develop/service.ts b/apps/api/src/develop/service.ts index 787c21b..8dc7205 100644 --- a/apps/api/src/develop/service.ts +++ b/apps/api/src/develop/service.ts @@ -3,6 +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 { photoUrls } from "../library/serialize.js"; import type { Broadcaster } from "../realtime.js"; import { type LocalStorage, type UrlSigner, VARIANT_SIZE } from "../storage/storage.js"; @@ -219,7 +220,10 @@ export class RenderWorker { }); } } catch (err) { - this.opts.log.warn({ err }, "develop: poll failed"); + // 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"); + else this.opts.log.warn({ err }, "develop: poll failed"); } finally { this.ticking = false; if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs); diff --git a/apps/api/src/lib/pg.ts b/apps/api/src/lib/pg.ts new file mode 100644 index 0000000..d01d84a --- /dev/null +++ b/apps/api/src/lib/pg.ts @@ -0,0 +1,9 @@ +/** + * Postgres killed a pooled client mid-query (restart, `pg_terminate_backend`, error 57P01). Prisma surfaces the + * raw-query failure as P2010 wrapping the server message; the next tick re-claims a fresh client, so this is a + * transient blip and must not surface as a worker error (BIT-32448, BIT-32449). + */ +export function isPostgresAdminShutdown(err: unknown): boolean { + if (!(err instanceof Error)) return false; + return /57P01|terminating connection due to administrator command/i.test(err.message); +} -- 2.47.3 From 3f7455ebfae35c38053b928da4d1946c7afef5f7 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Wed, 18 Nov 2026 18:04:28 +0000 Subject: [PATCH 2/2] fix(faces): downgrade 57P01 admin-shutdown poll errors to info so they stop paging the error loop (BIT-32450) A Postgres restart or pg_terminate_backend (error 57P01) kills the pooled client mid-`prisma.` in FaceWorker.claimWhere. Prisma surfaces it as P2010 wrapping the server message. The next tick re-claims a fresh client, so this is a transient blip: log it at info and keep looping instead of warning, which was paging the error loop. Reuses the shared isPostgresAdminShutdown helper from lib/pg.js (introduced for the autopilot and develop workers in BIT-32448/BIT-32449). --- apps/api/src/faces/service.ts | 6 ++- apps/api/src/faces/worker.test.ts | 74 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/apps/api/src/faces/service.ts b/apps/api/src/faces/service.ts index d002023..f99d113 100644 --- a/apps/api/src/faces/service.ts +++ b/apps/api/src/faces/service.ts @@ -6,6 +6,7 @@ import { DEFAULT_CODE_LIST } from "../library/iptc.js"; import type { FaceGroup, Person } 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 type { Broadcaster } from "../realtime.js"; import type { LocalStorage } from "../storage/storage.js"; import { normLabel } from "../jersey/service.js"; @@ -1530,7 +1531,10 @@ export class FaceWorker { } } } catch (err) { - this.opts.log.warn({ err }, "faces: poll failed"); + // A DB restart or `pg_terminate_backend` (57P01) takes the pooled client mid-`$queryRaw` (claimWhere); the next + // tick re-claims a fresh one, so log at info and let the loop continue instead of paging the error loop (BIT-32450). + if (isPostgresAdminShutdown(err)) this.opts.log.info({ err: (err as Error).message }, "faces: poll skipped, db connection was reset"); + else this.opts.log.warn({ err }, "faces: poll failed"); } finally { this.ticking = false; if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs); diff --git a/apps/api/src/faces/worker.test.ts b/apps/api/src/faces/worker.test.ts index 35c4fd6..a1ca251 100644 --- a/apps/api/src/faces/worker.test.ts +++ b/apps/api/src/faces/worker.test.ts @@ -1,5 +1,6 @@ import { describe, expect, it } from "vitest"; import { waitUntil } from "../lib/idle.js"; +import { isPostgresAdminShutdown } from "../lib/pg.js"; import { FaceWorker, type FaceService } from "./service.js"; const quiet = { info() {}, warn() {}, error() {}, debug() {} } as never; @@ -62,3 +63,76 @@ describe("the face worker", () => { expect(started).toEqual([]); }); }); + +describe("isPostgresAdminShutdown", () => { + it("matches the 57P01 admin-shutdown message Prisma surfaces from claimWhere's raw query (BIT-32450)", () => { + const err = new Error( + "Invalid `prisma.$queryRaw()` invocation:\n\nRaw query failed. Code: `57P01`. Message: `terminating connection due to administrator command`", + ); + expect(isPostgresAdminShutdown(err)).toBe(true); + }); + + it("does not match an unrelated query error", () => { + expect(isPostgresAdminShutdown(new Error("relation \"photos\" does not exist"))).toBe(false); + expect(isPostgresAdminShutdown(null)).toBe(false); + expect(isPostgresAdminShutdown("string")).toBe(false); + }); +}); + +describe("FaceWorker poll error handling", () => { + type Level = "info" | "warn"; + const makeLog = (): { info: (obj: unknown, msg: string) => void; warn: (obj: unknown, msg: string) => void; error: () => void; debug: () => void; calls: { level: Level; msg: string }[] } => { + const calls: { level: Level; msg: string }[] = []; + return { + calls, + info: (obj, msg) => calls.push({ level: "info", msg }), + warn: (obj, msg) => calls.push({ level: "warn", msg }), + error: () => {}, + debug: () => {}, + }; + }; + + const makeService = (claimErr: unknown): Pick => ({ + requeueStale: async () => 0, + refreshLineupEvents: async () => 0, + queueMissed: async () => { + throw claimErr; + }, + requeueFailed: async () => 0, + requeueVideos: async () => 0, + claimVideos: async () => [], + claim: async () => [], + claimReferences: async () => [], + processReference: async () => {}, + process: async () => {}, + processVideo: async () => {}, + stopVideos: () => {}, + }); + + it("logs a 57P01 admin shutdown at info, not warn, and keeps looping (BIT-32450)", async () => { + const log = makeLog(); + const worker = new FaceWorker(makeService(new Error("terminating connection due to administrator command (57P01)")) as unknown as FaceService, { + concurrency: 1, + intervalMs: 60_000, + log: log as unknown as never, + }); + worker.start(); + await new Promise((r) => setTimeout(r, 20)); + await worker.stop(); + expect(log.calls.some((c) => c.level === "info" && /db connection was reset/.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 FaceWorker(makeService(new Error("relation photos does not exist")) as unknown as FaceService, { + concurrency: 1, + intervalMs: 60_000, + log: log as unknown as never, + }); + worker.start(); + await new Promise((r) => setTimeout(r, 20)); + await worker.stop(); + expect(log.calls.some((c) => c.level === "warn" && /poll failed/.test(c.msg))).toBe(true); + }); +}); -- 2.47.3