Skip to content

Commit

Permalink
rule version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Oct 29, 2024
1 parent 6bc94fa commit 4b08266
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions rules/S7131/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
FIXME: add a description

// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
When using https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock[ReaderWriterLock] and https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim[ReaderWriterLockSlim] for managing read and write locks, you should not exit a read lock while holding a write lock otherwise you might have unexpected behavior.

== Why is this an issue?

FIXME: remove the unused optional headers (that are commented out)

//=== What is the potential impact?
== Why is this an issue?

== How to fix it
//== How to fix it in FRAMEWORK NAME
When you acquire a write lock using, for example, `EnterWriteLock`, you indicate that you need exclusive access to the resource, meaning no other thread should be able to read or write to the resource. If you exit a read lock while holding a write lock, you might inadvertently allow other threads to acquire a read lock, leading to race conditions and data corruption.
Additionally, it can lead to deadlocks (a situation where two or more threads are waiting indefinitely for each other to release locks) and runtime exceptions (unexpected errors that occur during the execution of a program), as the lock state becomes inconsistent.
Finally, correctly matching lock acquisition and release calls (for example, `EnterWriteLock` with `ExitWriteLock` and `EnterReadLock` with `ExitReadLock`) makes the code more transparent and easier to maintain. It ensures that the locking logic is straightforward and less prone to errors.

=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
FIXME
var lock = new ReaderWriterLock();
lock.EnterWriteLock();
try
{
// Write resources
}
finally
{
Lock.ExitReadLock(); // Noncompliant
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
FIXME
var lock = new ReaderWriterLock();
lock.EnterWriteLock();
try
{
// Write resources
}
finally
{
lock.ExitWriterLock(); // Release the writerlock first
}
----

//=== How does this work?

//=== Pitfalls

//=== Going the extra mile
== Resources

=== Documentation

//== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock[The lock statement - ensure exclusive access to a shared resource]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock[ReaderWriterLock Class]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim[ReaderWriterLockSlim]

0 comments on commit 4b08266

Please sign in to comment.