Skip to content

Commit

Permalink
text
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Oct 30, 2024
1 parent cf89e03 commit 37e07b1
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions rules/S7133/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

When you acquire a lock you should also release it in the same method.

This rule raises if you acquire a lock with one of the following methods and you do not release it.
When you acquire a lock, you should also release it in the same method.
This rule raises if you acquire a lock with one of the following methods and do not release it

* https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.acquirewriterlock[ReaderWriterLock.AcquireWriterLock]
* https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.acquirereaderlock[ReaderWriterLock.AcquireReaderLock]
Expand All @@ -19,8 +17,9 @@ This rule raises if you acquire a lock with one of the following methods and you
== Why is this an issue?
Not releasing a lock in the same method where you acquire it makes the code less clear and less easy to maintain. You are also introducing the risk of not releasing a lock at all
which can lead to deadlocks or exceptions.

Not releasing a lock in the same method where you acquire it, and releasing in another one, makes the code less clear and less easy to maintain. You are also introducing the risk of not releasing a lock at all which can lead to deadlocks or exceptions.


=== Code examples

Expand All @@ -37,7 +36,7 @@ class Example
{
try
{
rwLock.AcquireWriterLock(2000); // Noncompliant
rwLock.AcquireWriterLock(2000); // Noncompliant, lock is released in another method
sharedResource++;
}
finally
Expand Down Expand Up @@ -90,7 +89,7 @@ class Example
{
try
{
MethodThatMightThrow(); // If this method throws an exception the writer lock will never be released
MethodThatMightThrow();
rwLock.AcquireReaderLock(2000);
Console.WriteLine(sharedResource);
}
Expand Down

0 comments on commit 37e07b1

Please sign in to comment.