Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Do not hold SpinLock in fields marked as readonly #33773

Open

Description

@terrajobst

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

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. stephentoub commented on Mar 20, 2020

    @stephentoub
    Member

    Closely related / duplicative: dotnet/roslyn-analyzers#2811

  4. added this to the 5.0 milestone on Mar 20, 2020
  5. added
    code-fixer
    on Mar 21, 2020
  6. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Small
    • Fixer: Small
  7. removed
    untriaged
    on Mar 21, 2020
  8. added
    api-needs-work
    on Mar 23, 2020
  9. removed
    api-suggestion
    on Mar 23, 2020
  10. terrajobst commented on Mar 23, 2020

    @terrajobst
    ContributorAuthor

    Seems related to #33772

  11. modified the milestones: 5.0, Future on Mar 28, 2020
  12. carlossanlop commented on Nov 30, 2020

    @carlossanlop
    Contributor

    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?

  13. sharwell commented on May 20, 2021

    @sharwell
    Contributor

    @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.

  14. JC3 commented on May 23, 2021

    @JC3

    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.

  15. carlreinke commented on Mar 11, 2025

    @carlreinke
    Contributor

    I just got bit by making a readonly ManualResetValueTaskSourceCore<TResult> field.

    Can this be marked as ready-for-review?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-needs-workarea-System.Threadingcode-analyzercode-fixer

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions