Prisma-client-js: Zombie processes on process.exit()

Created on 6 Apr 2020  Â·  6Comments  Â·  Source: prisma/prisma-client-js

Bug description

I used hyperfine to run this to get a understanding of how long it'll take to create 100 text in the db. Moreover, It did a total of 10 runs, and created 10 clients. You can use any other benchmark tool and it'll yield similar results.

➜  hyperfine "node scripts/zombie.js"
Benchmark #1: node scripts/zombie.js
  Time (mean ± σ):      1.230 s ±  0.122 s    [User: 364.1 ms, System: 80.2 ms]
  Range (min … max):    1.039 s …  1.477 s    10 runs

As you can see, it created the clients, finished the benchmark, but did not clean up the processes,

I can confirm they are dead processes, because when I use dumb-init, they exit after parent nodfe process is gone.

dumb-init hyperfine "node scripts/zombie.js"

How to reproduce

The reproducable scripts can be found here: https://github.com/entrptaher/prisma-issues#zombie-process

git clone https://github.com/entrptaher/prisma-issues
cd prisma-issues
yarn
yarn prisma migrate save --experimental
yarn prisma migrate up --experimental
yarn prisma generate

Then run the following script multiple times,

node scripts/zombie.js

Or even easier, just use process.exit() after any prisma query on any project.

Expected behavior

The clients should exit if parent process are not connected anymore.

Prisma information

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

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

model Text {
  id   Int @default(autoincrement()) @id
  data String?
}

Environment & setup

Database: sqlite
Node Version: v11.15.0
Npm Version: 6.7.0
Prisma: 2.0.0-beta.1
Platform : debian-openssl-1.1.x
Hyperfine: hyperfine 1.9.0
bu2-confirmed kinbug

Most helpful comment

Sometimes, if the engine/client panics, they are left zombified as well. I think it should be handled by the engine. If a nodejs process is crashed, everything inside of it, all child processes also crashes, that's how it normally works.

All 6 comments

Thanks for the beautiful reproduction! I can reproduce and we'll look into it.

The process does not call the disconnect function on the client so that is expected I think?

Sometimes, if the engine/client panics, they are left zombified as well. I think it should be handled by the engine. If a nodejs process is crashed, everything inside of it, all child processes also crashes, that's how it normally works.

Thanks for reporting! The disconnect call should not be necessary, instead things should be cleaned up automatically.

Unfortunately, Node.js doesn't clean up the child processes properly, if process.exit has been called.

I added more process event listeners to make sure we clean up the children to not have dangling zombie processes.

Thanks a lot for the update @timsuchanek. Could you elaborate a bit more in detail how this is related to this issue? https://github.com/prisma/prisma-client-js/issues/540

540 is about not keeping Node.js busy once all the work is done.

This is about - Node.js decided to exit, now Prisma needs to make sure, that we clean up properly.

Was this page helpful?
0 / 5 - 0 ratings