runtime icon indicating copy to clipboard operation
runtime copied to clipboard

IOptionsSnapshot can return empty value

Open vladar107 opened this issue 6 years ago • 5 comments

I've try to test IOptions and his friends and find some strange behaviour

Simple Web API

With appsetings.json Controller returns settings value

    private readonly IOptionsSnapshot _optionsSnapshot;

    public SettingsController(IOptionsSnapshot optionsSnapshot, IScopedService scopedService,
        ITransientService transientService)
    {
        _optionsSnapshot = optionsSnapshot;
    }
    [HttpGet]
    public string Get()
    {
        return _optionsSnapshot.Value.Test.ToString();
    }

Client

    static async Task Main(string[] args)
    {
        using var client = new HttpClient();
        var prevResponse = String.Empty;
        while (true)
        {
            var response = await client.GetStringAsync("http://localhost:5010/settings");
            if (response != prevResponse)
            {
                Console.WriteLine(response);
                prevResponse = response;
            }
        }
    }

After start i get next result

    ScopedService IOptionsSnapshot value: 0

After changing settings value

    ScopedService IOptionsSnapshot value: // it's empty
    ScopedService IOptionsSnapshot value: changed setting // next log, value was changed

Problems

Why is it empty at first time? If we compare with IOptionMonitor - on first call it has old value, on second call it has empty value too, and we have third call where value was changed.

Full version of this example here

Environment

sw_vers                                                              
ProductName:	Mac OS X
ProductVersion:	10.15.5
BuildVersion:	19F101

dotnet --version                                                          
3.1.301

vladar107 avatar Jun 13 '20 18:06 vladar107

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

Dotnet-GitSync-Bot avatar Jun 13 '20 18:06 Dotnet-GitSync-Bot

Thanks for the issue @vladar107, I checked out your sample code and I see that it tries to investigate what happens to IOptionsSnapshot and IOptionsMonitor once a config value changes.

As you pointed out in the description, once the config value changes, even though the IOptionsSnapshot value eventually gets updated to its changed value, for scoped and transient (not singleton), we do also see an intermediate empty/null value before it finalizes to its next value set inside config file.

It would be good to look at this more closely as the intermediate empty value showing up seems unexpected. I'll switch the milestone to future since the value becomes eventually consistent and will update to its changed value ultimately and I'm also not sure if there is a guarantee on how quick these updates are supposed to happen.

maryamariyan avatar Aug 26 '20 23:08 maryamariyan

Hello all. Just in case, I wanted to report the same behavior. I have a service using IOptionsSnapshot to get its settings, but, if you put it under the load, let's say 30+rps and start manually changing the appsettings.json file, you may notice that sometimes the object doesn't contain required settings as if after the appsettings.json update it still tries to load changes you've made even though it's IOptionsSnapshot and should not care during the request.

qchroman avatar Feb 03 '21 16:02 qchroman

Sporadically have the same issue. Configuration for application is reloaded from the Redis and injected vai DI container into the MVC controller as IOptionSnapshot<List<AppConfigItem>>. Internally (in the CosmosDB config provider) configuration is replaced in the single assignment base.Data = newConfigData so no inconsistency should appear due to the assignment. But I noticed that IOptionSnapshot<List<AppConfigItem>>.Value returns empty list with 0 items in it.

vitaly-pavluk avatar Nov 18 '21 18:11 vitaly-pavluk

Try to reproduce on .Net 6.0. Can't such behaviour.

dotnet --version
6.0.301

Looks like, the issue can be closed.

vladar107 avatar Jul 11 '22 16:07 vladar107