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

Open
Drawaes opened this Issue Feb 22, 2017 · 8 comments

Comments

8 participants
@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

@gafter gafter added this to Backlog in Compiler: Warning Waves Feb 23, 2017

@Drawaes

This comment has been minimized.

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

This comment has been minimized.

Copy link
Author

Drawaes commented Feb 26, 2017

@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

This comment has been minimized.

Copy link
Contributor

CyrusNajmabadi commented Feb 26, 2017

Seems like an appropriate place for warning waves.

@Korporal

This comment has been minimized.

Copy link

Korporal commented Mar 7, 2017

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

@benaadams

This comment has been minimized.

Copy link
Contributor

benaadams commented Mar 15, 2017

@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 jcouv added this to Wanna fix in Compiler: Julien's umbrellas Oct 11, 2017

@jcouv jcouv moved this from Backlog to Rejected in Compiler: Needs LDM attention Nov 6, 2017

@jcouv jcouv removed this from Misc in Compiler: Julien's umbrellas Nov 15, 2017

@jcouv jcouv removed this from Rejected in Compiler: Needs LDM attention Nov 15, 2017

@jcouv

This comment has been minimized.

@jcouv

This comment has been minimized.

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

@jcouv jcouv removed this from Backlog in Compiler: Warning Waves Dec 26, 2017

@jinujoseph jinujoseph added this to the Unknown milestone Jan 8, 2018

@Drawaes

This comment has been minimized.

Copy link
Author

Drawaes commented Jul 19, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.