From 50699fa675f58ca656fe865332bdbf4e1c8a467f Mon Sep 17 00:00:00 2001 From: paula-alves Date: Wed, 18 Nov 2026 17:42:15 +0000 Subject: [PATCH] fix(autopilot): downgrade 57P01 admin-shutdown poll errors to info so they stop paging the error loop (BIT-32448) 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 autopilot's pooled client mid-`$queryRaw`. Prisma surfaces it as P2010 wrapping the server message, and the catch in `AutopilotWorker.start` logged it at warn as "autopilot: 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. Paperclip-Issue: BIT-32448 Paperclip-Cluster: 5019795ae39d --- apps/api/src/autopilot/service.test.ts | 66 ++++++++++++++++++++++++++ apps/api/src/autopilot/service.ts | 15 +++++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 apps/api/src/autopilot/service.test.ts diff --git a/apps/api/src/autopilot/service.test.ts b/apps/api/src/autopilot/service.test.ts new file mode 100644 index 0000000..1681c4b --- /dev/null +++ b/apps/api/src/autopilot/service.test.ts @@ -0,0 +1,66 @@ +import { describe, expect, it } from "vitest"; +import { AutopilotWorker, isPostgresAdminShutdown, type AutopilotService } from "./service.js"; +import type { IngestLogger } from "../ingest/service.js"; + +describe("isPostgresAdminShutdown", () => { + it("matches the 57P01 admin-shutdown message Prisma surfaces from a raw query (BIT-32448)", () => { + 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("AutopilotWorker 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 = (decideErr: unknown): Pick => ({ + requeueStale: async () => undefined, + claim: async () => [], + decideBursts: async () => { + throw decideErr; + }, + process: async () => undefined, + }); + + it("logs a 57P01 admin shutdown at info, not warn, and keeps looping (BIT-32448)", async () => { + const log = makeLog(); + const worker = new AutopilotWorker(makeService(new Error("terminating connection due to administrator command (57P01)")) as AutopilotService, { + 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 AutopilotWorker(makeService(new Error("relation photos does not exist")) as AutopilotService, { + 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/autopilot/service.ts b/apps/api/src/autopilot/service.ts index d77e50d..f8f38d3 100644 --- a/apps/api/src/autopilot/service.ts +++ b/apps/api/src/autopilot/service.ts @@ -548,6 +548,16 @@ 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); +} + /** Polls the autopilot queue and settles bursts. */ export class AutopilotWorker { private timer: ReturnType | undefined; @@ -578,7 +588,10 @@ export class AutopilotWorker { await this.service.decideBursts(); } } catch (err) { - this.opts.log.warn({ err }, "autopilot: poll failed"); + // A DB restart or `pg_terminate_backend` (57P01) takes the pooled client mid-query; the next tick + // re-claims a fresh one, so log at info and let the loop continue instead of paging the error loop (BIT-32448). + if (isPostgresAdminShutdown(err)) this.opts.log.info({ err: (err as Error).message }, "autopilot: poll skipped, db connection was reset"); + else this.opts.log.warn({ err }, "autopilot: poll failed"); } finally { this.ticking = false; if (!this.stopped) this.timer = setTimeout(tick, this.opts.intervalMs);