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

Make getting lazy properties on ServiceConfig threadsafe #1752

Merged
merged 3 commits into from
May 20, 2022

Conversation

JamesNK
Copy link
Member

@JamesNK JamesNK commented May 19, 2022

Fixes #1713

test/Grpc.Net.Client.Tests/ServiceConfigTests.cs Outdated Show resolved Hide resolved
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