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

Readonly Structs vs Classes have dangerous inconsistency - failed spin lock #17310

Closed
Drawaes opened this issue Feb 22, 2017 · 11 comments
Closed

Comments

@Drawaes
Copy link

Drawaes commented Feb 22, 2017

Version Used: Any

Steps to Reproduce:

  1. Create private readonly Spinlock _lock;
  2. take a lock

Expected Behavior:
That the code is protected by the lock... or the compiler throws an error

Actual Behavior:
struct is copied and lock is taken on the copy no compiler warning

This seems like it should either not compile or should work. Because the readonly struct is copied the lock never actually works

will do nothing, apparently this is a known issue, that is defended against only by comments?

https://github.com/dotnet/coreclr/search?l=C%23&q=mutable+readonly&type=Code&utf8=%E2%9C%93

@Drawaes Drawaes changed the title Lock() doesn't work on a readonly struct and no error SpinLock doesn't work on a readonly struct and no error Feb 22, 2017
@Drawaes
Copy link
Author

Drawaes commented Feb 26, 2017

Got caught by this again today, readonly GCHandles never show as deallocated when free is called for the same reason

@Drawaes
Copy link
Author

Drawaes commented Feb 26, 2017

https://gist.github.com/Drawaes/84704a1a8ce8eb9e3bca6ef364d8beb2

Simple example to repo the behavior

@Drawaes Drawaes changed the title SpinLock doesn't work on a readonly struct and no error Readonly Structs vs Classes have dangerous inconsistency - failed spin lock Feb 26, 2017
@CyrusNajmabadi
Copy link
Member

Seems like an appropriate place for warning waves.

@Korporal
Copy link

Korporal commented Mar 7, 2017

The example you refer to does not use any kind of lock, am I missing something?

@benaadams
Copy link
Member

@Korporal here's an example https://github.com/dotnet/coreclr/blob/master/src/mscorlib/shared/System/Buffers/ConfigurableArrayPool.cs#L169

It won't give you a compile error or runtime error if you make it readonly; it just won't lock

@jcouv jcouv added this to the 15.later milestone Sep 30, 2017
@jcouv
Copy link
Member

jcouv commented Dec 13, 2017

@jcouv
Copy link
Member

jcouv commented Dec 26, 2017

Moving to IDE as suggestion for analyzer, since LDM concluded this should not be a compiler error.

@jcouv jcouv removed this from the 15.6 milestone Dec 26, 2017
@jinujoseph jinujoseph added this to the Unknown milestone Jan 8, 2018
@Drawaes
Copy link
Author

Drawaes commented Jul 19, 2018

Any news on this? I think @GrabYourPitchforks had some ideas around this as well

@CyrusNajmabadi
Copy link
Member

@mavasani Can this be moved to Roslyn-Analyzers?

@mavasani
Copy link
Contributor

mavasani commented Mar 2, 2020

Tagging @stephentoub - I believe this is tracked by dotnet/roslyn-analyzers#2811 and there is already a WIP PR for it: dotnet/roslyn-analyzers#2831

@CyrusNajmabadi
Copy link
Member

Great. Closing out then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants