Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS - Print stack trace when stuck/frozen

Tags:

node.js

Is it possible to print the stack trace of a nodejs app when it is becoming very slow or froze to get information about performance spikes?

This would be incredibly helpful in instances where the reproduction for the issue is unknown.

In Java this saved hundreds of hours and was straight forward:

  1. spawn a new "watchdog" thread
  2. send a heartbeat every 50ms from the main thread to the watchdog
  3. if the "watchdog" doesn't receive a heartbeat for +200ms, log the main threads stacktrace

Is something like this possible with nodejs?

FI: the nodejs diagnostics report doesn't contain any javascript stack trace when initiated from a sig kill event.

like image 381
user2693017 Avatar asked Nov 12 '20 12:11

user2693017


People also ask

How to get the correct error stack in Node JS?

@isaacs answer is correct, but if you need more specific or cleaner error stack, you can use this function: function getCleanerStack () { var err = new Error (); Error.captureStackTrace (err, getStack); return err.stack; } This function is inspired directly from the console.trace function in NodeJS. Source code: Recent version or Old version.

How to get a stack trace for JavaScript when throwing an exception?

There are the following methods by which we can get a stack trace for JavaScript when throwing an exception. Using console.trace: The console object also has a method called console.trace () method, which gives you the trace on the console. Every time when it is called stack trace generate for the function.

How to track call stack in Node JS?

you can use node-stack-trace module which is a power full module to track call stacks.

What is the use of stack trace in Java?

This is a method of Java’s throwable class that prints the throwable Exception object as well as with other Information like the line number where Exception occurs and class name where the exception occurred. Throwable is super-class for all exception classes. Both the stack traces are discussed below:


Video Answer


1 Answers

You are looking for checking if event loop is blocked or slow. There is a npm package https://www.npmjs.com/package/blocked-at that detects slow synchronous execution and report where it started.

Usage:

const blocked = require('blocked-at');

blocked((time, stack) => {
  console.log(`Blocked for ${time}ms, operation started here:`, stack)
});

from scratch you can implement yourself a check in this way:

var interval = 500;
var interval = setInterval(function() {
    var last = process.hrtime();       
    setImmediate(function() {
        var delta = process.hrtime(last);
        if (delta > blockDelta) {
            console.log("node.eventloop_blocked", delta);
        }
    });
}, interval);

The idea is: if the timer doesn't fire after the expected time, this mean that event loop was blocked in some operation.

This snippet check if event loop is blocked for more than 500 ms. Isn't perfect, I'm suggest to use blocked-at for more robust control.

like image 84
Simone Nigro Avatar answered Sep 16 '22 16:09

Simone Nigro