Skip to content

Conversation

@JamesNK
Copy link
Member

@JamesNK JamesNK commented May 19, 2022

Fixes #1713

if (_value != null && innerValue == null)
{
// Set newly created value
SetValue(inner, _value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the SetValue function also lock since it's public and called by other code?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ConfigProperty and IConfigValue are both internal so I think it's okay. If this was ever exposed anywhere, locking on this would be bad. But unless it's being exposed as object, I don't see how anyone would easily get a reference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the SetValue function also lock since it's public and called by other code?

The goal is to make the type thread-safe for reads, e.g. multiple threads can do this without error:

if (serviceConfig.LoadBalancingConfigs.Count > 0)
{
    // ....
}

If someone calls SetValue value by setting a property on another thread, e.g. serviceConfig.LoadBalancingConfigs = new List<LoadBalancingConfig>() then an error is fine.

if (_value == null)
{
var innerValue = inner.GetValue<TInner>(_key);
_value = _valueFactory(innerValue);
Copy link

@halter73 halter73 May 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I probably should have asked this before approving, but can _valueFactory call into user code? Or inner.GetValue/SetValue? This reminds me a little bit of the locks I took in ConfigurationManager that caused dotnet/runtime#61747.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's internal.

Co-authored-by: Brennan <brecon@microsoft.com>
@JamesNK JamesNK merged commit fedf5ab into grpc:master May 20, 2022
@JamesNK JamesNK deleted the jamesnk/serviceconfig-threadsafety branch May 20, 2022 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Error starting gRPC call" - concurrency issue.

3 participants