diff --git a/rules/S7133/csharp/rule.adoc b/rules/S7133/csharp/rule.adoc index f5786cf4a06..fdcfa6cb8ec 100644 --- a/rules/S7133/csharp/rule.adoc +++ b/rules/S7133/csharp/rule.adoc @@ -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] @@ -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 @@ -37,7 +36,7 @@ class Example { try { - rwLock.AcquireWriterLock(2000); // Noncompliant + rwLock.AcquireWriterLock(2000); // Noncompliant, lock is released in another method sharedResource++; } finally @@ -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); }