From 67adf1ce703ae2ea1e6870dd12003ab1f78f7ab3 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Wed, 18 Nov 2026 22:22:41 +0000 Subject: [PATCH] fix(sports): stop syncEventSquads when P1017 exhausts the retry (BIT-32475) The error loop reported PrismaClientKnownRequestError P1017 ("Server has closed the connection.") from linkSquadPerson -> person.findFirst. retryOnConnectionClosed (BIT-32460) already retries syncEventSquadsNow once on a fresh client; when the server keeps closing the pool (a restart or pg_terminate_backend tearing through clients), the second attempt fails the same way and the rejection escaped the syncEventSquads catch, which only matched isPrismaServerUnreachable (P1001/57P03). The warn paged the error loop for a transient blip. Treat a P1017 that survives the retry like the existing unreachable handling: log at info and stop the sync, letting the next scheduled call retry once the pool is stable. Mirrors BIT-32463/BIT-32470/BIT-32471. Paperclip-Issue: BIT-32475 Paperclip-Cluster: 792bb5de9fcb --- apps/api/src/sports/service.test.ts | 39 +++++++++++++++++++++++++++++ apps/api/src/sports/service.ts | 10 +++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/apps/api/src/sports/service.test.ts b/apps/api/src/sports/service.test.ts index 170df98..8236b40 100644 --- a/apps/api/src/sports/service.test.ts +++ b/apps/api/src/sports/service.test.ts @@ -36,6 +36,9 @@ const p57p01 = () => { code: "P2010" }, ); +const p1017 = () => + Object.assign(new Error("Invalid `prisma.person.findFirst()` invocation:\n\nServer has closed the connection."), { code: "P1017" }); + const ligaTeam = (id: string, externalId: string) => ({ id, provider: "LIGA", @@ -193,6 +196,42 @@ describe("syncEventSquads downgrades a 57P03 from personForIdentity's sportsPlay }); }); +describe("syncEventSquads downgrades a P1017 server-closed connection that exhausts the retry (BIT-32475)", () => { + it("logs at info and returns zeros when person.findFirst is P1017 on both attempts", async () => { + // The error loop reported P1017 ("Server has closed the connection.") from linkSquadPerson → person.findFirst. + // retryOnConnectionClosed (BIT-32460) retries once on a fresh client; when the server keeps closing the pool + // (a restart tearing through clients), the second attempt fails the same way. Without this guard the rejection + // escapes to the archive caller's warn and pages the error loop for a transient blip. + const fpaTeam = { + id: "t-fpa", + provider: "FPA", + externalId: "fpa-1", + name: "Team FPA", + abbreviation: "FPA", + logoUrl: null, + }; + const db = { + event: { findUnique: async () => makeEvent({ homeTeam: fpaTeam, awayTeam: null }) }, + sportsPlayer: { + findFirst: async () => ({ season: "2026" }), + findMany: async () => [{ id: "pl-1", name: "Player 1", externalId: "lic-1", number: "1", photoUrl: null, season: "2026" }], + findUnique: async () => null, + }, + person: { + findUnique: async () => null, + // Throws on every call, so the retry also fails with P1017. + findFirst: async () => { throw p1017(); }, + }, + }; + const service = new SportsService(makeDeps(db)); + const out = await service.syncEventSquads("evt-1"); + expect(out).toEqual({ created: 0, updated: 0, headshots: 0 }); + const log = (service as unknown as { deps: { log: { calls: LogCall[] } } }).deps.log; + expect(log.calls.some((c) => c.level === "info" && /database connection closed/.test(c.msg))).toBe(true); + expect(log.calls.some((c) => c.level === "warn")).toBe(false); + }); +}); + describe("linkStoredSquads downgrades a 57P01 admin-terminated connection (BIT-32471)", () => { it("logs at info and returns 0 when the raw query is terminated mid-flight (57P01)", async () => { const db = { diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index 812d955..10c4acf 100644 --- a/apps/api/src/sports/service.ts +++ b/apps/api/src/sports/service.ts @@ -8,7 +8,7 @@ import type { IngestLogger } from "../ingest/service.js"; import { DEFAULT_CODE_LIST } from "../library/iptc.js"; import { badRequest, notFound } from "../lib/errors.js"; import { waitUntil } from "../lib/idle.js"; -import { isPostgresAdminShutdown, isPrismaServerUnreachable, retryOnConnectionClosed } from "../lib/pg.js"; +import { isPostgresAdminShutdown, isPrismaConnectionClosed, isPrismaServerUnreachable, retryOnConnectionClosed } from "../lib/pg.js"; import type { LocalStorage, UrlSigner } from "../storage/storage.js"; import { type CodeClash, type SquadForCodes, buildCodeLines, hasGeneratedBlock, mergeGenerated, slugify, toTsv } from "./codegen.js"; import { fetchYouthCamps, youthAbbrev } from "./fpa-youth.js"; @@ -1437,6 +1437,14 @@ export class SportsService { this.deps.log.info({ err: (err as Error).message, eventId }, "sports: squad sync stopped, database unreachable"); return { created: 0, updated: 0, headshots: 0 }; } + // The server closed the pooled client again on the retry (P1017): a restart or `pg_terminate_backend` is + // tearing through the pool, so the next query would fail the same way. The one retry already tried a fresh + // client (BIT-32460); stop here and let the next scheduled sync retry once the pool is stable, instead of + // rejecting to the archive caller's warn and paging the error loop for a transient blip (BIT-32475). + if (isPrismaConnectionClosed(err)) { + this.deps.log.info({ err: (err as Error).message, eventId }, "sports: squad sync stopped, database connection closed"); + return { created: 0, updated: 0, headshots: 0 }; + } throw err; }), );