Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1d1c77e

Browse files
author
Koundinya Veluri
committed
Fix new deadlock possibility in ReaderWriterLockSlim from a recent change
Fixed the following scenario that would lead to deadlock (rare, but frequently reproducible if code is changed): - Thread T0 signals the write waiter event or the upgradeable read waiter event to wake a waiter - There are no threads waiting on the event, but T1 is in WaitOnEvent() after exiting the spin lock and before actually waiting on the event (that is, it's recorded that there is one waiter for the event). It remains in this region for a while, in the repro case it typically gets context-switched out. - T2 acquires the RW lock in some fashion that blocks T0 from acquiring the RW lock - T0 fails to acquire the RW lock enough times for it to enter WaitOnEvent for the same event as T1 - T0 resets the event - T2 releases the RW lock and does not wake a waiter because the reset at the previous step lost a signal but _waiterStates was not updated to reflect that - T2 exits (or otherwise no longer attempts to acquire the RW lock), and there are no other threads spinning for the RW lock - T1 and other threads begin waiting on the event, but there's no longer any thread that would wake them
1 parent 6573823 commit 1d1c77e

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/mscorlib/shared/System/Threading/ReaderWriterLockSlim.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,39 @@ private bool WaitOnEvent(
967967
Debug.Assert(_spinLock.IsHeld);
968968
#endif
969969

970+
WaiterStates waiterSignaledState = WaiterStates.None;
971+
EnterSpinLockReason enterMyLockReason;
972+
switch (enterLockType)
973+
{
974+
case EnterLockType.UpgradeableRead:
975+
waiterSignaledState = WaiterStates.UpgradeableReadWaiterSignaled;
976+
goto case EnterLockType.Read;
977+
978+
case EnterLockType.Read:
979+
enterMyLockReason = EnterSpinLockReason.EnterAnyRead;
980+
break;
981+
982+
case EnterLockType.Write:
983+
waiterSignaledState = WaiterStates.WriteWaiterSignaled;
984+
enterMyLockReason = EnterSpinLockReason.EnterWrite;
985+
break;
986+
987+
default:
988+
Debug.Assert(enterLockType == EnterLockType.UpgradeToWrite);
989+
enterMyLockReason = EnterSpinLockReason.UpgradeToWrite;
990+
break;
991+
}
992+
993+
// It was not possible to acquire the RW lock because some other thread was holding some type of lock. The other
994+
// thread, when it releases its lock, will wake appropriate waiters. Along with resetting the wait event, clear the
995+
// waiter signaled bit for this type of waiter if applicable, to indicate that a waiter of this type is no longer
996+
// signaled.
997+
if (waiterSignaledState != WaiterStates.None && (_waiterStates & waiterSignaledState) != WaiterStates.None)
998+
{
999+
_waiterStates &= ~waiterSignaledState;
1000+
}
9701001
waitEvent.Reset();
1002+
9711003
numWaiters++;
9721004
HasNoWaiters = false;
9731005

@@ -986,28 +1018,6 @@ private bool WaitOnEvent(
9861018
}
9871019
finally
9881020
{
989-
WaiterStates waiterSignaledState = WaiterStates.None;
990-
EnterSpinLockReason enterMyLockReason;
991-
switch (enterLockType)
992-
{
993-
case EnterLockType.UpgradeableRead:
994-
waiterSignaledState = WaiterStates.UpgradeableReadWaiterSignaled;
995-
goto case EnterLockType.Read;
996-
997-
case EnterLockType.Read:
998-
enterMyLockReason = EnterSpinLockReason.EnterAnyRead;
999-
break;
1000-
1001-
case EnterLockType.Write:
1002-
waiterSignaledState = WaiterStates.WriteWaiterSignaled;
1003-
enterMyLockReason = EnterSpinLockReason.EnterWrite;
1004-
break;
1005-
1006-
default:
1007-
Debug.Assert(enterLockType == EnterLockType.UpgradeToWrite);
1008-
enterMyLockReason = EnterSpinLockReason.UpgradeToWrite;
1009-
break;
1010-
}
10111021
_spinLock.Enter(enterMyLockReason);
10121022

10131023
--numWaiters;

0 commit comments

Comments
 (0)