AppDomain Unload problem

  • Thread starter Thread starter Frank Colbert
  • Start date Start date
F

Frank Colbert

Hi,

We have a hosting app that creates AppDomains and calls
ExecuteAssembly on the appdomain instance. Under normal execution
this works fine, however under certain work-flows, a third party
component creates a foreground thread which may not terminate
gracefully. The problem is that ExecuteAssembly blocks forever in
this case. I tried explicitly unloading the app domain by setting a
timer, however I get a "CannotUnloadAppDomain" exception with the
message "Cannot unload appdomain because thread xxx cannot be unwound
out of it". Does anyone have any idea on how I can force the app
domain to unload?
 
Hello Frank,

Thanks for your post. As I understand, the problem you are facing is that
it fails to unload AppDomain with the error regarding "thread cannot be
unwound out of it". Please correct me if there is any misunderstanding. Now
I'd like to share the following information with you:

Based on my experience, I believe that the thread is currently executing in
unmanaged code so that the CLR cannot take control of that thread. In order
to unload an appdomain, every thread with stack in that appdomain must be
unwound out of the appdomain first. If the CLR cannot take control of a
thread, it cannot unwind it. I am afraid that we cannot work around this
issue.

I strongly recommend you contact the vendor of the 3rd-party component to
fix the hang problem in the foreground thread.

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Tian provided a good description of one problem the CLR can run into.
Ultimately the vendor must fix his bugs.

If you absolutely, positively must exit the appdomain and it hangs you could
take more drastic action. You could call System.Environment.Exit and exit
the entire application (not just the appdomain) and then restart the
application. The reason why this should work when unloading the appdomain
will not is due to the different manner in which the two operate.

When you unload an appdomain the CLR performs a graceful, controlled
shutdown - all threads are unwound, all finally blocks are run, finalizers
for objects rooted in the doomed appdomain are invoked, etc. Threads cannot
be suspended for finally blocks to run.

When you exit the application, either by calling Environment.Exit or when
the last foreground thread has terminated, all threads are suspended,
finalizers for all objects are run on a separate thread, but finally blocks
are not run (the threads are suspended) and then the application exits. It's
actually far more complex then that but that's the basic idea. This avoids
the issues related to hung threads, regardless of its being in managed or
unmanaged code at shutdown time.

It doesn't solve the problem but it does provide a workaround.

Dave
 
Thanks for the responses. I was able to work around this by killing
the process at the appropriate time. Not ideal, but it works.
 
Back
Top