Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Oct 29, 2024
1 parent be25b87 commit e42a3d1
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions rules/S7131/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ When using https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerw

== Why is this an issue?

When you acquire a write lock using for examples `EnterWriteLock`, you are indicating 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, which can lead to race conditions and data corruption.
Additionally, it can lead to deadlocks, where two or more threads are waiting indefinitely for each other to release locks and runtime exceptions, as the lock state becomes inconsistent.
Finally, properly matching lock acquisition and release calls (for example, `EnterWriteLock` with `ExitWriteLock` and `EnterReadLock` with `ExitReadLock`) makes the code clearer and easier to maintain. It ensures that the locking logic is straightforward and less prone to errors.

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

Expand All @@ -24,7 +23,7 @@ try
}
finally
{
fLock.ExitReadLock(); // Noncompliant
Lock.ExitReadLock(); // Noncompliant
}
----

Expand All @@ -41,7 +40,7 @@ try
}
finally
{
lock.ExitWriterLock();
lock.ExitWriterLock(); // Release the writerlock first
}
----

Expand Down

0 comments on commit e42a3d1

Please sign in to comment.