node-clinic icon indicating copy to clipboard operation
node-clinic copied to clipboard

bubbleprof Error: premature close

Open yvz5 opened this issue 3 years ago • 12 comments

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

  • Clinic.js version: v12.0.0

  • bubbleprof version: v9.0.0

  • Node.js version: v16.19.0

  • npm 8.19.3

  • Operating system and version: Linux USERNAME 6.0.6-76060006-generic #202210290932~1669062050~22.04~d94609a SMP PREEMPT_DYNAMIC Mon N x86_64 x86_64 x86_64 GNU/Linux

yvz5 avatar Dec 20 '22 14:12 yvz5

Can you provide a minimal repro?

simoneb avatar Dec 20 '22 14:12 simoneb

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);
  });
});

yvz5 avatar Dec 20 '22 14:12 yvz5

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)

taylor-knapp avatar Jan 12 '23 19:01 taylor-knapp

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

clinic % npm run test:express

> [email protected] 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

marco-ippolito avatar Jan 13 '23 14:01 marco-ippolito

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!

RafaelGSS avatar Jan 13 '23 16:01 RafaelGSS

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

AndrejGajdos avatar Jan 19 '23 08:01 AndrejGajdos

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

yvz5 avatar Jan 22 '23 20:01 yvz5

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 :(

Nickersoft avatar Jun 29 '23 20:06 Nickersoft

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

lgian4 avatar Aug 02 '23 04:08 lgian4

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)

rluvaton avatar Aug 11 '23 09:08 rluvaton

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

RafaelGSS avatar Aug 11 '23 14:08 RafaelGSS