🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(shared): skip Prisma read retries inside transactions (#19508)
Valery Meleshkin02d78cccdb9d
diff --git a/packages/shared/src/dbConnectionRetry.test.ts b/packages/shared/src/dbConnectionRetry.test.tsindex 2576c1ca8a..15c16a2eb9 100644--- a/packages/shared/src/dbConnectionRetry.test.ts+++ b/packages/shared/src/dbConnectionRetry.test.ts@@ -107,4 +107,43 @@ describe("postgresConnectionRetryExtension", () => {).rejects.toThrow("Can't reach database server");expect(retryWarnings()).toHaveLength(0);});++ it.each(["batch", "interactive"] as const)(+ "does not retry reads inside %s transactions",+ async (kind) => {+ const error = unreachable();+ const run = vi.fn().mockRejectedValue(error);+ const failingClient = client.$extends({+ query: {+ project: {+ findFirst: () => run(),+ },+ },+ });++ // Stub only transaction lifecycle calls; Prisma still supplies the real+ // transaction metadata to the query extensions without needing Postgres.+ const engine = (+ base as unknown as {+ _engine: { transaction: (action: string) => Promise<unknown> };+ }+ )._engine;+ const transaction = vi+ .spyOn(engine, "transaction")+ .mockResolvedValue({ id: "connection-retry-test" });++ try {+ const result =+ kind === "batch"+ ? failingClient.$transaction([failingClient.project.findFirst()])+ : failingClient.$transaction((tx) => tx.project.findFirst());++ await expect(result).rejects.toBe(error);+ expect(run).toHaveBeenCalledTimes(1);+ expect(retryWarnings()).toHaveLength(0);+ } finally {+ transaction.mockRestore();+ }+ },+ );});diff --git a/packages/shared/src/dbConnectionRetry.ts b/packages/shared/src/dbConnectionRetry.tsindex 54c290dec0..03b0e92abe 100644--- a/packages/shared/src/dbConnectionRetry.ts+++ b/packages/shared/src/dbConnectionRetry.ts@@ -47,8 +47,7 @@ const isConnectionError = (error: unknown): boolean => {// Retries cover a Postgres restart (e.g. an Aurora minor-version upgrade or// failover), which refuses connections for one to two seconds.// Writes and raw queries are never retried: a dropped connection may already-// have committed them, and a retried operation from a batch $transaction would-// run outside that transaction.+// have committed them.const isRetryable = (error: unknown, operation: string): boolean =>READ_OPERATIONS.has(operation) && isConnectionError(error);@@ -79,7 +78,16 @@ export const withPostgresConnectionRetry = <T>(export const postgresConnectionRetryExtension = Prisma.defineExtension({name: "postgresConnectionRetry",query: {- $allOperations({ model, operation, args, query }) {+ $allOperations(params) {+ const { model, operation, args, query } = params;+ // Prisma exposes transaction membership only through undocumented runtime+ // metadata. Batch read retries can escape the transaction; interactive+ // read retries cannot recover a transaction whose connection is gone.+ const { __internalParams } = params as typeof params & {+ __internalParams?: { transaction?: unknown };+ };+ if (__internalParams?.transaction !== undefined) return query(args);+return withPostgresConnectionRetry({ model, operation }, () =>query(args),);