diff --git a/apps/api/src/lib/pg.test.ts b/apps/api/src/lib/pg.test.ts index ff5a291..273dd69 100644 --- a/apps/api/src/lib/pg.test.ts +++ b/apps/api/src/lib/pg.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { isPostgresAdminShutdown, isPrismaConnectionClosed, isPrismaServerUnreachable, retryOnConnectionClosed } from "./pg.js"; +import { isPostgresAdminShutdown, isPrismaConnectionClosed, isPrismaServerUnreachable, isPrismaTransactionTimeout, retryOnConnectionClosed } from "./pg.js"; class PrismaLike extends Error { constructor(readonly code: string, message: string) { @@ -90,6 +90,30 @@ describe("isPrismaServerUnreachable", () => { }); }); +describe("isPrismaTransactionTimeout", () => { + it("matches a PrismaClientKnownRequestError with code P2028 (BIT-32083)", () => { + const err = new PrismaLike( + "P2028", + "Invalid `prisma.sportsCompetitionTeam.upsert()` invocation:\n\nTransaction API error: Unable to start a transaction in the given time.", + ); + expect(isPrismaTransactionTimeout(err)).toBe(true); + }); + + it("matches the transaction-timeout message without the code", () => { + expect(isPrismaTransactionTimeout(new Error("Transaction API error: Unable to start a transaction in the given time."))).toBe(true); + }); + + it("does not match a server-closed (P1017) error", () => { + expect(isPrismaTransactionTimeout(new PrismaLike("P1017", "Server has closed the connection."))).toBe(false); + }); + + it("does not match an unrelated query error", () => { + expect(isPrismaTransactionTimeout(new PrismaLike("P2002", "Unique constraint failed"))).toBe(false); + expect(isPrismaTransactionTimeout(null)).toBe(false); + expect(isPrismaTransactionTimeout("string")).toBe(false); + }); +}); + describe("retryOnConnectionClosed", () => { it("retries once when the first attempt throws P1017 and returns the second result", async () => { let calls = 0; diff --git a/apps/api/src/lib/pg.ts b/apps/api/src/lib/pg.ts index cc4adc3..604fe1d 100644 --- a/apps/api/src/lib/pg.ts +++ b/apps/api/src/lib/pg.ts @@ -39,6 +39,19 @@ export function isPrismaServerUnreachable(err: unknown): boolean { ); } +/** + * Prisma could not acquire a pooled connection to start a transaction within `maxWait` + * (code P2028, "Transaction API error: Unable to start a transaction in the given time."). + * This is brief pool contention under concurrent worker load — the next scheduled tick + * reclaims a fresh connection, so it is transient and must not page the error loop + * (BIT-32083 recurrence after the `transactionOptions` maxWait raise in db.ts). + */ +export function isPrismaTransactionTimeout(err: unknown): boolean { + if (!(err instanceof Error)) return false; + const code = (err as { code?: unknown }).code; + return code === "P2028" || /Unable to start a transaction in the given time/i.test(err.message); +} + /** * Runs `fn`, retrying it once when Postgres closed the connection before the query (`P1017`). The pool hands out a * fresh client on the next attempt, so a single retry clears the blip without surfacing it to the caller's diff --git a/apps/api/src/sports/service.ts b/apps/api/src/sports/service.ts index fca0b93..aad66a8 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, isPrismaConnectionClosed, isPrismaServerUnreachable, retryOnConnectionClosed } from "../lib/pg.js"; +import { isPostgresAdminShutdown, isPrismaConnectionClosed, isPrismaServerUnreachable, isPrismaTransactionTimeout, 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"; @@ -2186,7 +2186,11 @@ export class SportsWorker { } await this.service.pollLineups(); } catch (err) { - this.opts.log.warn({ err }, "sports: tick failed"); + if (isPrismaTransactionTimeout(err)) { + this.opts.log.info({ err: (err as Error).message }, "sports: tick skipped, transaction start timed out (pool contention)"); + } else { + this.opts.log.warn({ err }, "sports: tick failed"); + } } finally { this.ticking = false; if (!this.stopped) this.timer = setTimeout(tick, this.opts.lineupEveryMs ?? 5 * 60_000);