The Wayback Machine - https://web.archive.org/web/20201212161127/https://github.com/dotnet/corefx/pull/14289
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

Enable CA2002:DoNotLockOnObjectsWithWeakIdentity #14289

Merged
merged 3 commits into from Dec 8, 2016

Conversation

@alexperovich
Copy link
Member

@alexperovich alexperovich commented Dec 7, 2016

This change enables CA2002 and adds baseline files for current failures.

This change enables CA2002 and adds baseline files for current failures.
@danmosemsft
Copy link
Member

@danmosemsft danmosemsft commented Dec 7, 2016

Do we care about this rule given we don't have Appdomains? The doc says "An object is said to have a weak identity when it can be directly accessed across application domain boundaries."

@jkotas

@alexperovich
Copy link
Member Author

@alexperovich alexperovich commented Dec 7, 2016

This doesn't just have impact when app domains are involved. This also triggers when you lock on strings or objects that are public. Locking on strings in particular can be a very problematic pattern due to string interning. You should ideally only ever lock on private readonly object foo = new object(); fields.

@jkotas
Copy link
Member

@jkotas jkotas commented Dec 7, 2016

The description of the rule at https://msdn.microsoft.com/en-us/library/ms182290.aspx does not match what the rule actually does. The description says that it kicks it for a very small set of types. Looking at this PR, it seems to be hitting for many other types.

Historically, we assumed that it is a good hygiene to never lock on the public types. It is what this rule seems to be actually enforcing. It does not have much to do with AppDomains. I think we should keep it.

@danmosemsft
Copy link
Member

@danmosemsft danmosemsft commented Dec 8, 2016

Alex you might wish to encourage the doc owner (?Roslyn) to fix their doc then.

@@ -0,0 +1,3 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity", Scope = "member", Target = "System.Diagnostics.DelimitedListTraceListener.#set_Delimiter(System.String)")]

This comment has been minimized.

@stephentoub

stephentoub Dec 8, 2016
Member

What is the purpose of the lock in this case? It seems to be pointless in the current implementation... the right fix may be to just remove the lock rather than suppressing the warning.

@@ -56,6 +56,8 @@ protected override void Dispose(bool disposing)
}
}

//TODO: Add this to FxCopBaseline.cs once https://github.com/dotnet/roslyn/issues/15728 is fixed

This comment has been minimized.

@stephentoub

stephentoub Dec 8, 2016
Member

This is an internal type, so we could just change it from an explicit to an implicit interface implementation without impacting public surface area.

This comment has been minimized.

@alexperovich

alexperovich Dec 8, 2016
Author Member

I wasn't looking closely at all of the usages. This change is just about getting all of the baselines in. Then we can fix the individual issues and remove the suppression.

@@ -0,0 +1,3 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity", Scope = "member", Target = "System.Uri.#CreateUriInfo(System.Uri+Flags)")]

This comment has been minimized.

This comment has been minimized.

@alexperovich

alexperovich Dec 8, 2016
Author Member

added

@@ -0,0 +1,49 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Microsoft.Reliability", "CA2002:DoNotLockOnObjectsWithWeakIdentity", Scope = "member", Target = "System.IO.SyncTextReader.#Dispose(System.Boolean)")]

This comment has been minimized.

@weshaggard

weshaggard Dec 8, 2016
Member

Any reason these cannot be applied directly to the member?

This comment has been minimized.

@alexperovich

alexperovich Dec 8, 2016
Author Member

They can be, but I only want to do that if they aren't things we want to fix.

This comment has been minimized.

@stephentoub

stephentoub Dec 8, 2016
Member

They can be, but I only want to do that if they aren't things we want to fix.

The vast majority of these are things we're not going to change.

This comment has been minimized.

@alexperovich

alexperovich Dec 8, 2016
Author Member

Then we should review the cases and add justifications to the suppressions.

This comment has been minimized.

@weshaggard

weshaggard Dec 9, 2016
Member

Did you file an issue to review these and actually apply them to the members with justification?

This comment has been minimized.

@alexperovich

alexperovich Dec 9, 2016
Author Member

#14382

@alexperovich
Copy link
Member Author

@alexperovich alexperovich commented Dec 8, 2016

@dotnet-bot test Innerloop Ubuntu14.04 Release Build and Test please (S.C.Concurrent test failure)

@alexperovich alexperovich merged commit f8d1028 into dotnet:master Dec 8, 2016
8 checks passed
8 checks passed
Innerloop CentOS7.1 Debug Build and Test Build finished.
Details
Innerloop CentOS7.1 Release Build and Test Build finished.
Details
Innerloop OSX Debug Build and Test Build finished.
Details
Innerloop OSX Release Build and Test Build finished.
Details
Innerloop Ubuntu14.04 Debug Build and Test Build finished.
Details
Innerloop Ubuntu14.04 Release Build and Test Build finished.
Details
Innerloop Windows_NT Debug Build and Test Build finished.
Details
Innerloop Windows_NT Release Build and Test Build finished.
Details
@alexperovich alexperovich deleted the alexperovich:codeAnalysis branch Dec 8, 2016
@karelz karelz modified the milestone: 1.2.0 Dec 9, 2016
steveharter added a commit to steveharter/dotnet_corefx that referenced this pull request Dec 19, 2016
* Enable CA2002:DoNotLockOnObjectsWithWeakIdentity

This change enables CA2002 and adds baseline files for current failures.

* Add FxCop baseline for System.Console

* Add reference comment for System.Uri issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

7 participants
You can’t perform that action at this time.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.