Skip to content

Commit

Permalink
NET-589 Create rule S7131: You should not release a write lock when a…
Browse files Browse the repository at this point in the history
… read lock has been acquired and vice versa (#4433)
  • Loading branch information
github-actions[bot] authored Nov 14, 2024
1 parent 064a3a0 commit 423514e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
23 changes: 23 additions & 0 deletions rules/S7131/csharp/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"title": "A write lock should not be released when a read lock has been acquired and vice versa",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "30min"
},
"tags": [
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7131",
"sqKey": "S7131",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "infeasible",
"code": {
"impacts": {
"RELIABILITY": "HIGH"
},
"attribute": "LOGICAL"
}
}
100 changes: 100 additions & 0 deletions rules/S7131/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@

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 release a read lock while holding a write lock and vice versa, otherwise you might have runtime exceptions.
The locks should be always correctly paired so that the shared resource is accessed safely.

This rule raises if:

* you call https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.acquirewriterlock[ReaderWriterLock.AcquireWriterLock] or https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.upgradetowriterlock[ReaderWriterLock.UpgradeToWriterLock] and then use https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.releasereaderlock[ReaderWriterLock.ReleaseReaderLock]
* you call https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.enterwritelock[ReaderWriterLockSlim.EnterWriteLock] or https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.tryenterwritelock[ReaderWriterLockSlim.TryEnterWriteLock] and then use https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.exitreadlock[ReaderWriterLockSlim.ExitReadLock]
* you call https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.acquirereaderlock[ReaderWriterLock.AcquireReaderLock] or https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.downgradefromwriterlock[ReaderWriterLock.DowngradeFromWriterLock] and then use https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlock.releasewriterlock[ReaderWriterLock.ReleaseWriterLock]
* or you call https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.enterreadlock[ReaderWriterLockSlim.EnterReadLock], https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.tryenterreadlock[ReaderWriterLockSlim.TryEnterReadLock], https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.enterupgradeablereadlock[ReaderWriterLockSlim.EnterUpgradeableReadLock] or https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.tryenterupgradeablereadlock[ReaderWriterLockSlim.TryEnterUpgradeableReadLock] and then use https://learn.microsoft.com/en-us/dotnet/api/system.threading.readerwriterlockslim.exitwritelock[ReaderWriterLockSlim.ExitWriteLock]
== Why is this an issue?

If you use the `ReaderWriterLockSlim` class, you will get a https://learn.microsoft.com/en-us/dotnet/api/system.threading.lockrecursionexception[LockRecursionException].
In the case of `ReaderWriterLock`, you'll get a runtime exception for trying to release a lock that is not owned by the calling thread.


=== Code examples

==== Noncompliant code example

[source,csharp,diff-id=1,diff-type=noncompliant]
----
public class Example
{
private static ReaderWriterLock rwLock = new();
public void Writer()
{
rwLock.AcquireWriterLock(2000);
try
{
// ...
}
finally
{
rwLock.ReleaseReaderLock(); // Noncompliant, will throw runtime exception
}
}
public void Reader()
{
rwLock.AcquireReaderLock(2000);
try
{
// ...
}
finally
{
rwLock.ReleaseWriterLock(); // Noncompliant, will throw runtime exception
}
}
}
----

==== Compliant solution

[source,csharp,diff-id=1,diff-type=compliant]
----
public class Example
{
private static ReaderWriterLock rwLock = new();
public static void Writer()
{
rwLock.AcquireWriterLock(2000);
try
{
// ...
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public static void Reader()
{
rwLock.AcquireReaderLock(2000);
try
{
// ...
}
finally
{
rwLock.ReleaseReaderLock();
}
}
}
----

== Resources

=== Documentation

* 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]

include::../rspecator.adoc[]
3 changes: 3 additions & 0 deletions rules/S7131/message.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
=== Message

You should not release this [reader/writer] lock when [reader/writer] lock was acquired
2 changes: 2 additions & 0 deletions rules/S7131/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
9 changes: 9 additions & 0 deletions rules/S7131/rspecator.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::message.adoc[]

endif::env-github,rspecator-view[]

0 comments on commit 423514e

Please sign in to comment.