FileSystemWatcher intermittently throws an exception on Linux

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 instanciate 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:

Leave a reply