Skip to content
This repository was archived by the owner on Nov 7, 2018. It is now read-only.

Thread safety in OptionsManager #88

Closed
fedotovalex opened this issue Oct 9, 2015 · 4 comments
Closed

Thread safety in OptionsManager #88

fedotovalex opened this issue Oct 9, 2015 · 4 comments
Assignees
Milestone

Comments

@fedotovalex
Copy link

In the current implementation of OptionsManager, if I do something like this:

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyOptions>(options => {
          // some code, maybe not trivial
    });
}

and then later, from multiple threads:

var options = services.GetRequiredService<IOptions<MyOptions>>().Value;

the configuration action may be invoked more than once, which is not intuitively expected.

If this is the desired behavior, it needs to be documented very clearly. Personally, I would prefer the options manager to deal with concurrency issues and guarantee that the configuration action is only invoked once.

@herecydev
Copy link

Isn't it dangerous to assume any default lifetime? I certainly wouldn't expect that the framework to bind singleton for you. Though wouldn't this sort out your issue.

@HaoK
Copy link
Member

HaoK commented Nov 12, 2015

By default AddOptions adds OptionsManager as a singleton, so it's only done once in the normal case.

@HaoK HaoK closed this as completed Nov 12, 2015
@divega
Copy link

divega commented Nov 12, 2015

@HaoK I believe @fedotovalex is referring to the case of two threads (e.g. two concurrent requests) invoking .Value concurrently on the same singleton. I just checked and we don't have any code dealing with concurrency? Didn't we have it?

@divega divega reopened this Nov 12, 2015
@divega divega added this to the 1.0.0-rc2 milestone Nov 12, 2015
@HaoK
Copy link
Member

HaoK commented Dec 10, 2015

ec13249

@HaoK HaoK closed this as completed Dec 10, 2015
@HaoK HaoK added 3 - Done and removed 2 - Working labels Dec 10, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants