In my C# .Net 4.0 project, I use AppDomains to load plugin assemblies, ask them to do work, and then unload the AppDomain so I don't have to keep the assemblies in the main process.
For one particular plugin, I get a CannotUnloadAppDomainException
when I call AppDomain.Unload()
. I have found that this is most likely because the plugin has started a background thread which is either stuck or taking a long time (~10 seconds) in a catch
or finally
block. Unfortunately, I have no idea where the thread is.
This only happens on a customer's machine so I can't use my debugger to help. I can, however, give them a patch to write more information to a logfile if I can find something more helpful to write out.
I have the source code for the plugin assembly and I have been trying to do some static analysis and running some tests, but it is quite large and I have had no luck finding a place where it is getting stuck in a catch
or finally
.
My questions:
Is there any way where upon getting a
CannotUnloadAppDomainException
that I can get a list of threads that are running with their callstacks in the child AppDomain?Other than
catch
andfinally
blocks, are there other code sections that can throw this exception?