Node-API/`LibraryEngine`: `$disconnect` does not free up memory / kill engine
See original GitHub issueWhen Node-API/engineType=library
is active, calling $disconnect()
does not free up the memory the instance of the Engine is using. This is because the engine is not killed as it is/was with the BinaryEngine
(child process was completely killed on stop()
), but only the disconnect is sent to the Engine. Then when another $connect
or Query (which implicitly connects) comes in, the same instance of the QueryEngine is used again (vs. a newly started one, as with the binary).
If you (unrealistically) create many PrismaClient
instances and just $disconnect
them after use, this leads to growing memory usage until the script ends.
Context:
- Observation made during investigation of https://github.com/prisma/prisma/issues/8989, https://github.com/prisma/prisma/issues/8989#issuecomment-909494922
- Reproduction: https://github.com/janpio/prisma-node-api-memory-investigation + https://github.com/janpio/prisma-node-api-memory-investigation/actions/runs/1187427937
- Hacky
LibraryEngine
that changes this: https://github.com/prisma/prisma/pull/9041 - Modified reproduction with this engine showing different properties: https://github.com/janpio/prisma-node-api-memory-investigation/actions/runs/1187430271
Reproduction above, running for 60 minutes:
- Currently: 25467 rss 1605.77 MB
- With
library
engine deleted on$disconnect
: 43100 rss 288.34 MB
We think this is a problem, but not of highest priority and can be solved down the line.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top GitHub Comments
Yes, that would be awesome. It might be related to the original problem I describe above, but is fundamentally a different area. If your reproduction helps understanding that problem in isolation, that would be amazing.
That cleanup code is pretty neat by the way - although a tiny bit scary. Good that you scoped it to the current user etc.
I found out that the connections goes into a “idle” state. Either
$disconnect
isn’t working properly or it is putting the connection in “idle” state.I found a workaround to keep the connections down with a SQL script, which you either call within the middleware or teardown with the help of the
$executeRawUnsafe
function.This is the SQL script:
This cleans up all the “idle” connections that has been idling for 10 seconds or more.
Should I open another issue so someone could try to replicate & investigate to see if it is indeed a bug with
$disconnect
?