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

🐛Fix concurrency issue getting abbreviations #1305

Merged
merged 1 commit into from
Aug 12, 2023

Conversation

angularsen
Copy link
Owner

@angularsen angularsen commented Aug 12, 2023

Fixes #1303

Change back to ConcurrencyDictionary, there was a subtle write-on-read when loading the initial values that could throw in UnitAbbreviationsCache.GetAbbreviations():

if (!AbbreviationsMap.TryGetValue(key, out IReadOnlyList<string>? abbreviations))
    AbbreviationsMap[key] = abbreviations = ReadAbbreviationsFromResourceFile(unitInfo.QuantityName, unitInfo.PluralName, culture);
System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
    at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
    at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
    at UnitsNet.UnitAbbreviationsCache.GetAbbreviations(UnitInfo unitInfo, IFormatProvider formatProvider)
    at UnitsNet.UnitAbbreviationsCache.TryGetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider, String[]& abbreviations)
    at UnitsNet.UnitAbbreviationsCache.GetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider)

Fixes #1303

Change back to `ConcurrencyDictionary`, there was a subtle write-on-read when loading the initial values that could throw.

```
System.InvalidOperationException: Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.
    at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
    at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
    at UnitsNet.UnitAbbreviationsCache.GetAbbreviations(UnitInfo unitInfo, IFormatProvider formatProvider)
    at UnitsNet.UnitAbbreviationsCache.TryGetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider, String[]& abbreviations)
    at UnitsNet.UnitAbbreviationsCache.GetUnitAbbreviations(Type unitType, Int32 unitValue, IFormatProvider formatProvider)
```
@codecov
Copy link

codecov bot commented Aug 12, 2023

Codecov Report

Patch coverage: 100% and no project coverage change.

Comparison is base (2dd76b8) 84% compared to head (f50a1b8) 84%.

Additional details and impacted files
@@          Coverage Diff           @@
##           master   #1305   +/-   ##
======================================
  Coverage      84%     84%           
======================================
  Files         336     336           
  Lines       31918   31920    +2     
======================================
+ Hits        26903   26905    +2     
  Misses       5015    5015           
Files Changed Coverage Δ
UnitsNet/CustomCode/UnitAbbreviationsCache.cs 88% <100%> (+<1%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@angularsen angularsen merged commit df2e62c into master Aug 12, 2023
3 checks passed
@angularsen angularsen deleted the agl/fix-concurrency-abbreviations branch August 12, 2023 16:20
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.

Concurrency checks in UnitValueAbbreviationLookup no longer working correctly
1 participant