What are active handles in Node.js
Asked Answered
D

2

20

I see that my applications active handle count keeps on increasing. What exactly is the number of active handles ? Is this something i have to take care of preventing the app from crashing?

Dunn answered 8/2, 2018 at 17:56 Comment(3)
It means open files or database connections. If you see this grow without limit, it is definitely something to fix.Herzig
I'm using a module called puppeteer which is a headless browser controller and each time I open a page and closing, this value on pm2 monitor keeps on increasing. Any pointers on how to prevent / debug this ?Dunn
@Dunn I had the same problem with NightmareJS and solved it by calling .end() (which kills the Electron child process and closes the connection). I guess the equivalent for puppeteer would be to call browser.close().Liar
S
26

Active handles

A handle is a reference to an open resource like an opened file, a database connection or a request. To understand why handles might be active although they should have been closed, I give you a simple example:

const http = require('http');

http.createServer((req, res) => {
    if (req.url === '/secret-url') {
        return; // nobody should have access to this part of our page!
    }

    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World!');
}).listen(3000);

What does this code do? It runs a server on port 3000 and returns a Hello World message for any request, except for those going to the "secret URL". But there is a problem in this code. When we run into the "secret" if clause, we never close the connection. That means the client will keep the connection open for as long as he wants. We shouldve instead closed the connection. By making this mistake the number of active handles will increase, resulting in a memory leak.

Normally, memory leaks are much harder to detect as active handles might be passed from one function to another making it hard to track which of the code is responsible for closing the connection.

What does a rising number of active handles mean?

If you are seeing a constant increase in open handles, you very likely are having a memory leak in your code somewhere. Like in the example, you maybe forgot to close a resource

Memory leaks are in particular very bad, if you are planning on developing a script which should run for a long time, like a web server...

How to check for memory leaks?

There are various techniques to check for memory leaks. The easiest way is obviously keeping an eye on the memory. pm2 even has an option build in to restart the process in case the memory reaches a certain point. For more information on this topic, check out this guide.

What has this to do with puppeteer?

Two things. First, requests are very cheap. Even if you have a memory leak in your Node.js server application, you will only start seeing it in memory after a few thousand requests. In contrast to that, puppeteer is very expensive. Opening a Chromium browser will cost you memory in the range between 50 and 100 megabytes. So you should make sure that every browser you start, will be closed. Second, as the other answer already mentioned there are objects (like elementHandle) that you need to manually dispose to clear their resources.

Scansorial answered 3/5, 2019 at 16:20 Comment(0)
O
1

Puppeteer actually has a method to use when you're finished with your handles so the garbage collectors can do their job. You're supposed to use elementHandle.dispose() like this:

const bodyHandle = await frame.$('body');
const html = await frame.evaluate(body => body.innerHTML, bodyHandle);
// Once you're done with you handle just get rid of it
await bodyHandle.dispose();

Check out the docs:

Ortolan answered 25/2, 2018 at 17:35 Comment(3)
This does not answer the question that was asked at allBucolic
"Is this something i have to take care of preventing the app from crashing?" Was asked: Disposing your handles is a good way to prevent memory leaks thus preventing your app from random crashes.Ortolan
I'm not going to downvote this since I think the information is useful for those using puppeteer, but the original question's reference to puppeteer is only in the comments and the tag. It would be nice if the generic questions were answered, "what exactly is the number of active handles" and "Is this something I have to take care of...". The latter is a yes or no question and the answer really depends on what you're doing. I guess it wasn't really helpful of me to say this doesn't answer the question. What I should have asked is, "can someone answer the question more generically".Bucolic

© 2022 - 2024 — McMap. All rights reserved.