prisma: Jest has detected the following 1 open handle potentially keeping Jest from exiting: CustomGC

Bug description

When I run jest --detectOpenHandles I get the following: (Which seems to stem from the Prisma Client)

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  CustomGC

      at Runtime._loadModule (../node_modules/jest-runtime/build/index.js:1111:29)
      at load (../node_modules/@prisma/client/runtime/library.js:73:326)
      at ../node_modules/@prisma/client/runtime/library.js:73:759
      at runInChildSpan (../node_modules/@prisma/client/runtime/library.js:70:25817)
      at Fr.loadLibrary (../node_modules/@prisma/client/runtime/library.js:73:677)
      at Vt.loadEngine (../node_modules/@prisma/client/runtime/library.js:101:557)
      at Vt.instantiateLibrary (../node_modules/@prisma/client/runtime/library.js:100:1478)

How to reproduce

npx jest --detectOpenHandles

Expected behavior

There should be no open handles left over from Jest tests to prisma. There should be no open DB handles anyways since the tests are not connecting a database.

Prisma information

Cannot share on the given project unfortunately.

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v16.15.1

Prisma Version

prisma                  : 4.11.0
@prisma/client          : 4.11.0
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 8fde8fef4033376662cad983758335009d522acb (at node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 8fde8fef4033376662cad983758335009d522acb (at node_modules/@prisma/engines/migration-engine-darwin-arm64)
Format Wasm             : @prisma/prisma-fmt-wasm 4.11.0-57.8fde8fef4033376662cad983758335009d522acb
Default Engines Hash    : 8fde8fef4033376662cad983758335009d522acb
Studio                  : 0.483.0

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 29
  • Comments: 48 (2 by maintainers)

Most upvoted comments

I’m having the same issue here. And it’s causing Garbage Collector not to work well and so Heap Memory is huge after some test files. I’ve tested 4.8.1 and 4.11.0.

it’s not just minor issue. it brokes our pipeline on current project. probably by timeout.

I have the same issue and the Heap size is increasing a lot after writing some tests files like @rodrigocipriani has mentioned. I tested lots of approaches to close this warning and decrease memory consumption with Jest + Prisma but without success.

I created a repository to reproduce the issue: https://github.com/andrewmclagan/prisma-open-handles

It seems it only happens when using pnpm and could possibly be related to this issue, although I think likely its another issue. As this only starts happening on version ^4.11.0

Also seeing this - downgrading to 4.8.1 makes no difference to me

I’ve just published it on GitHub for who is interested

https://github.com/rodrigocipriani/prismatest

I’m reproducing in 4.15.0

EDIT

upgrading both @prisma/client and prisma to 4.15.0 solved it

Can confirm that ^4.11.0 causes open handle issue. Downgrading to 4.8.1 removes the issue.

Confirmed on 4.14.1 this does not look to be happening any more 🎉

I faced the same issue with customGC, preventing jest from exiting. Downgrading to 4.8.1 removed the issue.

My dependencies and devDependencies are the following.

  "dependencies": {
    "@prisma/client": "4.8.1",
    "express": "^4.18.2",
    "zod": "^3.21.4",
    "zod-express-middleware": "^1.4.0"
  },
  "devDependencies": {
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.0",
    "@types/supertest": "^2.0.12",
    "jest": "^29.5.0",
    "prisma": "4.8.1",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "typescript": "^5.0.3"
  },

it’s not just minor issue. it brakes the pipeline on current project. probably by timeout.

true it breaks pipeline. Anyone got it successfully passing the bdd tests? please let me know the version you have?

Also seeing this.

It looks like it was introduced in v4.9.0. Downgrading to v4.8.1 removes the message. Jest still exiting causing test suite to fail even after downgrading to 4.8.1

Environment and Setup prisma : 4.8.1 @prisma/client : 4.8.1 jest : 29.2.2 testcontainers :9.0.1 OS: macOS Database: PostgreSQL 14 Node.js version: v19.2.0

Executes all the test cases and fails at the end with following error Screenshot 2023-03-07 at 4 49 22 PM appended by Jest has detected the following 1 open handle potentially keeping Jest from exiting:

● CustomGC

  at Runtime._loadModule (../node_modules/jest-runtime/build/index.js:1111:29)
  at load (../node_modules/@prisma/client/runtime/library.js:73:326)
  at ../node_modules/@prisma/client/runtime/library.js:73:759
  at runInChildSpan (../node_modules/@prisma/client/runtime/library.js:70:25817)
  at Fr.loadLibrary (../node_modules/@prisma/client/runtime/library.js:73:677)
  at Vt.loadEngine (../node_modules/@prisma/client/runtime/library.js:101:557)
  at Vt.instantiateLibrary (../node_modules/@prisma/client/runtime/library.js:100:1478)

Please let me know if I you need additional info

Worth noting this happens with

generator client {
  provider = "prisma-client-js"
}

The --detectOpenHandles flag is only supposed to help debugging aync operations that keep jest from exiting. As long as you are not using --forceExit and jest doesn’t get stuck with a warning after running your tests, I wouldn’t worry too much about it.

Unlike other database connections we’re using in our tests (e.g. knex), prisma doesn’t seem to require any explicit teardown in order for jest to exit cleanly, however an await prismaClient.$disconnect() wouldn’t hurt.

The main issue I see with this warning about the open prisma handle is that it hides the actual source of the issue from the user. E.g. if jest gets stuck due to an unclosed connection from knex and I use --detectOpenHandles to debug this, it would lead me to the wrong assumption that prisma is at fault.

FYI running with DEBUG="prisma:client:libraryEngine":

  prisma:client:libraryEngine  internalSetup +0ms
  prisma:client:libraryEngine  library starting +793ms
  prisma:client:libraryEngine  library started +87ms
  prisma:client:libraryEngine  sending request, this.libraryStarted: true +51ms
  prisma:client:libraryEngine  requestBatch +18ms
  prisma:client:libraryEngine  sending request, this.libraryStarted: true +60ms
  prisma:client:libraryEngine  library stopping +201ms
  prisma:client:libraryEngine  library stopped +2ms

4.7.0 didn’t work for me.

@andrewmclagan I’m using npm and see the issue.

@aravind605 I’m glad that now it’s working for you, thanks for reaching us back. I will not say that this “bug” is fixed because it’s just working with a preview feature, so it’s not so safe to use for now.

Prisma 4.13.0 and Prisma 4.13.0 stable versions are released. Still no luck with jest tests exiting with a failure as mentioned in this comment. 😦

Thanks for digging in to create a minimum reproduction @rodrigocipriani With rolling prisma back so far, I have to wonder if this has always been going on for the most part, but maybe Jest updated something that does a better job of catching this problem, so now we’ve been seeing it.

I’ve just tested without Typescript and still not having CustomGC error. But the Heap size decreased a lot. image

Hey @aravind605 … I didn’t try to update this repo to test it. I did it in my other repo and it worked. Did you try to use the preview feature I cote in this comment?

#18146 (comment)

If I have some time I’ll update the test repo you’ve mentioned.

Let me know if you tested with the jsonProtocol.

It might be due to the previous node modules. once I’ve cleared and re-installed Issue is not appearing any more!! Thanks @rodrigocipriani and team prisma!

Yes. If you can confirm using one of the repositories that were posted, let us know - that makes it easier for us.

@janpio What do we need to get the “unconfirmed” label removed? Is there any additional information required? Others have added example repositories as an example for reproduction. Does confirmation need to happen from an internal team?

4.8.1 didn’t help… Does anyone have any solution?

I’ve just published it on GitHub for who is interested https://github.com/rodrigocipriani/prismatest

I’ve checked it. Ive cloned it and executed it. I found no issues when running sum.test.*.

But the issue appears when i establish a connection and make a create operation with prisma.

When I downgrade to version 4.8.1. The issue indeed disapppears.

https://github.com/gitnlsn/prisma-test

In this repository there are 2 branches, one for each case. In the readme, I wrote the results from my environment.

Also, I guess the issue might not be related to prisma version at devDependencies, but related to @prisma/client at dependencies. Having "prisma": "^4.8.1", at devDependencies and running npx prisma generate it will install “@prisma/client”: “^4.12.0” and the issue will persist.

I hope those information help to debug it.

Thanks for the tests. I just realize that I was using “^4.8.1” instead of “4.8.1”. So after this I stopped receiving the CustomGC error. But the thing is that the Garbage collector seems not working yet.

I’ve created different scenarios to understand the error better. I notice that when you instantiate and use Prisma the heap memory just increases. You can see those in this PR https://github.com/gitnlsn/prisma-test/pull/1

If you run

npm test --resolveJsonModule ./withPrismaActionsWithoutTypescript/user*

or

npm test --resolveJsonModule ./withPrismaActions/user*

You’ll notice that HEAP just increases consistently.

Thanks for digging in to create a minimum reproduction @rodrigocipriani

With rolling prisma back so far, I have to wonder if this has always been going on for the most part, but maybe Jest updated something that does a better job of catching this problem, so now we’ve been seeing it.

Maybe you’re right, now it’s making better job catching problems. But GC still not working, and memory heap size just increases each test. But only when Prisma is “in the game”. Right now I’m not sure that I’ve tested without Typescript. I I’ll get some time tomorrow to test without Typescript.

Didn’t work here also with 4.8.1.

I’ve created a very simple example you can replicate. I’ve tested with nodejs 18, 16 and 14.

package.json

{
  "name": "prismatest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --detectOpenHandles"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@prisma/client": "^4.12.0",
    "prisma": "^4.12.0"
  },
  "devDependencies": {
    "@types/jest": "^29.5.0",
    "jest": "^29.5.0",
    "ts-jest": "^29.1.0",
    "typescript": "^5.0.3"
  }
}

`jest.config.js``

/** @type {import('ts-jest').JestConfigWithTsJest} */
module. Exports = {
  preset: "ts-jest",
  testEnvironment: "node",
};

`tsconfig.json``

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

schema.prisma

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

generator client {
    provider = "prisma-client-js"
}

model User {
    id        Int      @id @default(autoincrement())
    createdAt DateTime @default(now())
    email     String   @unique
    name      String?
}

sum.ts

function sum(a: number, b: number): number {
  return a + b;
}

export default sum;

sum.test.ts

import { PrismaClient } from "@prisma/client";
import sum from "./sum";

const prismaClient = new PrismaClient();

test("adds 1 + 2 to equal 3", () => {
  expect(sum(1, 2)).toBe(3);
});

npm test

➜ prismatest npm test

> prismatest@1.0.0 test
> jest --detectOpenHandles

 PASS  ./sum.test.ts
  ✓ adds 1 + 2 to equal 3 (4 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.332 s
Ran all test suites.

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  CustomGC

      at Runtime._loadModule (node_modules/jest-runtime/build/index.js:1009:29)
      at load (node_modules/@prisma/client/runtime/library.js:73:326)
      at node_modules/@prisma/client/runtime/library.js:73:759
      at runInChildSpan (node_modules/@prisma/client/runtime/library.js:70:25817)
      at qr.loadLibrary (node_modules/@prisma/client/runtime/library.js:73:677)
      at zt.loadEngine (node_modules/@prisma/client/runtime/library.js:101:557)
      at zt.instantiateLibrary (node_modules/@prisma/client/runtime/library.js:100:1477)

EDIT:

With the package.json below, it worked! But with prisma = 3😕

{
  "name": "prismatest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --detectOpenHandles"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@prisma/client": "^3.15.2",
    "prisma": "^3.15.2"
  },
  "devDependencies": {
    "@types/jest": "^29.5.0",
    "jest": "^29.5.0",
    "ts-jest": "^29.1.0",
    "typescript": "^5.0.3"
  }
}

EDIT 2: 🥲

It seems that with Prisma 3 it didn’t work, it stops showing the CustomGC error, but the Memory use is even worse!

~I suspect this might be an issue with something else interacting with prisma. I encountered the issue after doing a an npm update which updated prisma and @prisma/client from 4.9.0 to 4.12.0 (among other things).~ ~However, after updating just the prisma packages with npm install @prisma/client@4.12.0 prisma@4.12.0, I am not seeing the issue.~

Turns out that I got a tests-not-exiting issue caused by another library (bullmq+nestjs) following an npm update. That was causing my jest e2e test suite to hang and not exit. When investigating with --detectOpenHandles I saw the Prisma CustomGC warning, so initially thought the issue was Prisma.

So I am seeing a CustomGC warning from Prisma 4.12.0, but my tests still exit/finish.

Using npm myself and hitting this so it’s definitely not just pnpm.