New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prisma Memory Leak when using in NestJS app, reported by Jest #17925
Comments
Hi @adrian-goe. One problem I find with you reproduction - I don't see Second problem: when node run with |
@SevInf oh i forgot the app.close call in the test. I will add that alter. But in our production code we have this in every that case. The jest problem you mentioned, wouldn't this also affect the other test as well? |
@SevInf also, i have a prisma Service class. This is provided in the module that extends the OnApplicationShutdown hook from nestjs, so it should correctly shut down, when nestJs shuts down. |
@adrian-goe Also, try |
@SevInf I added the I also added the |
So I can confirm that the reproduction outputs the following message:
I also sent a PR to make the reproduction actually work out of the box: adrian-goe/prisma-nestjs-memory-leak-repoduction#1 Note: Even though this is now on |
Hey guys I'm currently debugging a memory-leak on our production app (which is not using NestJS but a similar approach of extending I've noticed from your reproduction that if I print out However if I downgrade to prisma@3.0.2 the memory goes down after the query (the test still fails because there might be some open handles or some other stuff). To reproduce follow the steps in README.md and adjust the file import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { PrismaModule } from './prisma/prisma.module';
import { AppPrismaController } from './app-prisma.controller';
import { AppPrismaService } from './app-prisma.service';
describe('AppPrismaController', () => {
let app: TestingModule;
beforeAll(async () => {
app = await Test.createTestingModule({
imports: [PrismaModule],
controllers: [AppPrismaController],
providers: [AppPrismaService],
}).compile();
});
afterAll(async () => {
await app.close();
});
describe('getData', () => {
it('should return "Welcome to prisma-memeory-leak!"', async () => {
const appController = app.get<AppPrismaController>(AppPrismaController);
console.log(
`before querying ${
Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100
} MB`
);
expect(await appController.getData()).toEqual({
message: 'Welcome to prisma-memeory-leak!',
});
console.log(
`after querying ${
Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100
} MB`
);
});
});
}); Then run the following commands:
see the memory increase:
then downgrade to prisma@3.0.2:
and run the tests again:
The changelog between 3.0.2 and 3.1.1 versions can be found here: https://github.com/prisma/prisma/releases/tag/3.1.1 And here's a comparison between 3.0.2 and 3.1.1: And here's a comparison between |
I've created a new issue where there's a reproduction with no other dependency but Prisma. Technically I cannot call it a memory leak but I am for sure calling it a memory mismanagement. My feeling is that the following commit is causing it: f395aba but that's just a feeling since I know the prisma infrastructure is super complex and I'm not a Rust developer. I hope it helps in your investigation. |
we face same issue with Prisma and nestjs, even calling prisma.collection.delete catch memory leak and invalid pointers etc...
|
I see they backport the commit to all major releases! |
@adrian-goe I notice both of us use arm64 |
@xlmnxp since this is also a problem in our CI, that don't use arm64, it is probably not related to this. |
Hmm, we have same issue, |
@adrian-goe I find something wired that if the issue happen once, then I cannot start nestjs and got one of the following
|
I think related to #18510 |
Has anyone come across a workaround for this? I'm noticing a very slow memory leak in my production server that causes a crash after 1-2 weeks. In my local debugging Prisma seems to come up pretty often in my heap dumps. |
We're also seeing a very clear memory leak in prisma, staring somewhere between version 4.3.1 and 4.12.0. The amount of leaked memory seems to depend on the amount of queries made with prisma, with some of our high-load services running out of memory in a day or two. |
Same problem and I found there is a WeakRef in LibraryEngine.ts try {
// Using strong reference to `this` inside of log callback will prevent
// this instance from being GCed while native engine is alive. At the same time,
// `this.engine` field will prevent native instance from being GCed. Using weak ref helps
// to avoid this cycle
const weakThis = new WeakRef(this)
this.engine = new this.QueryEngineConstructor(
{
datamodel: this.datamodel,
env: process.env,
logQueries: this.config.logQueries ?? false,
ignoreEnvVarErrors: true,
datasourceOverrides: this.datasourceOverrides,
logLevel: this.logLevel,
configDir: this.config.cwd,
engineProtocol: 'json',
},
(log) => {
weakThis.deref()?.logger(log)
},
)
} I'm not sure is Forward the reply |
In case folks missed it, this looks like its related to using Prisma on Node 16/18. Updating to Nodejs v20 fixed the memory leak for my team. |
@mmmeff I tested it with the reproduction, i posted. That did not fix it. Node: 20.6.1 @janpio I pushed it on a seperate branch https://github.com/adrian-goe/prisma-nestjs-memory-leak-repoduction/tree/node20 edit: here is a PR with a test pipline: adrian-goe/prisma-nestjs-memory-leak-repoduction#3 |
Thanks for anticipating my next question @adrian-goe and delivering the answer 🙇 Can you do the same @whalesync-ryder maybe? |
@whalesync-ryder This is what 6 hours on |
@janpio Thanks for running it, i had a background task running this morning that found similar results. It's 10x better on node 20: Node 16.13.0: Increases at 47. MB/hour |
My result for 6 hours of node 16.13.0: |
@janpio Very interesting! So you're not seeing any difference. Maybe I had something else going on to explain the 10x difference in leak speed between my two runs. I can't imagine what it would be. |
I believe that we are also experiencing this - I can't give source code publicly, but I am happy to add a few developers to our repo for research purposes if that helps. Nest.js - Node 18 - Prisma, deployed on Heroku, crashes out with heap allocation about once per day. |
Thanks for the offer, but we have a few general apps that run out of memory with "just keep it running with normal production level traffic" - that is super hard to reproduce for us. Optimal would a minimal reproduction that clearly shows how memory is leaking. The more obvious, the easier it is for us to figure out what is going on. |
I'm sorry I know this is not a particularly helpful comment, but we're definitely getting the same issues as described above in production too. For now we're also restarting everything regularly with pm2. Can't share too much other info about our setup on here, but if someone from prisma wants to reach out I can spare some time. |
using Prisma 5.3.1 and Node.js 20.6 decreased the amount of restarts per day to 0.25, which is a major improvement from the daily / twice restarts |
Hello 👋🏼 I'm working through posts here and reproductions attempts. Note for @adrian-goe |
This comment was marked as outdated.
This comment was marked as outdated.
@whalesync-ryder I could reproduce finally using Node.js v16, the reproduction in GitHub Actions was missing Did you upgrade to v18 or v20 already? This problem looks only related to Node.js v16 (which is "end of life") to me at the moment, see results: Note:
Using
|
I am in the process of upgrading my node right now, so i should be able to confirm pretty soon if that resolved it for me. Thanks for the info. |
Can confirm: Upgrading to Node v20 and Prisma v5.5.2 fixed the leak for me. Previously, memory usage would increase to 2GB within a few hours. Now, it remains at approximately 600MB over the same time span. |
Same here, my issues have been resolved since the node upgrade. |
Same - had a few weeks without issue since the upgrade, seems to be fixed! |
I still experience memory leaks with Prisma 5.5.2 on both Node.js 18.18.2 and 20.10.0. |
Hey @livthomas we'll need further information to be able to help you at all. |
Hello here, thanks for the updates! We're very happy to see that Upgrading to Node.js v18+ solved the issue for you 🙌🏼 We will now close this, here are a some notes about some things that were discussed here. Like we found in #13575, about Jest Note that Prisma 5 got big performance improvements, like the internal protocol switch, which uses less RAM, for more details see https://www.prisma.io/blog/prisma-5-f66prwkjx72s The memory leak in this issue was confirmed when using Node.js v16, which is now End-Of-Life. If you have a reproduction where a memory leak can be observed with a recent version of Prisma, please open a new issue with a reproduction, we will be happy to investigate and try to reproduce. As a general note, when creating an issue, let us know if what you see is excessive memory usage or something that looks like a memory leak.
I hope I can create a code template in the future with some guidelines to help with identifying potential memory leaks in the future (to be done). |
This seems to have some effect on me, thank you all |
@GrinZero Just curious, could you give some details about what you modified, and what effect you see? |
Update somethings:
I use |
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
andjest --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
Prisma Version
In our internal project, this was also an issue with Prisma 4.1.1
The text was updated successfully, but these errors were encountered: