Files
mono/mcs/class/System.Private.CoreLib/System.Threading/RegisteredWaitHandle.cs
T
lateralusX 68d1fceba9 Fix race related to final event signaling in RegisteredWaitHandle.
We have seen sporadic failures in thread pool tests on CI related to
test case:

System.Threading.ThreadPools.Tests.ThreadPoolTests.QueueRegisterPositiveAndFlowTest [FAIL]
      Assert.Equal() Failure
      Expected: 0
      Actual:   1
      Stack Trace:
          at System.Threading.ThreadPools.Tests.ThreadPoolTests.QueueRegisterPositiveAndFlowTest () [0x00143] in D:\j\workspace\z3\label\w64\external\corefx\src\System.Threading.ThreadPool\tests\ThreadPoolTests.cs:337
          at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)
          at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in D:\j\workspace\z3\label\w64\mcs\class\corlib\System.Reflection\RuntimeMethodInfo.cs:395

Finally managed to track down the issue to a race when using the same final
event in sequential calls to RegisterWaitForSingleObject. The event could, depending on
timing, get signaled multiple times for the same RegisterWaitHandle instance
causing a thread waiting for completion on another RegisterWaitHandle using same event
to be signaled before that callback was completed. That will cause the race seen in the
failing test above.

Fix make sure we only signal the final event one time per RegisterWaitHandle
instance.
2019-06-26 07:12:16 -05:00

92 lines
2.2 KiB
C#

using System.Runtime.InteropServices;
namespace System.Threading
{
public sealed class RegisteredWaitHandle : MarshalByRefObject
{
WaitHandle _waitObject;
WaitOrTimerCallback _callback;
object _state;
WaitHandle _finalEvent;
ManualResetEvent _cancelEvent;
TimeSpan _timeout;
int _callsInProcess;
bool _executeOnlyOnce;
bool _unregistered;
internal RegisteredWaitHandle (WaitHandle waitObject, WaitOrTimerCallback callback, object state, TimeSpan timeout, bool executeOnlyOnce)
{
_waitObject = waitObject;
_callback = callback;
_state = state;
_timeout = timeout;
_executeOnlyOnce = executeOnlyOnce;
_cancelEvent = new ManualResetEvent (false);
}
internal void Wait (object? state)
{
bool release = false;
try {
_waitObject.SafeWaitHandle.DangerousAddRef (ref release);
try {
WaitHandle[] waits = new WaitHandle[] {_waitObject, _cancelEvent};
do {
int signal = WaitHandle.WaitAny (waits, _timeout, false);
if (!_unregistered) {
lock (this) {
_callsInProcess++;
}
ThreadPool.QueueUserWorkItem (new WaitCallback (DoCallBack), (signal == WaitHandle.WaitTimeout));
}
} while (!_unregistered && !_executeOnlyOnce);
} catch {
}
lock (this) {
_unregistered = true;
if (_callsInProcess == 0 && _finalEvent != null)
throw new NotImplementedException ();
}
} catch (ObjectDisposedException) {
// Can happen if we called Unregister before we had time to execute Wait
if (release)
throw;
} finally {
if (release)
_waitObject.SafeWaitHandle.DangerousRelease ();
}
}
void DoCallBack (object? timedOut)
{
try {
if (_callback != null)
_callback (_state, (bool)timedOut!);
} finally {
lock (this) {
_callsInProcess--;
if (_unregistered && _callsInProcess == 0 && _finalEvent != null) {
EventWaitHandle.Set (_finalEvent.SafeWaitHandle);
_finalEvent = null;
}
}
}
}
public bool Unregister(WaitHandle waitObject)
{
lock (this) {
if (_unregistered)
return false;
_finalEvent = waitObject;
_unregistered = true;
_cancelEvent.Set();
return true;
}
}
}
}