Tuesday, March 1, 2011

Code Coverage Analysis exception: The process cannot access the file because it is being used by another process

Sometimes when I open code coverage window from Test->Windows->Code Coverage Results, I couldn't open the code coverage data and got the following exceptions.

"Code Coverage Analysis engine threw exception(s):
 The process cannot access the file 'data.coverage' because it is being used by another process."
First of all, we need to check who is holding the file. Go to the file path and try to delete it.



So it turns out that data.coverage file is in use in VSPerfMon.exe process.
Basically I think this is a Visual Studio bug. I spent some time to dig into it and here is my understanding.

Cause
When you run tests, VS creates a child process called QTAgent32.exe (test runner)
and QTAgent32.exe, in turn, creates another child process ‘VSPerfMon.exe’ which is a code coverage process.
If there is a unhandled exception, QTAgent32.exe can crash and it terminates the test runner process but does not have a chance to terminate its child process. So VSPerfMon.exe survives and holding the coverage data file (data.coverage).
Now when you open Code Coverage window, it tries to read coverage data file while VSPerfMon process still hold the file in write mode. Hence the exception.

Resolution
Sure, Microsoft should fix it. Until then,
1. Short-term
If you kill the remaning VSPerfmon process, you can open the code coverage.
After kill the process, close existing Code Coverage window & reopen it.

C > kill -f VSPerfMon.exe
process VSPerfMon.exe (6856) - 'OleMainThreadWndName' killed

2. Long-term
Fix the test code so that it does not threw any unhandled exception that crashes test runner.
Regardless of MS's fix, you'd always better fix your test codes :-)

No comments:

Post a Comment