Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

.net - Can I prevent an uncaught exception in another AppDomain from shutting down the application?

I'm having trouble with a misbehaved library that throws an exception in a finalizer, which of course crashes the application.

To avoid this, I tried loading the library in its own AppDomain, but the exception still bubbles to the surface and crashes the application.

As documented on MSDN, registering to AppDomain.UnhandledException doesn't prevent the exception from bubbling up, but I'm quite surprised that there is no other way to catch such an exception in a "sub AppDomain".

How do plugin hosts, or applications that use AppDomains to sandbox potentially harmful code, do to stop unhandled exceptions ? Is it in fact possible ?

Note: I already have another workaround, the one described here. The bad finalizer is on a long-lived object, which seems to be only collected during shutdown, so it is sufficient to hide this "bogus" error from the user. Still, I find this workaround brittle, since it will either hide other, real errors, or risk blowing up my application if the object is collected earlier.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

An AppDomain is nice since you can ditch the program state but you still have the problem that the thread is dead. That this happens on the finalizer thread is fatal, the CLR will abort the process without recourse.

The only 'fix' is to run this component in its own process. You can shoot it in the head with Process.Kill() to prevent the finalizer thread from running at process exit. Use one of the interprocess communication mechanisms that .NET supports to talk to it. A socket, named pipe, Remoting or WCF. Good luck with it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...