prisma: Possible memory leak using driver adapters with cloudflare D1

Bug description

Around the 16th november we started to get errors when making prisma calls in our production prisma/cloudflare workers/d1 setup.

image

The errors consisted of either:

“memory access out of bounds” or “Invalid typed array length: undefined”

We had made no recent deployments nor had our load profile changed, this was pretty out of the blue so might suggest there was a change made on the cloudflare side.

At the moment, our workaround when we start getting these errors is to redeploy the same code base, and this (usually) seems to trigger some sort of worker restart and clears the problem for a short period.

We have been going backwards and forwards with their support but it’s fairly slow going, and their current response is:

Our Workers team confirmed this is coming from the Prisma library you’re using. They seem to be using WASM, and it’s causing these memory access violations. You should be able to see this locally too with an up-to-date Wrangler. We’d recommend reaching out to the Prisma team and reporting the issue to them. It would be best to provide a reproduction case to them if you can as well.

I think we can reproduce some sort of memory leak locally, and a reproduction case is available here - but it is fairly trivial and basically a reproduction of instructions from cloudflare / prisma.

If you run it, and start making requests against it, e.g. with

while true; do curl http://localhost:8787 ; done

and take memory snapshots every now and again, you see typed array usage increasing rapidly

image

and can inspect:

image

How to reproduce

  1. Clone https://github.com/tellisnz/prisma-d1-example and npm i
  2. Create your own demo db i.e. npx wrangler d1 create prisma-demo-db (although not sure if completely necessary if just attempting to run locally) - but if using remote change database_id in wranger.toml
  3. Run npx wrangler d1 migrations apply prisma-demo-db --local
  4. Run npx prisma generate
  5. Run npm run dev
  6. Press d to open dev tools.
  7. Make requests to http://localhost:8787 and observe memory usage in dev tools.

Expected behavior

There should be no memory leak and memory usage should stay fairly constant.

Prisma information

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}
import { PrismaClient } from "@prisma/client";
import { PrismaD1 } from "@prisma/adapter-d1";

export interface Env {
	DB: D1Database;
}

export default {
	async fetch(request, env, ctx): Promise<Response> {
		const adapter = new PrismaD1(env.DB);
		const prisma = new PrismaClient({ adapter, log: [ "info", "query", "warn", "error" ] });
		// const users = await prisma.user.upsert({
		// 	create: {
		// 		email: 'test@test.com',
		// 		name: 'test'
		// 	},
		// 	update: {
		// 		email: 'test@test.com',
		// 		name: 'test'
		// 	},
		// 	where: {
		// 		email: 'test@test.com'
		// 	}
		// });
		// const users = await prisma.user.create({ data: { name: Math.random().toString(), email: Math.random().toString() }})
		const users = await prisma.user.findMany({ where: { name: 'test'}});
		const result = JSON.stringify(users);
		return new Response(result);
	},
} satisfies ExportedHandler<Env>;

Environment & setup

  • OS: macOS
  • Database: SQLite / Cloudflare D1
  • Node.js version: v22.11.0

Prisma Version

prisma                  : 5.22.0
@prisma/client          : 5.22.0
Computed binaryTarget   : darwin-arm64
Operating System        : darwin
Architecture            : arm64
Node.js                 : v22.11.0
Query Engine (Node-API) : libquery-engine 605197351a3c8bdd595af2d2a9bc3025bca48ea2 (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Schema Engine           : schema-engine-cli 605197351a3c8bdd595af2d2a9bc3025bca48ea2 (at node_modules/@prisma/engines/schema-engine-darwin-arm64)
Schema Wasm             : @prisma/prisma-schema-wasm 5.22.0-44.605197351a3c8bdd595af2d2a9bc3025bca48ea2
Default Engines Hash    : 605197351a3c8bdd595af2d2a9bc3025bca48ea2
Studio                  : 0.503.0
Preview Features        : driverAdapters

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 21 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Awesome! @prisma/adapter-pg + Cloudflare Workers + Hyperdrive are running like a charm.

Should be now fixed in https://github.com/prisma/prisma/pull/27493, please let us know if you are still facing any issues after we release 6.11.0 next week! You can also try the latest dev build (dev dist-tag on npm).