Jonas Sourlier
Jonas Sourlier

Reputation: 14435

How to detect a "file handle leak" in NodeJS?

I have just lost several days with the following situation:

I'm running a NodeJS console application, mostly from VS Code. The app scrapes various data and files from the web. Naturally, it deals with a high number of HTTP requests and disk reads and writes.

At some point in time, I noticed that the app started to just quit in the midst of a run. There was no indication in VS Code about the reason. I ruled out the common reasons:

This indicated to me that something deeper was the reason, like a memory leak. But the memory usage of the app was nearly constant, and not high.

After some more debugging, I noticed that shortly before the crash, all writeFile promises were hanging. The app could not write files anymore.

In the end, the reason for the crash was a file handle leak, i.e. there was a call to fs.createReadStream, and the file streams were not closed. At about 5'000 open file handles, the process crashed without any indication about the reason.

Is there a way I could have detected the reason for the crash, like some logging I could have enabled that would have shown the reason? Had it been a memory leak, I would have seen an increasing memory usage of the app, and maybe some "memory pressure" warnings. But the process was running fine (at least as indicated by SysInternals' Process Explorer), and then it was just crashing without any hint about the reason.

Upvotes: 3

Views: 1195

Answers (1)

Boogie
Boogie

Reputation: 818

Maybe this library can help you.

The library is call leaked-handles https://www.npmjs.com/package/leaked-handles, and ut is used for testing

Upvotes: 1

Related Questions