Skip to content

[Uri] FxCop: System.Uri locks on a string. #19130

Closed

Description

@alexperovich

This came from running FxCop analyzers on corefx. Reproduce steps found here.

"E:\corefx\src\src.builds" (default target) (1) ->
"E:\corefx\src\System.Private.Uri\src\System.Private.Uri.builds" (Build target) (293) ->
"E:\corefx\src\System.Private.Uri\src\System.Private.Uri.csproj" (Build target) (44:7) ->
System\Uri.cs(2427,19): error CA2002: Do not lock on a reference of type 'string' as it has weak identity. Replace that with a lock against an object with strong identity. [E:\corefx\src\System.Private.Uri\src\System.Private.Uri.csproj]

Activity

  1. karelz commented on Oct 28, 2016

    @karelz
    Member

    What is the context/source of this bug?

  2. alexperovich commented on Oct 28, 2016

    @alexperovich
    MemberAuthor

    FxCop analyzer code analysis. I will add repro steps.

  3. changed the title [-]System.Uri locks on a string.[/-] [+]FxCop: System.Uri locks on a string.[/+] on Oct 28, 2016
  4. karelz commented on Oct 28, 2016

    @karelz
    Member

    From the dupe bug comment: https://github.com/dotnet/corefx/issues/11471#issue-175290581

    https://github.com/dotnet/corefx/blob/ca9e2b068e75898a3b35860632b1fec01cc5af93/src/System.Private.Uri/src/System/Uri.cs#L2199-L2206

                lock (_string)
                {
                    if ((_flags & Flags.MinimalUriInfoSet) == 0)
                    {
                        _info = info;
                        _flags = (_flags & ~Flags.IndexMask) | cF;
                    }
                }

    This is a scalability concern, e.g. this Uri could end up sharing the same lock as another that's locking on the same string instance, such as if the string is interned. Now, given the rarity of this, this might be the best option, as unless there's another object available on which to lock, this is likely the better option than allocating a new object per Uri just for the purposes of this lock. If there's another existing object that could be used that's not shared, that would be better. Even better would be getting rid of this lock entirely... since Uri to my knowledge is not deemed to be thread-safe, the purpose of this lock is unclear to me... maybe it could just be removed entirely?

    And comment https://github.com/dotnet/corefx/issues/11471#issuecomment-245026819

    Uri is immutable and I think that people often assume (correctly or not) that reading the same object from multiple threads is fine, so it makes sense to me to make Uri thread-safe.

    And it seems Uri was designed to be thread-safe, since there are some lock (_info) in it and also some comments about multi-threading.

    Maybe it would make sense to verify that Uri indeed is thread-safe and then change the documentation?

  5. davidsh commented on Oct 28, 2016

    @davidsh
    Contributor

    I would not change this lock. It might change behavior and System.Uri has large impacts on the codebase.

  6. davidsh commented on Oct 28, 2016

    @davidsh
    Contributor

    cc: @CIPop

  7. danmoseley commented on Nov 29, 2016

    @danmoseley
    Contributor

    @CIPop if you agree, then @alexperovich can baseline and close this.

  8. CIPop commented on Nov 29, 2016

    @CIPop
    Contributor

    This came from running FxCop analyzers on corefx.

    @danmosemsft I agree with @alexperovich that the initial FXCop bug should be fixed (not baselined).
    In general, I think we should have a clean analysis log (#3501).

    Maybe it would make sense to verify that Uri indeed is thread-safe and then change the documentation?

    @karelz This is a good idea but we would need to do some perf analysis first: would making it thread-safe loose a lot of perf?

  9. alexperovich commented on Nov 29, 2016

    @alexperovich
    MemberAuthor

    I can baseline the error and file an issue to fix. Added to dotnet/corefx#14095

  10. CIPop commented on Mar 22, 2017

    @CIPop
    Contributor

    We need to remove all instances where the Uri code is locking on a string and replace them with a proper lock objects.

    Uri is immutable and I think that people often assume (correctly or not) that reading the same object from multiple threads is fine, so it makes sense to me to make Uri thread-safe.

    I think this is a different, more complicated task and it shouldn't be addressed part of this issue.

  11. changed the title [-]FxCop: System.Uri locks on a string.[/-] [+][Uri] FxCop: System.Uri locks on a string.[/+] on May 2, 2018
  12. transferred this issue fromdotnet/corefxon Jan 31, 2020
  13. added this to the Future milestone on Jan 31, 2020
  14. self-assigned this
    on Mar 5, 2020
  15. modified the milestones: Future, 5.0.0 on Aug 18, 2020
  16. ghost locked as resolved and limited conversation to collaborators on Dec 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Netbughelp wantedtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions