Search code examples
c#.netmemory-mapped-files

MemoryMappedFile.CreateOrOpen throws The handle is invalid


I have code which creates a memory mapped file as follows:

using (Mutex mutex = new Mutex(false, CoordinatorMutexName))
{
    mutex.WaitOne();

    var security = new MemoryMappedFileSecurity();
        security.SetAccessRule(
            new AccessRule<MemoryMappedFileRights>(
                new SecurityIdentifier(WellKnownSidType.WorldSid, null), // everyone
                MemoryMappedFileRights.FullControl,
                AccessControlType.Allow));

    MemoryMappedFile coordinator = MemoryMappedFile.CreateOrOpen(
        CoordinatorMMFName,
        16 * 1024 * 1024 + 4,
        MemoryMappedFileAccess.ReadWrite,
        MemoryMappedFileOptions.DelayAllocatePages,
        security,
        HandleInheritability.None);

...
...

}

Under certain circumstances (described below), the CreateOrOpen call throws the following exception:

 System.IO.IOException: The handle is invalid.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpenCore(SafeFileHandle fileHandle, String mapName, HandleInheritability inheritability, MemoryMappedFileSecurity memoryMappedFileSecurity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, Int64 capacity)
   at System.IO.MemoryMappedFiles.MemoryMappedFile.CreateOrOpen(String mapName, Int64 capacity, MemoryMappedFileAccess access, MemoryMappedFileOptions options, MemoryMappedFileSecurity memoryMappedFileSecurity, HandleInheritability inheritability)

It only throws this exception while running automated tests, I can't reproduce this locally, inside or outside a debugger. I tried extracting just the above code into stand-alone tests, and could not reproduce the issue. But there's too much code to post everything here. The MemoryMappedFile is persisted for the duration of the thread (assuming it gets created), and then disposed.

Here's the conditions where the tests fail: The tests are designed to exercise this code from multiple threads. They're in NUnit, running under CruiseControl.NET, whose service is running under a domain account (not a local machine account), on 64 bit Windows 2008 Server. I can run those same tests manually while logged into the same machine, and they all pass.

I know that's probably not enough information for someone to solve directly, but I'm at a loss of even how to investigate this problem. What kinds of things could cause a "handle is invalid" message while trying to CreateOrOpen a memory mapped file?


Solution

  • It is documented error code for CreateFileMapping(), the underlying winapi function that creates a memory mapped file:

    If lpName matches the name of an existing event, semaphore, mutex, waitable timer, or job object, the function fails, and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

    So pick a good random name to avoid this error. You can get one from Visual Studio: Tools + Create GUID, option 4. It is Globally Unique.