IOException from ReaderWriteLockSlim?

I have a static class that several threads access and use ReaderWriterLockSlim in various ways to ensure thread safety. This works fine in most cases, but very often I see that the IOException is an invalid error caused by one specific method, as shown in the stack trace below. This bothers me a lot, because I didn’t even know that System.IO was involved in ReaderWriterLock.

Any help at all in understanding where the error might come from would be greatly appreciated.

Stack trace:

System.IO.IOException: Invalid handle.
   in System.IO .__ Error.WinIOError (Int32 errorCode, String maybeFullPath)
   in System.Threading.EventWaitHandle.Reset ()
   in System.Threading.ReaderWriterLockSlim.WaitOnEvent (EventWaitHandle waitEvent, UInt32 and numWaiters, Int32time milliseconds)
   . ReaderWriterLockSlim.TryEnterUpgradeableReadLock (Int32 millisecondsTimeout)

the code:

class Class1
{
    private static ReaderWriterLockSlim readwriteLock = new ReaderWriterLockSlim();
    private const int readwriterlocktimeoutms = 5000;

    private static void myMethod(int ID)
    {
        bool IsTaken = false;
        bool isWriteLockTaken = false;

        if (!readwriteLock.TryEnterUpgradeableReadLock(readwriterlocktimeoutms))
        {
            throw new Exception("SafeGetSuItem: Error acquiring read lock");
        }
        else { IsTaken = true; }
        try
        {                
         // do some work which may require upgrading to a write lock depending on particular conditions
        }
        finally
        {
            if (IsTaken)
            {
                try
                {
                    readwriteLock.ExitUpgradeableReadLock();
                    IsTaken = false;
                }
                catch
                {
                    throw;
                }
            }                
        }
    }

}


enter code here

bool IsWriteTaken = false;
        try
        {
            if (!readerwriterlock.TryEnterWriteLock(readerwriterlocktimeout))
            {
                // log the error
            }
            else
            {
                IsWriteTaken = true;
            }

            if (IsWriteTaken)
            {
                // do some work
            }
        }
        finally
        {
            if (IsWriteTaken)
            {
                try
                {
                    readerwriterlock.ExitWriteLock();
                }
                catch
                {
                    throw;
                }
            }
        }
+3
source share
1 answer

It's a bit strange. You may have stumbled upon a mistake in the class WaitHandle. I selected this separately through Reflector, and this is what I see.

  • A call Disposeto ReaderWriterLockSlimwill call Closeto the EventWaitHandleone specified in the stack trace.
  • Close EventWaitHandle SafeHandle.
  • Reset EventWaitHandle ResetEvent Win32 API kernel32.dll SafeHandle.
  • , SafeHandle , API Win32.

Dispose ReaderWriterLockSlim , TryEnterUpgradeableReadLock ? . , , , IOException.

, BCL, IOException -, , Microsoft - ObjectDisposedException, , , . Microsoft.

+1

All Articles