A reader-writer lock is released in another mode than the one the method took it in
What it finds
ExitWriteLock on a ReaderWriterLockSlim that the method entered with EnterReadLock throws SynchronizationLockException, and leaves the read lock held; the same holds for every other pair that does not match, and for ReleaseReaderLock and ReleaseWriterLock of the older ReaderWriterLock. Reported is a release of one kind on a lock that the same method takes only in another kind, unless an if around it checks that the lock is held, as with IsReadLockHeld.
From the rule's source, mismatched_unlock.rs.
using System.Threading; class Cache{ private readonly ReaderWriterLockSlim gate = new ReaderWriterLockSlim(); private readonly ReaderWriterLockSlim other = new ReaderWriterLockSlim(); private int value; public int Read() { gate.EnterReadLock(); try { return value; } finally { gate.ExitWriteLock();tx0 reports: ExitWriteLock releases a write lock, but this method took the lock in read mode } } public void Write(int next) { gate.EnterWriteLock(); try { value = next; } finally { gate.ExitWriteLock(); } } public void Upgrade(int next) { gate.EnterUpgradeableReadLock(); gate.EnterWriteLock(); value = next; gate.ExitWriteLock(); gate.ExitUpgradeableReadLock(); other.EnterReadLock(); other.ExitReadLock(); } public void Release() { gate.ExitReadLock(); }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:mismatched-unlock" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:mismatched-unlock] reason="..."tx0 explain tx0/csharp:mismatched-unlock shows the same in the terminal. Found a false positive? Report it.