-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Fix torn write of struct causing crash in symbol finder. #69927
Conversation
@@ -8,146 +8,145 @@ | |||
using Roslyn.Utilities; |
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.
view with whitespace off.
Contract.ThrowIfNull(value, nameof(value)); | ||
Contract.ThrowIfNull(_concatenatedLowerCaseWords, nameof(_concatenatedLowerCaseWords)); | ||
Contract.ThrowIfTrue(_nodes.IsDefault, $"{nameof(_nodes)}.{nameof(_nodes.IsDefault)}"); | ||
Contract.ThrowIfTrue(_edges.IsDefault, $"{nameof(_edges)}.{nameof(_edges.IsDefault)}"); |
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.
this is the important part. ideally so we get a dump that tells us which of these is the problem.
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.
Is this going to stay in the PR since you think you've figured out the problem?
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.
yeah, i don't mind it :)
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.
{ | ||
get | ||
{ | ||
_spellChecker ??= CreateSpellChecker(Checksum, _nodes); |
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.
SpellChecker?
was a Nullable<SpellChecker>
. This is just a pure struct which can have torn writes. By moving to a StrongBox<SpellChecker>
we have teh runtime guarantee that if _spllChecker
is not null then _spellChecker.Value
is 'completely written'.
{ | ||
Logger.Log(FunctionId.SymbolTreeInfo_ExceptionInCacheRead); | ||
var rawSpellChecker = SpellChecker.TryReadFrom(reader); | ||
spellChecker = rawSpellChecker is null ? null : new(rawSpellChecker.Value); |
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.
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.
yes. if we can't read in teh spell checker (should never happen, but we're resilient to it anyways), we can still proceed as the new spell checker can be lazily created.
/// <summary> | ||
/// Explicitly boxed so that we can safely initialize/read this across threads without the need for a lock. | ||
/// </summary> | ||
private StrongBox<SpellChecker>? _spellChecker; |
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.
❗ Since _spellChecker.Value
is never written after construction, and there are no other instantiations of the SpellChecker
type, this change is functionally equivalent to (but much more complicated than) just making SpellChecker
a reference type. You could further simplify things by using the helpers from #69017 to initialize the field on first use.
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.
The other option if you want to keep this a value type is to use this helper:
roslyn/src/Compilers/Core/Portable/InternalUtilities/RoslynLazyInitializer.cs
Lines 26 to 27 in bb804d5
public static T EnsureInitialized<T>([NotNull] ref T? target, ref bool initialized, [NotNullIfNotNull(nameof(syncLock))] ref object? syncLock, Func<T> valueFactory) | |
=> LazyInitializer.EnsureInitialized<T>(ref target!, ref initialized, ref syncLock, valueFactory); |
The implementation of EnsureInitialized
contains a lock-free fast path for the case where initialized
is true (set at the end of initialization). You may need to use PooledDelegates
to avoid a delegate allocation on the fast path as well.
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.
Thanks. Moved to just be a refernece type. That fits best here for me :)
SpellChecker.FindSimilarWords(ref similarNames.AsRef(), name, substringsAreSimilar: false); | ||
// Ensure the spell checker is initialized. This is concurrency safe. Technically multiple threads may end | ||
// up overwriting the field, but even if that happens, we are sure to see a fully written spell checker as | ||
// the runtime guarantees that the initialize of the SpellChecker instnace completely written when we read |
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.
// the runtime guarantees that the initialize of the SpellChecker instnace completely written when we read | |
// the runtime guarantees that the initialize of the SpellChecker instance completely written when we read |
Fixes #69919