NUnit V2 Test Framework

Bug 686560 AppDomainUnloadedException not fixed

Reported by DIDissa on 2009-09-03
34
This bug affects 4 people
Affects Status Importance Assigned to Milestone
NUnit Framework
Undecided
Unassigned
NUnit V2
Medium
Charlie Poole

Bug Description

Though it was stated in NUnit 2.1 release notes that the bug "686560 - AppDomainUnloadedException in nunit-console" is fixed/closed, I still get this in NUnit 2.4.7. This time I get it when I try AppDomain.Unload() which is definitely not unloaded. No exceptions thrown by Visual Studio. We can not rely on the work-around disabling "legacyUnhandledExceptionPolicy" in configs. Any fixes for this?

Related branches

DIDissa (dinethraid) on 2009-09-03
Changed in nunit-3.0:
status: New → Invalid
DIDissa (dinethraid) wrote :

This exist in NUnit versions 2.4.8 and 2.5.2 as well.

DIDissa (dinethraid) wrote :

Though it was stated in NUnit 2.1 release notes that the bug "686560 - AppDomainUnloadedException in nunit-console" is fixed/closed, I still get this in NUnit versions 2.4.7, 2.4.8 and 2.5. This time I get it when I try AppDomain.Unload() for an AppDomain that is definitely not unloaded before. No exceptions thrown by Visual Studio (2008 / .Net 3.5). We can not rely on the work-around disabling "legacyUnhandledExceptionPolicy" in configs. Any fixes for this?

Charlie Poole (charlie.poole) wrote :

I see you have re-instated the bug, which I had previously converted to a question.

So far, there is not enough info to treat this as a bug. It's not a general problem, nor does it appear to have anything to do with
the old bug fixed in version 2.1. That bug had to do with NUnit's unloading of its own AppDomains.

Something in your code having to do with AppDomains is apparently causing this. To reproduce it, we need to know what that code is actually doing. Can you provide a short test that demonstrates the problem?

[As an aside, I'm guessing somebody recommended the "work-around disabling legacyUnhandledExceptionPolicy" to you. This is not anything I would recommend. NUnit requires this setting in it's own config file and it should not be removed.]

Charlie

Changed in nunitv2:
status: New → Incomplete
David Schmitt (david-dasz) wrote :

Hi,

the attached zip archive contains a solution which demonstrates the bug. Running the resulting AppDomainUnloadedExceptionBugDemo.exe from the command line produces the same (trace-)output as running from nunit, but doesn't throw the exception.

Please consider this now as a real bug!

Thanks for your time and work,

David Schmitt

P.S: you need to "netsh http add urlacl url=http://+:6666/Service user=%myusername%" as administrator to allow the example to run at all.

David Schmitt (david-dasz) wrote :

P.P.S: I've tested this with 2.5.3. The error occurs on any "/domain" setting when "/process=Single", but does NOT happen when using "/domain=None|Single /process=Separate".

"/domain=Multiple /process=Separate" and "/process=Multiple" hang in setup while waiting for the worker thread to start.

David Schmitt (david-dasz) wrote :

Since the example project demonstrates a execution flow that does work, which throws an obscure exception when running under current nunit 2.5, I reset the bug status to new before this bug expires. At least this way the workaround I have posted on 2010-01-18 stays available.

Changed in nunitv2:
status: Incomplete → New
Charlie Poole (charlie.poole) wrote :

Since David confirmed it, I changed status to confirmed.

David... feel free to do this yourself on any bug that you didn't originate!

Changed in nunitv2:
status: New → Confirmed
David Schmitt (david-dasz) wrote :

I finally found the cause for this problem. The closing of the WCF ServiceHost in the example races with the AppDomainUnload a few lines later. Adding a three second delay after host.Close() in Skel.Stop() Code.cs:~120 gives the ServiceHost enough time to finish and the AppDomainUnloadedException does not occur.

Strangely enough I still cannot reproduce the behaviour without running under NUnit. I do believe though that this constitutes a bug in WCF that is (only) triggered by NUnit.

KYL (zionyx) wrote :

I have this problem too. Instead of creating a new bug against NUnit 2.5.5, here is my test log.

This test requires my test dll to load a 3rd party dll file. And this only happened in nunit-console.exe. Works fine in the GUI mode.

C:\Builds>nunit-console DataAccess-UTest.dll
NUnit version 2.5.5.10112
Copyright (C) 2002-2009 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A.
Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
   OS Version: Microsoft Windows NT 5.1.2600 Service Pack 3
  CLR Version: 2.0.50727.3607 ( Net 2.0 )
ProcessModel: Default DomainUsage: Single
Execution Runtime: net-2.0
.F
Tests run: 1, Errors: 1, Failures: 0, Inconclusive: 0, Time:
45,1258664 seconds
  Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
Errors and Failures:
1) Test Error : DataAccess.Test.Test.TestImport
   System.NullReferenceException : Object reference not set to an
instance of an object.
   at DataAccess.Test.Test.TestImport() in e:\DataAccess\DataAccess-
UTest\Test.cs:line 69
Unhandled Exception:
System.AppDomainUnloadedException: Attempted to access an unloaded
AppDomain.
   at System.AppDomain.get_FriendlyName()
   at NUnit.Util.DomainManager.DomainUnloader.Unload()
   at NUnit.Util.TestDomain.Unload()
   at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options)
   at NUnit.ConsoleRunner.Runner.Main(String[] args)

David Schmitt (david-dasz) wrote :

Kyl, are you or your 3rd party DLL using WCF? If yes, then please read comment #8. If not, it would be really great if you could post a minimal example that demonstrates the problem without WCF!

KYL (zionyx) wrote :

Hi David,

I dont think the 3rd party DLL use WCF. It's a tool that communicates with a device driver, wrapped by a VC++ project.

Line 69 in my test codes generally trying to loop through a null referenced list like this:
            foreach (IDataSequenceGroupOutput group in baseGroup.OutputGroups)

But the unhandled exception ONLY occurred when i test my dll with a wrong version of the 3rd party DLL file. After i updated the 3rd party DLL file with the right version, the unhandled exception disappeared, still nunit-console.exe got stuck indefinitely until Ctrl+C is pressed.

I know this is caused by accessing a NULL referenced variable, but i hope nunit-console.exe does not get stuck due to this.
NUnit performs the tests without problems, the test failed but it wasn't stuck.

Do you still need more info?

Regards,
KYL

tags: removed: appdomainunloadedexception
Changed in nunitv2:
importance: Undecided → Medium
Kevin Chapman (kevin-chapman) wrote :

I am also receiving the same exception from NUnit 2.5.5 console runner.

Unhandled Exception:
System.AppDomainUnloadedException: Attempted to access an unloaded AppDomain.
   at System.AppDomain.get_FriendlyName()
   at NUnit.Util.DomainManager.DomainUnloader.Unload()
   at NUnit.Util.TestDomain.Unload()
   at NUnit.ConsoleRunner.ConsoleUi.Execute(ConsoleOptions options)
   at NUnit.ConsoleRunner.Runner.Main(String[] args)

We're suffering from this issue as well. Is there a way to solve this in NUnit or my unit test code rather than modifying the WCF-using code?

Charlie Poole (charlie.poole) wrote :

Note to All:

Their are (at least) two bugs being conflated here, which makes it a bit more
difficult to resolve. If you add any problems on, please indicate _which_
of the two situations apply:

1. The original bug, also referenced in comment 12, with a stack trace that
indicates an exception thrown when NUnit itself is unloading an AppDomain.

2. An exception thrown when your own code (test or under test) attempts
to unload an AppDomain.

#2 could be a bug in NUnit or in your own code, depending on the details.
Under no circumstances should you unload the actual test assembly while
NUnit is using it.

Charlie

Changed in nunitv2:
assignee: nobody → Charlie Poole (charlie.poole)
milestone: none → 2.5.9
Charlie Poole (charlie.poole) wrote :

The original bug (NUnit throwing an exception when trying to unload the test appdomain) has been fixed.
There was some code that required access to the domain being executed outside a try block.

David's sample code seems to be a timing problem that could occur under various circumstances - any situation
that gave allowed the unload to execute before the host had finally stopped. No changes made to NUnit for this.

Any future bugs around domain unloading should avoid conflating NUnit's own use of domains with those
domains created by the test or SUT.

Charlie

Changed in nunitv2:
status: Confirmed → Fix Committed
Changed in nunitv2:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Duplicates of this bug

Other bug subscribers