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

Analyzers for lock primitives that need thread affinity #1313

Open
joaocpaiva opened this issue May 16, 2024 · 0 comments
Open

Analyzers for lock primitives that need thread affinity #1313

joaocpaiva opened this issue May 16, 2024 · 0 comments

Comments

@joaocpaiva
Copy link

joaocpaiva commented May 16, 2024

Is your feature request related to a problem? Please describe.

I have been advocating for vs threading analyzers, which are awesome to enforce proper async semantics and prevent common anti-patterns.

Unfortunately, while fixing the many violations in a service codebase, a subtle regression was introduced inside a lock scope using ReaderWriterLockSlim which was changed from sync-over-async to proper async calling pattern. Needless to say, the code start throwing exceptions when using this lock.

Given ReaderWriterLockSlim relies on thread affinity and the code did not have enough test coverage to detect the regression, the issue went undetected and resulted in a bad outage.

Describe the solution you'd like

Keeping aside that the correct fix is to use an async lock such as AsyncReaderWriterLock I'd love to see a static analyzer rule to detect misuse of async inside lock scopes that rely on thread affinity such as ReaderWriterLockSlim. Ideally such rule would be bundled in VS Threading analyzers, so this type of regression can be avoided in the future, especially when adopting the VS threading analyzers itself, one would hope it helps reducing issues in code, not adding.

Describe alternatives you've considered

I don't see alternatives to prevent such mistakes at compile time.

Additional context

C# lock​ blocks disallow await​ expressions within them for this reason but analyzers could do the same for other thread-affinitized primitives. Some impacted classes:

  • Monitor
  • ReaderWriterLock(Slim)
  • Mutex

cc: @AArnott

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

2 participants