clinicjs / node-clinic

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

bubbleprof Error: premature close #378

Closed yvz5 closed 1 month ago

yvz5 commented 1 year ago

Expected Behavior

it should create bubbleprof report

Current Behavior

it exits with error message: Error: premature close

[   =] Analysing dataError: premature close
    at onclosenexttick (/usr/lib/node_modules/clinic/node_modules/end-of-stream/index.js:53:86)
    at processTicksAndRejections (node:internal/process/task_queues:78:11)

Steps to Reproduce (for bugs)

clinic bubbleprof --on-port="ab -k -c 10 -n 250 http://localhost:8080/" -- node --max-http-header-size=32768 dist/main.js

Sample upload

N/A

Environment

simoneb commented 1 year ago

Can you provide a minimal repro?

yvz5 commented 1 year ago

I think it might be a problem with the SIGTERM handling. If I remove that process.on callback, it manages to create the report

import express from "express";
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello World!");
});

const server = app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

process.on("SIGTERM", () => {
  console.log("received signal: SIGTERM");
  server.close((err) => {
    if (err) {
      console.error("error closing server", err);
      process.exit(1);
    }

    console.log("service is closed");

    process.exit(0);
  });
});
taylor-knapp commented 1 year ago

I am getting the same error.

Versions:

❯ clinic --version
v12.0.0
❯ node --version
v16.16.0
❯ uname -a
Darwin ${USERNAME} 22.2.0 Darwin Kernel Version 22.2.0: Fri Nov 11 02:04:44 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8103 arm64 => Ventura

Source test.js file:

const http = require("http");

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello World");
});

server.listen(3000, () => console.log('listening'));

Command:

❯ clinic bubbleprof -- node src/test.js
To generate the report press: Ctrl + C
Warning: The code is transpiled, bubbleprof does not support source maps yet.
listening
[====] Analysing dataError: premature close
    at onclosenexttick (/Users/taylorknapp/.nvm/versions/node/v16.16.0/lib/node_modules/clinic/node_modules/end-of-stream/index.js:53:86)
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
marco-ippolito commented 1 year ago

I've created a repro of this bug. On mac m1 it created the report but outputs this error:

clinic % npm run test:express

> tests@1.0.0 test:express
> clinic bubbleprof --on-port='ab -k -c 10 -n 250 http://localhost:8080/' -- node --max-http-header-size=32768 express.js

EXPRESS: Example app listening on port 3000
This is ApacheBench, Version 2.3 <$Revision: 1901567 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)

Test aborted after 10 failures

apr_socket_connect(): Invalid argument (22)
Analysing data
Generated HTML file is file:

It is probably related to SIGTERM, I'll conduct further investigation. Also I've found a few issue related to probably the similar problem: https://github.com/clinicjs/node-clinic/issues/336 expecially this one: https://github.com/clinicjs/node-clinic/issues/73 https://github.com/clinicjs/node-clinic/issues/293 @RafaelGSS

RafaelGSS commented 1 year ago

IIRC this is a bug in the end-of-stream (see https://github.com/mafintosh/end-of-stream/issues/30). I've faced it a few times and we could either use another library or fix this one. Let me know if you find something else!

AndrejGajdos commented 1 year ago

I have the same issue. Why was the original issue closed?

yvz5 commented 1 year ago

@AndrejGajdos I can confirm that it works if you dont handle SIGTERM in your code

Nickersoft commented 1 year ago

I just ran into this issue as well – I'm not manually handling SIGTERM AFAIK, but I am using NestJS. Not sure if maybe something is happening under the hood there. Sad because I really needed to use bubbleprof :(

lgian4 commented 1 year ago

I try to manually end the task in taskmanager by its PID code. you can find the PID code by opening the .clinic folder and finding the latest folder, and find the task with PID by search from he top of task manager

rluvaton commented 1 year ago

Having the same issue with this:

Run:

clinic bubble -- node  ./index.js
'use strict';
const { it } = require('node:test');

async function run(n, type) {
  const promises = new Array(n);

  // eslint-disable-next-line no-unused-vars
  let avoidV8Optimization;

  for (let i = 0; i < 1e4; i++) {
    promises[i] = it(`${i}`, () => {
      avoidV8Optimization = i;
    });
  }

  await Promise.all(promises);
}

function main({ n, type } = {n: 1e4, type: 'sync'}) {
  // bench.start();
  run(n, type);
}

main();

and got:

[==  ] Analysing dataError: premature close
    at onclosenexttick (/~.nvm/versions/node/v20.5.0/lib/node_modules/clinic/node_modules/end-of-stream/index.js:53:86)
    at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
RafaelGSS commented 1 year ago

That's due to https://github.com/clinicjs/node-clinic/issues/378#issuecomment-1382077760

TimGuendel commented 5 months ago

Same problem here (Nuxt 3.12, Node 20.14).

MagicIndustries commented 2 months ago

Does anyone have a workaround for this? I'm getting it constantly trying to use bubbleprof and end-of-stream doesn't seem to have been updated in years.

I'm on the latest version of clinic, and it doesn't seem to matter how much I strip out of my application I keep getting this error when I hit CTRL-C on the clinic command. Im on node v20.11.1

I did have code handling sigint and sigterm, but I get the error with or without that code.

simoneb commented 2 months ago

We are not actively maintaining this project, I added a note in the readme to convey this. Apologies for any troubles this may cause, contributions are always welcome.