42

the asp.net core MVC report error after certain times of query to the mysql database (on Ubuntu 14.04/16.04) with the following message: "The configured user limit (128) on the number of inotify instances has been reached." It can be identified that the error was raised because of the controller opened too many files and exceeded the limits of iNotify setting (in the /proc/sys/fs/inotify/max_user_instances). But I was just baffled when the ASP.NET opened files on each http request and why it doesn't close the file properly? Any one encounted this issue too? Remarks: I was using Mysql.data.core and mysql.data.entityframeworkcore provider.

    private static string classiferstring = "sports,outdoor,startup,pets,child,adult,elderly";

    [AllowAnonymous]
    [HttpGet]
    public async Task<object> Classify([FromQuery] string classifyword)
    {
        string[] classifers = classiferstring.Split(',');
        if (!classifers.Contains(classifyword))
        {
            return new
            {
                status = 0,
                info = "WrongClassifier",
                Data = ""
            };
        }

        try
        {
            var predata = await (from d in _context.descriptor
                              join a in _context.combination on d.ID equals a.ID
                              select new ProductsVM
                              {
                                  CREATETIME = a.CREATETIME,
                                  ID = a.ID,
                                  COMPANY = a.COMPANY,
                                  NAME = a.NAME,
                                  PRICE = a.PRICE,
                                  TYPE = a.TYPE,
                                  HEADPHOTO = a.HEADPHOTO,
                                  REMARK = a.REMARK,

                                  Tags = d.Tags,
                                  Classifier = d.Classifier,
                                  OriginName = d.OriginName,
                                  Briefing = d.Briefing
                              }).ToListAsync();
            var data = (from x in predata
                          where x.Classifier.Contains(classifyword.ToLower())
                          select x).ToList();

            if(predata.Count<=0)
            {
                return new
                {
                    status = 2,
                    info = "NoResult",
                    Data = ""
                };
            }else
            {
                return new
                {
                    status = 1,
                    info = "Success",
                    Data = data
                };
            };
        }
        catch(Exception e)
        {
            return new
            {
                status = 0,
                info = "Error",
                Data = e.Message
            };
        }
    }

Please notice the exception was raised only in the try/catch code block instead of immediately after the action is invokded.

Many thanks if anyone has some clue to resolve this problem.

imady
  • 453
  • 1
  • 5
  • 8
  • You failed to mention which provider you are using. Oracle's MySQL Provider is pretty shitty and the latest version completely broken if you do more than 1 query (it closes connection after first query). Likely there is something horribly broken with the Oracle provider so that it doesn't correctly recycle connections. My advice: throw away Oracle's provider and use some other – Tseng Apr 18 '17 at 10:20
  • Thanks for suggestion. Unfortunately I have to continue on MySQL provider for some reason. It's worse since I have to maintain connection simultaneously with a Java project to the database... Meanwhile I noticed that ASP.NET core might use lots of inotify instances for UseStaticFiles that under wwwroot. I use 'lsof -p pid |wc -l' command and found the dotnet process used hundred instances. – imady Apr 18 '17 at 14:32
  • It turns out it might not be a problem of mysql provider but a infrastructural bug of dotnet... The mysql provider was changed to Dapper and it works well untill the bug was reported again after several dotnet restarts... I will try to report this issue to Microsoft. – imady Apr 27 '17 at 02:12
  • 1
    I was talking about the Oracle's MySQL Provider for EntityFramework Core (since your question is tagged with efcore). Dapper doesn't use EF Core, just the underlying `MySqlConnection` and except for that, is agnostic about any database. i.e. pomello mysql driver doesn't have these issues with connection being closed after each query, which clearly indicates the bad quality of Oracles MySQL Provider for EF Core – Tseng Apr 27 '17 at 10:08

6 Answers6

70

The best solution I found so far is to increase the fs.inotify.max_user_instances in /etc/sysctl.conf by running this command:

echo fs.inotify.max_user_instances=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

Source: https://github.com/dotnet/aspnetcore/issues/8449#issuecomment-512275929

Creak
  • 4,021
  • 3
  • 20
  • 25
34

For containerized environments (where config won't change for a lifetime of a container) or on build servers, the way to mitigate the issue is to set environment variable

DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE=false

This allows to continue using Host.CreateDefaultBuilder and prevents the service from creating unnecessary inotify instanced.

Andrii Litvinov
  • 12,402
  • 3
  • 52
  • 59
  • 1
    Note DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE is for Host.CreateDefaultBuilder, not the legacy WebHost.CreateDefaultBuilder. – Tratcher Jun 18 '21 at 17:50
  • Thanks, I've updated my answer, not sure how I missed as generic host is what I use. – Andrii Litvinov Jun 19 '21 at 04:32
  • 1
    Thanks! I was really left scratching my head on this one, but your answer solved it for me. It would be great if you had a header in the beginning, highlighting that this is for containerized environments. It was really easy to miss that part when I first scrolled through. – Andreas Forslöw Dec 03 '21 at 11:34
  • 1
    @AndreasForslöw I'm glad that it helped and thanks for the heads up! I've updated the answer. – Andrii Litvinov Dec 03 '21 at 11:46
17

The reason why error happens is reloadOnChange cause the issue while accessing appSetting.json files.

the configured user limit (128) on the number of inotify instances has been reached

Set reloadOnChange to false:

.AddJsonFile($"appsettings.json", optional: true, reloadOnChange: false);

NOTE: if you also use the default WebHost.CreateDefaultBuilder, be aware that inside it reloadOnChange is also set to true.

So probably would be more safe to just configure your host from scratch - this is an example how to do that (a copy of Microsoft WebHost.CreateDefaultBuilder but without FileWatcher dependency and IISIntegration as you don't need IIS on Ubuntu).

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
4

@dmitry-pavlov is wrong, you can use the default builder as the last builder added wins - but doing so makes appsettings.json settings override all other config sources, env and command line. See https://github.com/dotnet/AspNetCore.Docs/pull/22391 where this will soon be officially documented. If you don't care about appsettings.json reloads, set it to false

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.AddJsonFile("appsettings.json",
                optional: true,
                reloadOnChange: false);
        })
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });
}
RickAndMSFT
  • 20,912
  • 8
  • 60
  • 78
3

I just had to close some visual studio code windows and especially files open in the visual studio code editor.

bit
  • 443
  • 1
  • 7
  • 19
0

I recently saw this problem on dot.net 5.0. In my case it was caused by using the bundle tag helper found here: https://github.com/meziantou/Meziantou.AspNetCore.BundleTagHelpers . It seems that tag helpers which use asp-append-version have been known to cause this issue, see here https://github.com/dotnet/runtime/issues/27272#issuecomment-525007303 .

Nick Evans
  • 11
  • 3