MacOS system starts the React front-end project and reports an error Error: EMFILE: too many open files, open solution

error scenario

Recently, I was developing a front-end micro-application of React. When the module was started, an error was reported when the module was built. Module build failed, Error: EMFILE: too many open files,

As shown below: 

Error: EMFILE: too many open files error, after investigation, is because a single micro-app project is large, and the release process has exceeded the default file monitoring limit of mac. Usually the reason for this type of error is that the number of file handles in linux or MacOS is not enough.

Solution

The solution is also relatively simple, just modify the maximum file size limit. Note that this is a temporary modification, and after a period of time, it will return to the default size . Permanent modification needs to be modified in the configuration file. My temporary modification can also meet the needs, so I will not change the configuration file.

Modify the default program of the ulimit command is launchd. 

First open the terminal, enter launchctl limit and press Enter, you can see the following picture: 

launchctl limit

 

 We enter sudo launchctl limit maxfiles 5000000 5000000 again and press Enter, here you will be prompted to enter a password

sudo launchctl limit maxfiles 5000000 5000000

Change the upper limit to a larger value and enter the password

When viewed again, the value has been changed 

 start again normally

 Extended knowledge:

1. Why does EMFILE appear, too many open files?

If a large number of concurrent calls are made to the file system, the number of file descriptors of the operating system will be used up in an instant, and EMFILE, too many open files will be thrown.

The significant gap between asynchronous I/O and synchronous I/O: Synchronous I/O because each I/O is blocked by each other, in the loop body, it is always called one after another, and there will be no excessive consumption of file descriptors. In many cases, the performance is also low;

For asynchronous I/O, although concurrency is easy to implement, it still needs to be given certain overload protection to prevent the performance of the underlying system from being overly squeezed.

2. Too many open files is a common error in Linux and MacOS systems. Literally, it means that the number of files opened by the program is too large, but the files here are not only the meaning of files, but also open communication links (such as socket ), the port being listened to, etc., so it can sometimes be called a handle (handle), and this error can usually be called the number of handles exceeds the system limit. The reason is that the process has opened more files and communication links than the system limit at a certain time.

The file here means the file handle (file handle) more accurately. Most of the cases where this error occurs is that the file handle (file handle) leaks. In layman's terms, the file handle is constantly being opened, but it is not after the use is completed. A normal shutdown results in a constant increase in the number of open files.

File handles leak for many reasons, not just opening files, common sources are: sockets, pipes, database connections, files. Under normal circumstances , the server itself will not suddenly report this error. It must be that the business program we deployed on the cloud server has opened too many files and has not closed them. As a result, the number of open files at the same time exceeds the system limit :

One situation is that the program itself needs to open a lot of file handles. In this case, the number of open files is greater than the limit of the number of open files of the system itself. At this time, we need to increase the limit of the system, just like the specific method given above. .

Another situation is that our program does not close normally after the file handle is used. Usually, the network connection is not closed, the file is not closed, etc. At this time, we need to fix the bug in the program and ensure that the opened file Both end up being closed, and the network connection is also closed.



references

1. Error in Nodejs: EMFILE, too many open files solution_keepupblw's blog-CSDN blog

2. The number of linux file handles is not enough Error: EMFILE: too many open files_Viogs' Blog-CSDN Blog

3. The principle and solution of "too many open files"_Foxconn Quality Inspector Zhang Quandan's Blog-CSDN Blog

4. Modify ulimit parameters under mac OSX_mac limits.conf_Jiangpan Dubu Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/qq_35624642/article/details/127229318