clinicjs / node-clinic

Clinic.js diagnoses your Node.js performance issues
https://clinicjs.org
MIT License
5.67k stars 125 forks source link

If a process uses process.exit(0), generation of the report will fail #463

Open Stono opened 9 months ago

Stono commented 9 months ago

If you do this:

const interval = setInterval(() => {
  console.log('hi')
}, 1000)

const stop = () => {
  console.log('stopping')
  clearInterval(interval)
  process.exit(0)
}

process.on('SIGINT', stop)
process.on('SIGTERM', stop)

eg, capture SIGINT, SIGTERM and then process.exit once you've gracefully shut down, generation will fail with:

[    ] Received Ctrl+C, closing process...SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /Users/Karl.Stoney/.nvm/versions/node/v18.16.1/lib/node_modules/clinic/node_modules/@clinic/heap-profiler/src/analysis/index.js:126:50
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)

Changing that to setTimeout(() => { process.exit(0), 2500 }) for example; "fixes" it, giving the report time to finish.

This is quite a common pattern in all the apps i work on, and in very common frameworks like nextjs; https://github.com/vercel/next.js/blob/b3ad907d2bbe5f16988565ca6e99d434084bded0/packages/next/src/server/lib/start-server.ts#L285

alfonsograziano commented 9 months ago

Hey! I would like to work on it, can you please provide a basic repository to reproduce the issue?

Bruno-Meowtel commented 2 weeks ago

We are using Nuxt.js 3.x and I had a similar issue in our entry.mjs file.

@Stono's fix resolved the issue, but it'd be awesome if this was working right out of the box :)

Rigo-m commented 2 days ago

We are using Nuxt.js 3.x and I had a similar issue in our entry.mjs file.

@Stono's fix resolved the issue, but it'd be awesome if this was working right out of the box :)

Hi Bruno, how did you fix it in nuxt 3?