ReaderWriterLockSlim gets locked forever

376 Views Asked by At

Good time, all.

My problem is the following:

numerous threads are waiting for an event to acquire read lock, one thread is waiting for an event to obtain write lock. Lock is not held by any thread at that moment.

0:173> !do 0x0000000001c679f8
Name:        System.Threading.ReaderWriterLockSlim
...
Value Name
1 fIsReentrant
0 myLock
1 numWriteWaiters
28 numReadWaiters
0 numWriteUpgradeWaiters
0 numUpgradeWaiters
0 fNoWaiters
-1 upgradeLockOwnerId
-1 writeLockOwnerId
000000000381eb38 writeEvent
00000000035a32e0 readEvent
0000000000000000 upgradeEvent
0000000000000000 waitUpgradeEvent
9 lockID
0 fUpgradeThreadHoldingRead
1073741824 owners
0 fDisposed

0:173> .formats 0n1073741824
Evaluate expression:
  Hex:     00000000`40000000

From ReaderWriterLockSlim.cs:

private const uint WAITING_WRITERS = 0x40000000;

First I made an assumption of thread aborts making this sort of corruption to lock's state. Its easy to reproduce the problem: http://chabster.blogspot.com/2013/07/a-story-of-orphaned-readerwriterlockslim.html.

I made lock usages look like

try {} finally { lock.EnterXYZ(); }
try { /* resource usage code */ } finally { lock.ExitXYZ(); }

and was sure aborts could happen only within try { /* resource usage code */ }.

Now I got another dump with the same problem and I ran out of ideas.

I must say this happens time to time on 24 core environments. Could it be RWLS bug on ht/milticore/multiprocessor systems? I see that ReaderWriterLockSlim class updates it's members without interlocked instructions, which might be a potential problem on multicore environments.

PS: I'd like to hear from ReaderWriterLockSlim author, Joe Duffy, but couldn't reach him via email.

0

There are 0 best solutions below