System.CannotUnloadAppDomainException with xUnit

Aug 7, 2009 at 11:58 PM

When running the supplied samples with xUnit, I (almost always) get this:

System.CannotUnloadAppDomainException: Error while unloading appdomain. (Exception from HRESULT: 0x80131015)
   at System.AppDomain.Unload(AppDomain domain)
   at Xunit.ExecutorWrapper.Dispose()
   at Xunit.ConsoleClient.Program.Main(String[] args)

This happens *after* xUnit reports the test results (correctly), and I've narrowed it down to just the "VerifyInput" test function.

I've tried using the supplied xunit 1.1 as well as xunit 1.5 ctp2 and get the same exception.

It could be an xunit bug, but my instinct says it's a TestAPI problem.

Is it possible that somehow the AutomatedApplication object isn't fully cleaning up after itself?  See, for example,

  http://msdn.microsoft.com/en-us/library/t0a88z4d%28VS.80%29.aspx

Thanks for any suggestions.

-mpg

 

Aug 10, 2009 at 1:50 PM

This is an issue with TestApi itself but only seems to happen in xUnit.  I could not repro this in MSTest or NUnit.  I'm still trying to find the root cause but it only seems to happen when closing a test app within a [Fact] like the samples are doing.  When closing down tests through the normal xUnit mechanism (Dispose pattern) like the ones in the acceptance tests (see WpfOutOfProcUnitTests.cs) then this does not occur.  Will update with further findings.  If this is blocking you in any way at all try using the Dispose pattern to close down your xUnit tests.

Aug 11, 2009 at 12:58 AM

OK -- I'm using Dispose now and all seems to be well.  Thanks!

 

-mpg

 

Mar 19, 2010 at 4:57 AM

I am experiencing the same issue (with the same call stack). I know it is caused by the test app which calls Microsoft's HPC Cluster API (network/cluster related).  I've tried my best to free up all resources but still can't seem to get around this problem.  I suspect Microsoft might leak some resources under the hood. Functionally, the tests pass but the test runner process fails which makes our CI Server think that the tests failed.  I've been using xunit for over a year now and really enjoy its lightweight and intuitive feel.  I would certainly appreciate any advice/guidance you might have for working around this problem as it is unlikely that I will be able to free up leaked resources allocated by third-party components the test app uses.

Thanks and Best Regards,

Will