Search code examples
asp.net-coreentity-framework-coreinotify

Why do I get "The configured user limit (128) on the number of inotify instances has been reached" for MySQL queries from ASP.NET Core MVC


My ASP.NET Core MVC app reports an error after certain runs of a query to a 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.

The error was raised because the controller opened too many files and exceeded the limits of the iNotify setting (in the /proc/sys/fs/inotify/max_user_instances). But I was baffled when ASP.NET opened files on each HTTP request; why doesn't it close the file properly?

I am using Mysql.data.core and Mysql.data.entityframeworkcore providers.

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
        };
    }
}

The exception was raised only in the try/catch code block instead of immediately after the action is invoked.


Solution

  • Dmitry Pavlov's answer is wrong: you can use the default builder, since the last builder added wins, but doing so makes appsettings.json settings override all other config sources; both 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>();
            });
    }