Skip to content

Transaction API Error: "Closed transaction found in active transactions map" with Query Compiler and Driver Adapter #27660

Closed

Description

@PatrickRogg

Bug description

When using the query compiler with PrismaPg driver adapter, I'm encountering a transaction error on a specific query in production only.

Error

Transaction API error: Internal Consistency Error: Closed transaction found in active transactions map

Failing Query

const piecesOfInformation = await coreBypassRlsPrisma.pieceOfInformation.findMany({
  where: {
    processingStatus: 'PENDING'
  }
});

Other queries on the same table and all other tables work fine with the same client.

Severity

🚨 Critical: Data loss, app crash, security issue

Reproduction

The issue only happens in our production environment. I can't replicate this problem in my local dev and our test environment. Our test setup the same as production. The only difference is the data.

The findMany should return 300 records. A SQL query on the table completes in less than 20ms, so the amount of data shouldn't be the issue either.

I also tried setting a timeout and wait time on the transaction, but the issue persists.

Expected vs. Actual Behavior

Expected: That prisma returns the rows
Actual: Prisma client throws transaction error

Frequency

Consistently reproducible

Does this occur in development or production?

Only in production (e.g., query engine, generated client)

Is this a regression?

No, the same issue persists with 6.10 as well.

Workaround

Switching back to the Rust-based client resolves the issue:

// This works without errors
return new PrismaClientBase({ datasourceUrl: connectionString }).$extends(
  useExtendedPrismaModel()
);

### Prisma Schema & Queries

### Prisma Schema

```prisma
generator client {
  provider               = "prisma-client"
  output                 = "./generated"
  previewFeatures        = ["postgresqlExtensions", "relationJoins", "queryCompiler"]
  runtime                = "nodejs"
  moduleFormat           = "esm"
  generatedFileExtension = "ts"
  importFileExtension    = "ts"
}

generator json {
  provider  = "prisma-json-types-generator"
  output    = "./generated"
  namespace = "PrismaJson"
}

datasource db {
  provider   = "postgresql"
  url        = env("CORE_DATABASE_URL")
  directUrl  = env("CORE_DATABASE_DIRECT_URL")
  extensions = [pgsodium(schema: "pgsodium"), supabase_vault, uuid_ossp(map: "uuid-ossp", schema: "extensions"), pgvector(map: "vector")]
}

model PieceOfInformation {
  id String @id @default(dbgenerated("typeid_generate_text('infopiece'::text)"))

  ownedByOrganization   Organization @relation(fields: [ownedByOrganizationId], references: [id])
  ownedByOrganizationId String       @default(dbgenerated("current_organization_id()"))

  processingStatus PieceOfInformationProcessingStatus @default(PENDING)
  status           LifecycleStatus                    @default(ACTIVE)
  name             String

  createdOn DateTime @default(now())
  updatedOn DateTime @updatedAt

  @@index([ownedByOrganizationId])
  @@index([ownedByOrganizationId, status])
  @@index([ownedByOrganizationId, itemId])
  @@index([processingStatus])
}

Client Initialization

const extendedPrismaClient = (connectionString: string) => {
  const pgAdapter = new PrismaPg({ connectionString });
  return new PrismaClientBase({ adapter: pgAdapter }).$extends(useExtendedPrismaModel());
};

const baseCoreByPassRlsPrisma = 
  globalThis.baseCoreByPassRlsPrisma ?? extendedPrismaClient(process.env.CORE_DATABASE_URL!);

export const coreBypassRlsPrisma = baseCoreByPassRlsPrisma.$extends(
  byPassRls()
) as unknown as CorePrismaClient;

RLS Middleware (wraps all operations in transactions)

const rlsExtension = (claimSql: Prisma.Sql, user: SimpleAuthUser) => {
  const rlsStore = new AsyncLocalStorage<RlsStore>();

  return Prisma.defineExtension(client => {
    return client.$extends({
      name: 'rlsExtension',
      query: {
        $allModels: {
          async $allOperations({ args, query }) {
            const alreadyInRls = rlsStore.getStore()?.alreadyInRls;
            if (alreadyInRls) return query(args);

            const [, result] = await client.$transaction([
              client.$executeRaw(claimSql),
              query(args)
            ]);

            return result;
          }
        }
     }
   }
}

Environment & Setup

  • OS: Debian
  • Database: PostgreSQL
  • Node.js version: 22

Prisma Version

 6.11

Activity

  1. changed the title [-]Transaction API Error: "Closed transaction found in active transactions map" with Query Compiler and Driver Adapter Environment[/-] [+]Transaction API Error: "Closed transaction found in active transactions map" with Query Compiler and Driver Adapter[/+] on Jul 15, 2025
  2. eliliam commented on Jul 16, 2025

    @eliliam

    We have also seen this happen a few times, but are unsure of the circumstances in which it is caused.

  3. ramipellumbi commented on Aug 1, 2025

    @ramipellumbi

    +1 to observing it since switching off the rust-based client. Unsure of what changed internally. My context is a long running transaction ends up having an operation fail inside of it due to: "Internal Consistency Error: Closed transaction found in active transactions map."

    Edit: seeing this should have been fixed. Updating now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions