FileSystemWatcher intermittently throws an exception on Linux

 
 
  • Gérald Barré

In my quest of hosting my ASP.NET Core web sites on Linux, I encounter an intermittent exception:

System.IO.IOException: The configured user limit (128) on the number of inotify instances has been reached.
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   ...

This exception occurs when I instantiate a FileSystemWatcher. To protect itself, the Linux kernel has a maximum number of inotify objects it can create. You can read more about inotify in its man page. On my VPS, the limit is by default very low: only 128. You can safely increase that number to a much higher number. Each inotify instance is about 1kB, so it won't use too much memory 😃

Shell
sudo vim /etc/sysctl.conf

Then, add the configuration line:

fs.inotify.max_user_watches=50000

Finally, reload the configuration:

Shell
sudo sysctl -p

This should solve the issue.

Here's some great links about the max_user_watches flag:

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub