-
Notifications
You must be signed in to change notification settings - Fork 815
Make getting lazy properties on ServiceConfig threadsafe #1752
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |
|
|
||
| namespace Grpc.Net.Client.Internal.Configuration | ||
| { | ||
| internal struct ConfigProperty<TValue, TInner> where TValue : IConfigValue | ||
| internal sealed class ConfigProperty<TValue, TInner> where TValue : IConfigValue | ||
| { | ||
| private TValue? _value; | ||
| private readonly Func<TInner?, TValue?> _valueFactory; | ||
|
|
@@ -37,13 +37,23 @@ public ConfigProperty(Func<TInner?, TValue?> valueFactory, string key) | |
| { | ||
| if (_value == null) | ||
| { | ||
| var innerValue = inner.GetValue<TInner>(_key); | ||
| _value = _valueFactory(innerValue); | ||
|
|
||
| if (_value != null && innerValue == null) | ||
| // Multiple threads can get a property at the same time. We want this to be safe. | ||
| // Because a value could be lazily initialized, lock to ensure multiple threads | ||
| // don't try to update the underlying dictionary at the same time. | ||
| lock (this) | ||
| { | ||
| // Set newly created value | ||
| SetValue(inner, _value); | ||
| // Double-check locking. | ||
| if (_value == null) | ||
| { | ||
| var innerValue = inner.GetValue<TInner>(_key); | ||
| _value = _valueFactory(innerValue); | ||
|
|
||
| if (_value != null && innerValue == null) | ||
| { | ||
| // Set newly created value | ||
| SetValue(inner, _value); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. |
||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
_valueFactorycall into user code? Orinner.GetValue/SetValue? This reminds me a little bit of the locks I took inConfigurationManagerthat caused dotnet/runtime#61747.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's internal.