Repository navigation
Description
Description
I'm using ResrouceManager to access the resources file in satellite assemblies, and the function is working fine as expected, however I cannot get expected value if I turn on the ignore case for the Resrouce Manager.
Reproduction Steps
- I prepared a resource file which has 26 strings, the name is capital letter, and the value is lowercase letter. for example: A -> a; B -> b; ...; Y -> y; Z -> z
- I created 52 tasks to call the GetString method to get value for all alphabet ( including capital letters and lower case letters)
Following is the validation code to verify the value
static void TestResource(ResourceManager manager, string key, Flags flags)
{
try
{
var value = manager.GetString(key);
if(!key.ToLower().Equals(value))
{
flags.Exception = true;
Console.WriteLine($"{key} -> {value}");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Expected behavior
The expected value should be the lower case of the key.
Actual behavior
Below is the output, you can see the the value for E is not e, it's ☺, and some tasks have exception to get value.
E -> ☺
System.FormatException: Corrupt .resources file. Invalid offset '23593217' into data section.
at System.Resources.ResourceReader.AllocateStringForNameIndex(Int32 index, Int32& dataOffset)
at System.Resources.ResourceReader.ResourceEnumerator.get_Key()
at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString)
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
at System.Resources.ResourceManager.GetString(String name)
at MyApp.Program.TestResource(ResourceManager manager, String key, Flags flags)
Regression?
No response
Known Workarounds
The workaround is to add lock for the GetString method to prevent concurrent access.
Configuration
- Which version of .NET is the code running on?
.Net 6 - What OS and version, and what distro if applicable?
Windows 11, [Version 10.0.22000.856] - What is the architecture (x64, x86, ARM, ARM64)?
x64 - Do you know whether it is specific to that configuration?
I don't think so. - If you're using Blazor, which web browser(s) do you see this issue in?
N/A
Other information
The GetObject method in RuntimeResourceSet has two lock, and I believe we should use one lock for all cases because the second lock cannot lock it when multiple threads call this method.
Activity
danmoseley commentedon Aug 17, 2022
That seems like a bug as GetString should be thread safe.
It would be interesting to know whether this bug exists for .NET Framework.
tarekgh commentedon Aug 17, 2022
@danmoseley looks you are correct. Some fields in the
ResourceReadershould be accessed inside a lock but this does not happen in all cases. _store is a good example of that.danmoseley commentedon Aug 17, 2022
The locking in this and its related classes needs some auditing, and it would probably be helpful to add some
Debug.Assert(Monitor.IsEntered(..))and comments to make clear where protection is expected, what is shared state etc so it can be reasoned about more easily.buyaa-n commentedon Aug 17, 2022
Doesn't seem regression, adding to future milestone for now, @danmoseley @tarekgh feel free to prioritize as needed
danmoseley commentedon Aug 18, 2022
should we mark help wanted?
madelson commentedon Aug 30, 2022
I'd be interested in working on this.
danmoseley commentedon Aug 31, 2022
It's yours @madelson
Fix thread-safety issues with enumerating ResourceManager.
Fix thread-safety issues with enumerating ResourceManager. (#75054)
Fix thread-safety issues with enumerating ResourceManager.
Fix thread-safety issues with enumerating ResourceManager.
Fix thread-safety issues with enumerating ResourceManager.
[release/7.0] Fix thread-safety issues with enumerating ResourceManag…
[release/6.0] Fix thread-safety issues with enumerating ResourceManag…