Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOptionsSnapshot can return empty value #37860

Closed
vladar107 opened this issue Jun 13, 2020 · 5 comments
Closed

IOptionsSnapshot can return empty value #37860

vladar107 opened this issue Jun 13, 2020 · 5 comments
Labels
area-Extensions-Options bug question Answer questions and provide assistance, not an issue with source code or documentation.
Milestone

Comments

@vladar107
Copy link

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
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jun 13, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

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

@vladar107 vladar107 changed the title IOptionsSnapshot can return empty value [area-Extensions-Options] IOptionsSnapshot can return empty value Jun 13, 2020
@vladar107 vladar107 changed the title [area-Extensions-Options] IOptionsSnapshot can return empty value IOptionsSnapshot can return empty value Jun 13, 2020
@maryamariyan maryamariyan added this to Untriaged-NoMilestone in ML, Extensions, Globalization, etc, POD. via automation Jun 18, 2020
@maryamariyan maryamariyan removed the untriaged New issue has not been triaged by the area owner label Jul 9, 2020
@maryamariyan maryamariyan modified the milestone: 6.0.0 Jul 9, 2020
@maryamariyan maryamariyan added question Answer questions and provide assistance, not an issue with source code or documentation. customer assistance labels Jul 9, 2020
@maryamariyan maryamariyan added this to the 5.0.0 milestone Jul 9, 2020
@maryamariyan
Copy link
Member

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 maryamariyan modified the milestones: 5.0.0, Future Aug 26, 2020
@maryamariyan maryamariyan moved this from N5.0 to Uncommitted in ML, Extensions, Globalization, etc, POD. Oct 5, 2020
@maryamariyan maryamariyan moved this from Uncommitted to Future in ML, Extensions, Globalization, etc, POD. Feb 3, 2021
@qchroman
Copy link

qchroman commented Feb 3, 2021

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.

@vitaly-pavluk
Copy link

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.

@vladar107
Copy link
Author

vladar107 commented Jul 11, 2022

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

dotnet --version
6.0.301

Looks like, the issue can be closed.

@dotnet dotnet locked as resolved and limited conversation to collaborators Aug 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Options bug question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

7 participants