I encountered a scenario where ReaderWriterLockSlim seems to get broken after a series of legal actions.
The flow is:
- Thread 1 takes writer lock1
- Thread 2 tries to take reader lock1 - blocks
- Thread 2 is interrupted, and calls lock1.ExitReadLock Thread 2 didn't get the lock. It seems that an exception should have been thrown.
- Thread 1 exits writer lock of lock1
- Any thread that tries to take lock1.EnterReadLock will block forever
After stage 3 above, a debugger shows that lock1.CurrentReadCount is corrupt - seems to have overflowed down to 0x7FFFFFF.
I wonder if anyone had encountered this, or maybe I'm missing something.
The code that reproduces it:
[TestMethod]
public void ReproTest()
{
var rwlock = new ReaderWriterLockSlim();
rwlock.EnterWriteLock();
bool taken = false;
var reader = new Thread(() =>
{
try
{
rwlock.EnterReadLock();
s_logger.Info("Enter");
}
catch (ThreadInterruptedException)
{
rwlock.ExitReadLock();
}
});
reader.Name = "Reader";
reader.Start();
Thread.Sleep(1000);
reader.Interrupt();
Thread.Sleep(1000);
rwlock.ExitWriteLock();
while (!taken)
{
taken = rwlock.TryEnterReadLock(1000);
}
Thread.Sleep(1000);
}