In this lesson, you will learn how limits imposed by your operating system on system resources, specifically file descriptors, can cause your Node.js application to crash.
What is a File Descriptor?
In the world of Linux and Unix-based operating systems, "everything is a file." When your Node.js application opens a file, connects to a database, or listens for an incoming network request, the operating system assigns it a unique integer called a file descriptor (FD). This integer acts as a reference point so the OS can track the resource.
The Hidden Ceiling
Every process running on your machine has a strict limit on the number of file descriptors it can hold open at once. If your application logic continues to open files or sockets without closing them, you will eventually hit this system-defined ceiling.
Imagine you are running a server that logs every visitor to a new file but forgets to close the file handle after writing:
const fs = require('fs');
// A "leaky" loop that opens files without closing them
setInterval(() => {
fs.open('visitor_log.txt', 'a', (err, fd) => {
if (err) throw err;
// We keep opening, but never call fs.close(fd)!
});
}, 10);
Eventually, the OS will refuse to provide any more file descriptors. When Node.js tries to perform a system call to open a new resource, it will receive an EMFILE error (Too many open files). Because your application likely doesn't expect this, it results in an unhandled exception and a process crash.
Stability and Monitoring
Because Node.js runs as a single process, resource exhaustion is a "stop-the-world" event for that instance. If you leak file descriptors, the event loop can no longer facilitate new network connections or I/O operations. Stability depends on your ability to ensure that every resource opened via the C++ layer is promptly released back to the operating system once its job is finished.
Key Takeaway: Since the operating system imposes a hard limit on open file descriptors, failing to properly close I/O resources will inevitably lead to process instability and application failure.