dotnetcore 2.0 on Linux suffering inotify errors

I am currently writing my first dotnet core 2.0 application deployed to a Linux environment. The development has been on a Mac using a combination of the dotnet cli and Visual Studio for Mac.

In deploying the app to a QA site, we experienced an error I’ve never seen before

An exception occured reading app key DeviceId default value  applied.
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()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at app.Shared.Utilities.GetAppConfig[T](String key, T defaultValue) in /var/app/source/app/app.Shared/Utilities.cs:line 33
   at System.IO.FileSystemWatcher.StartRaisingEvents()
   at System.IO.FileSystemWatcher.StartRaisingEventsIfNotDisposed()
   at Microsoft.Extensions.FileProviders.Physical.PhysicalFilesWatcher.CreateFileChangeToken(String filter)
   at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
   at Microsoft.Extensions.Configuration.Json.JsonConfigurationSource.Build(IConfigurationBuilder builder)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()

I didn’t know where to start so I Googled the issue and landed on some StackOverflow questions and GitHub issues associated with the same issue. The most helpful questions and answers came from the following sources.

My solution was to replace the call to WebHost.CreateDefaultBuilder(..) with a modified version of Microsoft’s implementation but with the following changes:

  • Make appsettings.json a required file (this was for my needs, you can change is to not be required)
  • Remove adding appsettings.{env}.json as we don’t use it. I am considering add this back to the gist.
  • Disable reloading the configuration when appsettings.json is modified (this is what instantiates FileSystemWatcher and causes our error)
  • Remove IIS integration (as we are deploying the app on Linux-based docker containers)

The modified method is available as a GitHub gist.

Leave a Reply

Your email address will not be published. Required fields are marked *