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 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:
- https://github.com/dotnet/runtime/issues/16208
- https://unix.stackexchange.com/a/13757
- https://askubuntu.com/a/148177
- https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers
Do you have a question or a suggestion about this post? Contact me!
Enjoy this blog?💖 Sponsor on GitHub