From 4ff13dbe8e6dcfbca899be5e213f7c0608fa1a49 Mon Sep 17 00:00:00 2001 From: paula-alves Date: Fri, 20 Nov 2026 23:07:14 +0000 Subject: [PATCH] fix(sports): downgrade transient 'Transaction already closed' P2028 errors to info (BIT-32810) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The 'Transaction already closed: A commit cannot be executed on a transaction that was rolled back' error (P2028) fires when Prisma's implicit transaction is rolled back mid-operation (connection blip, deadlock) and a commit is then attempted on the closed transaction. Like the P2028 'Unable to start a transaction' variant, it is transient — the next scheduled tick reclaims a fresh connection and retries. Add isPrismaTransactionClosed to pg.ts and check it before the broad isPrismaTransactionTimeout (which matches all P2028 codes) in the SportsWorker tick handler, so the closed-transaction variant logs at info with an accurate message instead of warn-paging the error loop. Paperclip-Issue: BIT-32810 --- apps/api/src/lib/pg.test.ts | 26 +++++++++++++++++++++++++- apps/api/src/lib/pg.ts | 11 +++++++++++ apps/api/src/sports/service.ts | 6 ++++-- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/apps/api/src/lib/pg.test.ts b/apps/api/src/lib/pg.test.ts index 273dd69..b0c2503 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, isPrismaTransactionTimeout, retryOnConnectionClosed } from "./pg.js"; +import { isPostgresAdminShutdown, isPrismaConnectionClosed, isPrismaServerUnreachable, isPrismaTransactionClosed, isPrismaTransactionTimeout, retryOnConnectionClosed } from "./pg.js"; class PrismaLike extends Error { constructor(readonly code: string, message: string) { @@ -114,6 +114,30 @@ describe("isPrismaTransactionTimeout", () => { }); }); +describe("isPrismaTransactionClosed", () => { + it("matches the 'Transaction already closed' P2028 variant (BIT-32810)", () => { + const err = new PrismaLike( + "P2028", + "Invalid `prisma.sportsCompetitionTeam.upsert()` invocation:\n\nTransaction API error: Transaction already closed: A commit cannot be executed on a transaction that was rolled back.", + ); + expect(isPrismaTransactionClosed(err)).toBe(true); + }); + + it("matches the message without the code", () => { + expect(isPrismaTransactionClosed(new Error("Transaction API error: Transaction already closed: A commit cannot be executed on a transaction that was rolled back."))).toBe(true); + }); + + it("does not match a transaction-start-timeout (P2028) error", () => { + expect(isPrismaTransactionClosed(new PrismaLike("P2028", "Transaction API error: Unable to start a transaction in the given time."))).toBe(false); + }); + + it("does not match an unrelated query error", () => { + expect(isPrismaTransactionClosed(new PrismaLike("P2002", "Unique constraint failed"))).toBe(false); + expect(isPrismaTransactionClosed(null)).toBe(false); + expect(isPrismaTransactionClosed("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 604fe1d..e5ed924 100644 --- a/apps/api/src/lib/pg.ts +++ b/apps/api/src/lib/pg.ts @@ -52,6 +52,17 @@ export function isPrismaTransactionTimeout(err: unknown): boolean { return code === "P2028" || /Unable to start a transaction in the given time/i.test(err.message); } +/** + * A Prisma implicit or explicit transaction was rolled back (connection blip, deadlock, or query error) and then a + * commit was attempted on the already-closed transaction (code P2028, "Transaction API error: Transaction already + * closed: A commit cannot be executed on a transaction that was rolled back."). The next tick reclaims a fresh + * connection and retries, so this is transient and must not page the error loop (BIT-32810). + */ +export function isPrismaTransactionClosed(err: unknown): boolean { + if (!(err instanceof Error)) return false; + return /Transaction already closed/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 adabcac..df95fa5 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, isPrismaTransactionTimeout, retryOnConnectionClosed } from "../lib/pg.js"; +import { isPostgresAdminShutdown, isPrismaConnectionClosed, isPrismaServerUnreachable, isPrismaTransactionClosed, 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"; @@ -2546,7 +2546,9 @@ export class SportsWorker { await this.service.pollLineups(); await this.service.syncJoga(); } catch (err) { - if (isPrismaTransactionTimeout(err)) { + if (isPrismaTransactionClosed(err)) { + this.opts.log.info({ err: (err as Error).message }, "sports: tick skipped, transaction rolled back (connection blip or deadlock)"); + } else 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");