views:

45

answers:

1

I have a couple applications running in separate processes that each write to a central xml doc on the harddrive. I am using a named mutex to synchronize access to the file - but get the following exception often:

System.IO.IOException: The requested operation cannot be performed on a file with a user-mapped section open.

I realized that I am calling the File.Exists() method before obtaining a lock on the mutex, is it possible that this could be the cause?

More simply, can anyone confirm if that method does a Read on the file to check if it exists? Im wondering if I need to lock the mutex before checking if the file is present. I did verify that there is no AV software running, as that seemed to be common with this exception.

Rough example of current code:

private bool UpdateFile(string Text)
{
  if (!File.Exists(@"C:\FileName.xml"))
    return false;
  else
  {
    using (Mutex mutex = new Mutex(false, "ExampleMutexName"))
    {
      mutex.WaitOne(); 
      //Write to the file
      mutex.ReleaseMutex();
    }
    return true;
  }
}

EDIT: I was not using a global mutex because everything runs inside the same user account - so I believed a local mutex would be fine. However, one of the processes I start is with impersonation - which is why I SHOULD have used a global mutex. My mistake was just thinking that everything runs with in the same user account and would be fine.

+1  A: 
  1. File.Exists returns false in case of errors and does not throw. It does not read the contents of the file though it checks for read permissions.
  2. It does not make a lot of sense to check if file exists before locking the mutex because it results in race conditions. File could be removed etcetera by someone else after your check and before your mutex is locked.
  3. The mutex you are using is not global, thus there is no guarantee that another application of the same kind is not modifying the file in parallel. Even if it was global, there is a problem when some application does not honor the lock. The best way to go here is to use file locking. See Lock and Unlock.
  4. The error you see basically means that file is opened by another process, suggesting a memory-mapped region.

P.S.: It would also be nice to exclude this file from indexing service.

Vlad Lazarenko
To add to Vlad's answer, there's another post with the same error and the issue was with the file being locked by another process, see this post for additional reference: http://stackoverflow.com/questions/1302698/system-io-exception-error-the-requested-operation-cannot-be-performed-on-a-file
Miguel Sevilla
I understand the race conditions in calling `File.Exists()` before locking the mutex. I understand that atomically, another process / thread can have modified the file. With in even that second, if the file was no present - there was no need to update the file. That could be rather lax - which is why I returned false and moved on.It is my understanding that a global mutex was cross session. These applications operate with in a single user account. In which I didnt think it matter - that could be the issue.
kmfk