FileSystemWatcher intermittently throws an exception on Linux

  • ASP.NET Core
  • .NET
  • Web

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 creates. 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 😃

sudo vim /etc/sysctl.conf

Then, add the configuration line:

fs.inotify.max_user_watches=50000

Finally, reload the configuration:

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 on Twitter or by email!

Follow me:
Enjoy this blog?Buy Me A CoffeeDonate with PayPal