FileSystemWatcher intermittently throws an exception on Linux

 
 
  • Gérald Barré

While hosting my ASP.NET Core websites on Linux, I encountered 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 instantiating a FileSystemWatcher. The Linux kernel limits the number of inotify instances that can be created to protect system resources. You can read more about inotify in its man page. On my VPS, the default limit is very low: only 128. You can safely raise this limit significantly. Each inotify instance uses about 1 kB of memory, so increasing it will not have a noticeable impact on memory usage.

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 are some useful 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?