You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Do not hold SpinLock in fields marked as readonly #33773
SpinLock is a mutable struct, meant only for advanced scenarios. Accidentally making a SpinLock field readonly can result in silent but significant problems, as any mutations to the instance (e.g. Enter, Exit) will be done on a compiler-generated copy and thus be ignored, making the lock an expensive nop. (It might make sense to extend this analyzer to additional mutable struct types where storing them in a readonly field is likely a bug, e.g. GCHandle.)
Category: Reliability
Reacted by Jason C, NN, Petr Onderka, Theodor Zoulias, Paulus Pärssinen, Cole Tobin, Carl Reinke, LeaFrock and littletoxic
@carlossanlop I think a review would be good. The proposed rule only indirectly addresses the underlying problem, and we have a large number of related cases in #50389.
Along those lines, storing SpinLock and other such values in Nullable, e.g.:
SpinLock? example;
That'd be covered implicitly with non-read-only constraints, but should probably have a modified error description so that a warning / failure message makes reasonable sense.
Activity
stephentoub commentedon Mar 20, 2020
Closely related / duplicative: dotnet/roslyn-analyzers#2811
jeffhandley commentedon Mar 21, 2020
Estimates:
terrajobst commentedon Mar 23, 2020
Seems related to #33772
carlossanlop commentedon Nov 30, 2020
This analyzer has a long discussion here already: dotnet/roslyn-analyzers#2811 which @mavasani already approved.
There's even an PR out trying to get it fixed: dotnet/roslyn-analyzers#2831
Do we want this issue to go through the dotnet/runtime API review process anyway?
sharwell commentedon May 20, 2021
@carlossanlop I think a review would be good. The proposed rule only indirectly addresses the underlying problem, and we have a large number of related cases in #50389.
JC3 commentedon May 23, 2021
Along those lines, storing
SpinLockand other such values inNullable, e.g.:That'd be covered implicitly with non-read-only constraints, but should probably have a modified error description so that a warning / failure message makes reasonable sense.
carlreinke commentedon Mar 11, 2025
I just got bit by making a
readonly ManualResetValueTaskSourceCore<TResult>field.Can this be marked as ready-for-review?