views:

695

answers:

3

At work, we have migrated from Windows XP to Windows Vista. After the migration, some of my unit tests, using nUnit, started to randomly fail with the System.UnauthorizedAccessException being thrown. The failing tests each involve writing files used for tests stored as embedded resources in the test DLL to the current directory, running the tests, then deleting them, typically in the setup/teardown or the fixture setup/teardown, in rapid succession. I do this so that my tests are agnostic to the location on each developer's drive that they are ran from and not worrying about relative file paths.

While troubleshooting this, I found that it related to the creation and deletion of the files. On deletion, each delete follows the pattern:

if( File.Exists(path) ) { File.Delete(path) }

When I surround this with a try-catch block and breakpoint on the catch (if the exception was thrown), the file would already be deleted from the disk. For the failures of file creation, usually using XmlWriter or StreamWriter, each are specified to overwrite the file if it exists.

Odd thing is, while investigating, I created this C# program that seems to recreate the exception:

class Program
{
    static void Main(string[] args)
    {
        int i = 0;
        try
        {              
            while (true)
            {
                System.IO.TextWriter writer = new System.IO.StreamWriter("file.txt");
                i++;
                System.Console.Out.WriteLine(i);
                writer.Write(i);
                writer.Close();
                System.IO.File.Delete("file.txt");
            }
        }
        catch (System.UnauthorizedAccessException ex)
        {
            System.Console.Out.WriteLine("Boom at: " + i.ToString());
        }

    }
}

On one of our machines that still has XP on it, it will keep iterating into the hundreds of thousands without excepting until I kill it. On any of our Vista machines, it will print "Boom" anywhere between 150 and 500 iterations.

Since I do not have access to a Vista machine outside of work, I can't determine whether this particular 'quirk' is because of my employer's security configuration of Vista or Vista itself.

Suffice to say, I am quite stumped.

EDIT:

I would like to thank everyone for their responses. I used the Process Monitor suggested by Christian and found that the Windows Vista SearchIndexer and TortoiseSVN's TSVNCache processes were trying to access the target file while my code was running, as suggested by Martin.

Thanks again.

A: 

Do you have any virus scanners? Try to disable them or watch the file activity with ProcMon from http://www.sysinternals.com

Christian
+1  A: 

In vista, go to Performace Monitor (controlpanrl->Administrative tools) and observe the virtual bytes(for memory leaks) for your application when it is running. Performance monitor gives you a lot of details about the process you want to investigate. I suspect that your application is not releasing resources since it is working heavily on file system.

Also play around with performance monitor to try and see other measures that might have caused your issue. It is difficult to blame one or the other service with out doing little investigation.

Syam
+1  A: 

Could it be occassionally colliding with a background service, such a file indexing? Try switching off as many of these services as you can.

Martin