I have code that 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),
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), a call to CreateOrOpen raises 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 throws this exception only when performing automatic tests, I cannot reproduce it locally, inside or outside the debugger. I tried to extract only the specified code in standalone tests and could not reproduce the problem. But there is too much code to publish everything here. MemoryMappedFile is stored throughout the stream (provided that it is created), and then deleted.
, : . NUnit, CruiseControl.NET, ( ) 64- Windows 2008 Server. , .
, , , , - , . "handle is invalid" CreateOrOpen ?