From 0197b4f6e81d0f37faac93575dc5fa9c3b915be3 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Fri, 20 Nov 2026 22:47:31 +0000 Subject: [PATCH] fix(sports): downgrade transient Prisma transaction timeout from warn to info (BIT-32083) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The transactionOptions maxWait raise to 10s in db.ts (PR #207) reduced frequency but did not eliminate the P2028 "Unable to start a transaction in the given time" error — 1 recurrence in 13 days under brief pool contention. The sports tick already swallows errors and reschedules, so this is transient like 57P01/P1017/P1001 already downgraded in faces, autopilot, develop and sports itself. Add isPrismaTransactionTimeout to pg.ts (matches P2028 code or message) and use it in SportsWorker.tick to log at info instead of warn, so the error loop no longer pages on a single pool-contention blip. Paperclip-Issue: BIT-32083 --- apps/api/src/lib/pg.test.ts | 26 +++++++++++++++++++++++++- apps/api/src/lib/pg.ts | 13 +++++++++++++ apps/api/src/sports/service.ts | 8 ++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) 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); -- 2.47.3