FG
💻 Software🗄️ DatabasesVercel

Prisma Memory Leak when using in NestJS app, reported by Jest

Fresh5 days ago
Mar 14, 20260 views
Confidence Score60%
60%

Problem

Bug description In our (closed source) project, we added a lot of test for a new feature. That resulted in memory problems in our ci. We found, that Prisma is might be one of the problems. I created a minimal reproduction https://github.com/adrian-goe/prisma-nestjs-memory-leak-repoduction There is one test without using prisma app.controller.spec.ts and one using prisma app-prisma.controller.spec.ts running both test with `node --expose-gc` and `jest --detectLeaks` results in jest finding memory leaks in the test with prisma. It might be possible, that this leaks also happens while running the application, but I didn't test that. It could be possible, that this is a nestJs problem as well, but we ware only be able to reproduce this with Prisma, but no other dependency or module. How to reproduce https://github.com/adrian-goe/prisma-nestjs-memory-leak-repoduction Expected behavior _No response_ Prisma information see https://github.com/adrian-goe/prisma-nestjs-memory-leak-repoduction Environment & setup - OS: [macOS, Windows, Debian] might be more - Database: [PostgreSQL] only tested with postgres - Node.js version: v16.18.0 Prisma Version [code block] In our internal project, this was also an issue with Prisma 4.1.1

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix Prisma Memory Leak in NestJS Tests

Medium Risk

The memory leak occurs due to improper handling of database connections in Prisma when running tests. Specifically, instances of Prisma Client are not being properly disposed of after each test, leading to lingering references and memory not being freed. This is exacerbated in a testing environment where multiple tests are run in succession, causing the memory footprint to grow.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Ensure Proper Cleanup of Prisma Client

    Modify your test setup to ensure that the Prisma Client instance is properly disconnected after each test. This can be achieved by using Jest's 'afterEach' hook to call the disconnect method on the Prisma Client.

    typescript
    afterEach(async () => {
      await prisma.$disconnect();
    });
  2. 2

    Limit Prisma Client Instances

    Ensure that you are not creating multiple instances of the Prisma Client in your tests. Instead, create a single instance and reuse it across tests. This can be done by initializing the Prisma Client in a global setup file.

    javascript
    const { PrismaClient } = require('@prisma/client');
    const prisma = new PrismaClient();
    module.exports = prisma;
  3. 3

    Use Jest Global Setup and Teardown

    Implement Jest's global setup and teardown to manage the lifecycle of the Prisma Client. This ensures that the client is instantiated once before all tests and disconnected after all tests have completed.

    javascript
    module.exports = async () => {
      global.prisma = new PrismaClient();
    };
    
    module.exports = async () => {
      await global.prisma.$disconnect();
    };
  4. 4

    Monitor Memory Usage

    After implementing the above changes, run your tests with memory monitoring tools to ensure that memory usage stabilizes and no leaks are present. Use the command 'node --expose-gc' and 'jest --detectLeaks'.

    bash
    jest --detectLeaks
  5. 5

    Upgrade Prisma Version

    Check for the latest version of Prisma and upgrade if necessary. Memory leak issues may have been addressed in newer releases. Update your package.json and run npm install.

    bash
    npm install @prisma/client@latest

Validation

Run your Jest tests with the memory leak detection enabled. Confirm that no memory leaks are reported after implementing the above steps. Monitor the memory usage during the test run to ensure it remains stable.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

prismaormpostgresqlbug/2-confirmedkind/bugtopic:-prisma-generatetopic:-node-version