fix(sports): retry syncEventSquads when Postgres closes the pooled client (BIT-32460) #271

Merged
spark merged 1 commits from fix/BIT-32460-sports-p1017-connection-closed into main 2026-11-18 20:17:38 +00:00
Owner

Problem

PrismaClientKnownRequestError code P1017 ("Server has closed the connection.") was raised from personForIdentity (apps/api/src/sports/service.ts:1767) → linkSquadPerson → syncEventSquadsNow → syncEventSquads, and rejected up to the archive caller, whose .catch() logs archive: squads not synced at warn and pages the error loop (errors.bitreport.pt, ERRSIG 42f1a9a09199, ERRCLUSTER 21cd13c42286).

P1017 is transient: the server closed an idle pooled client before the next query (idle timeout, restart, pg_terminate_backend, network blip) and the @prisma/adapter-pg pool hands out a fresh client on the following attempt.

Fix

Adds isPrismaConnectionClosed() and retryOnConnectionClosed() to apps/api/src/lib/pg.ts (matching the existing isPostgresAdminShutdown() family for 57P01), and wraps the syncEventSquadsNow call in syncEventSquads with a single retry on a fresh connection.

The operation is idempotent under a re-run (upserts, findUnique lookups, unique-constraint creates whose P2002 is already caught), so replaying it on a fresh connection is safe.

Distinct from 57P01, which is handled in the worker poll loops (BIT-32448/32449/32450) and the pg.Pool error listener (BIT-32447).

Tests

New apps/api/src/lib/pg.test.ts covers isPrismaConnectionClosed (P1017 code, message-only, distinct from 57P01, non-matches) and retryOnConnectionClosed (retry once on P1017, no retry on P2002, one retry only, success path). Verified via tsx (13/13) since the API test global-setup requires a _test database unavailable in this workspace; CI runs the full suite against a photoreport_test Postgres service.

pnpm -r build and pnpm --filter @photoreport/api typecheck green.

Paperclip-Issue: BIT-32460
Paperclip-Cluster: 21cd13c42286

## Problem `PrismaClientKnownRequestError` code `P1017` ("Server has closed the connection.") was raised from `personForIdentity` (`apps/api/src/sports/service.ts:1767`) → `linkSquadPerson` → `syncEventSquadsNow` → `syncEventSquads`, and rejected up to the archive caller, whose `.catch()` logs `archive: squads not synced` at `warn` and pages the error loop (errors.bitreport.pt, ERRSIG `42f1a9a09199`, ERRCLUSTER `21cd13c42286`). P1017 is transient: the server closed an idle pooled client before the next query (idle timeout, restart, `pg_terminate_backend`, network blip) and the `@prisma/adapter-pg` pool hands out a fresh client on the following attempt. ## Fix Adds `isPrismaConnectionClosed()` and `retryOnConnectionClosed()` to `apps/api/src/lib/pg.ts` (matching the existing `isPostgresAdminShutdown()` family for `57P01`), and wraps the `syncEventSquadsNow` call in `syncEventSquads` with a single retry on a fresh connection. The operation is idempotent under a re-run (upserts, `findUnique` lookups, unique-constraint creates whose `P2002` is already caught), so replaying it on a fresh connection is safe. Distinct from `57P01`, which is handled in the worker poll loops (BIT-32448/32449/32450) and the `pg.Pool` error listener (BIT-32447). ## Tests New `apps/api/src/lib/pg.test.ts` covers `isPrismaConnectionClosed` (P1017 code, message-only, distinct from 57P01, non-matches) and `retryOnConnectionClosed` (retry once on P1017, no retry on P2002, one retry only, success path). Verified via `tsx` (13/13) since the API test global-setup requires a `_test` database unavailable in this workspace; CI runs the full suite against a `photoreport_test` Postgres service. `pnpm -r build` and `pnpm --filter @photoreport/api typecheck` green. Paperclip-Issue: BIT-32460 Paperclip-Cluster: 21cd13c42286
spark added 1 commit 2026-11-18 20:07:07 +00:00
fix(sports): retry syncEventSquads when Postgres closes the pooled client (BIT-32460)
ci / rust (pull_request) Successful in 4m7s
ci / typescript (pull_request) Successful in 10m11s
d21612f7c4
PrismaClientKnownRequestError code P1017 ("Server has closed the
connection.") is raised when the server closes an idle pooled client
before the next query runs (idle timeout, restart, pg_terminate_backend,
network blip). The @prisma/adapter-pg pool hands out a fresh client on
the following attempt, so the failure is transient — but it surfaced
from personForIdentity -> linkSquadPerson -> syncEventSquadsNow and
rejected up to the archive caller, whose .catch() logged it at warn and
paged the error loop (errors.bitreport.pt, ERRSIG 42f1a9a09199).

Add isPrismaConnectionClosed() and retryOnConnectionClosed() to lib/pg
(matching the existing isPostgresAdminShutdown() family for 57P01), and
wrap the syncEventSquadsNow call in syncEventSquads with a single retry.
The operation is idempotent under a re-run (upserts, findUnique lookups,
unique-constraint creates whose P2002 is already caught), so replaying
it on a fresh connection is safe. Distinct from 57P01, which is handled
in the worker poll loops (BIT-32448/32449/32450) and the pg.Pool error
listener (BIT-32447).

Paperclip-Issue: BIT-32460
Paperclip-Cluster: 21cd13c42286
spark merged commit 0c20b6ba4a into main 2026-11-18 20:17:38 +00:00
Sign in to join this conversation.