Skip to content

System.Resources.ResourceManager is not thread safe when ignore case is enabled #74052

Closed

Description

@longnlp

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

  1. 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
  2. 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

  1. ghost added
    untriaged
    on Aug 17, 2022
  2. danmoseley commented on Aug 17, 2022

    @danmoseley
    Contributor

    That seems like a bug as GetString should be thread safe.
    It would be interesting to know whether this bug exists for .NET Framework.

  3. tarekgh commented on Aug 17, 2022

    @tarekgh
    Member

    @danmoseley looks you are correct. Some fields in the ResourceReader should be accessed inside a lock but this does not happen in all cases. _store is a good example of that.

  4. danmoseley commented on Aug 17, 2022

    @danmoseley
    Contributor

    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.

  5. buyaa-n commented on Aug 17, 2022

    @buyaa-n
    Contributor

    Doesn't seem regression, adding to future milestone for now, @danmoseley @tarekgh feel free to prioritize as needed

  6. added this to the Future milestone on Aug 17, 2022
  7. ghost removed
    untriaged
    on Aug 17, 2022
  8. danmoseley commented on Aug 18, 2022

    @danmoseley
    Contributor

    should we mark help wanted?

  9. added
    help wanted
    on Aug 18, 2022
  10. madelson commented on Aug 30, 2022

    @madelson
    Contributor

    I'd be interested in working on this.

  11. removed
    help wanted
    on Aug 30, 2022
  12. danmoseley commented on Aug 31, 2022

    @danmoseley
    Contributor

    It's yours @madelson

  13. added a commit that references this issue on Sep 3, 2022
    862e7fe
  14. ghost added
    in-pr
    on Sep 3, 2022
  15. added a commit that references this issue on Jan 3, 2023
    15fcb99
  16. ghost removed
    in-pr
    on Jan 3, 2023
  17. added 2 commits that reference this issue on Jan 27, 2023
    075c9ae
    dcf0257
  18. ghost locked as resolved and limited conversation to collaborators on Feb 2, 2023
  19. added a commit that references this issue on Feb 10, 2023
    3b33698
  20. added 2 commits that reference this issue on Mar 10, 2023
    6e6eab2
    677c557
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions