[英]Gracefully handling corrupted state exceptions
与此问题相关,我想强制 CLR 让我的 .NET 4.5.2 应用程序捕获损坏状态异常,其唯一目的是记录它们然后终止应用程序。 如果我在应用程序周围的几个地方有catch (Exception ex)
,那么正确的方法是什么?
因此,在我指定<legacyCorruptedStateExceptionsPolicy>
属性后,如果我理解正确,所有catch (Exception ex)
处理程序都会捕获像AccessViolationException
这样的异常并愉快地继续。
是的,我知道catch (Exception ex)
是一个坏主意™,但是如果 CLR 至少将正确的堆栈跟踪放入事件日志中,我将非常乐意向客户解释他的服务器应用程序在凌晨 1 点快速失败晚上不上网是件好事。 但不幸的是,CLR 将一个不相关的异常记录到事件日志中,然后关闭该进程,因此我无法找出实际发生的情况。
问题是,如何在整个过程中实现这一点:
if the exception thrown is a Corrupted State Exception:
- write the message to the log file
- end the process
(更新)
换句话说,这可能适用于简单应用程序中的大多数异常:
[HandleProcessCorruptedStateExceptions]
[SecurityCritical]
static void Main() // main entry point
{
try
{
}
catch (Exception ex)
{
// this will catch CSEs
}
}
但是,它不适用于:
Main
入口点) 所以看起来<legacyCorruptedStateExceptionsPolicy>
是使这项工作的唯一方法,在这种情况下,我不知道在记录 CSE 后如何失败?
与其使用<legacyCorruptedStateExceptionsPolicy>
不如使用[HandleProcessCorruptedStateExceptions]
(和[SecurityCritical]
),如下所述:
https://msdn.microsoft.com/en-us/magazine/dd419661.aspx
在此之后,你的Main
方法应该是这个样子:
[HandleProcessCorruptedStateExceptions, SecurityCritical]
static void Main(string[] args)
{
try
{
...
}
catch (Exception ex)
{
// Log the CSE.
}
}
但请注意,这不会捕获更严重的异常,如StackOverflowException
和ExecutionEngineException
。
也finally
参与的try
块将不会被执行:
对于其他未处理的 appdomain 异常,您可以使用:
AppDomain.CurrentDomain.UnhandledException
Application.Current.DispatcherUnhandledException
TaskScheduler.UnobservedTaskException
(当特定处理程序适合您的情况时,请搜索详细信息。例如TaskScheduler.UnobservedTaskException
有点棘手。)
如果您无权访问Main
方法,您还可以标记您的 AppDomain 异常处理程序以捕获 CSE:
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
...
[HandleProcessCorruptedStateExceptions, SecurityCritical]
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// AccessViolationExceptions will get caught here but you cannot stop
// the termination of the process if e.IsTerminating is true.
}
最后一道防线可能是像这样的非托管 UnhandledExceptionFilter:
[DllImport("kernel32"), SuppressUnmanagedCodeSecurity]
private static extern int SetUnhandledExceptionFilter(Callback cb);
// This has to be an own non generic delegate because generic delegates cannot be marshalled to unmanaged code.
private delegate uint Callback(IntPtr ptrToExceptionInfo);
然后在流程开始的某个地方:
SetUnhandledExceptionFilter(ptrToExceptionInfo =>
{
var errorCode = "0x" + Marshal.GetExceptionCode().ToString("x2");
...
return 1;
});
您可以在这里进一步了解可能的返回代码的详细信息:
https://msdn.microsoft.com/en-us/library/ms680634(VS.85).aspx
UnhandledExceptionFilter
一个“特性”是,如果附加了调试器,它就不会被调用。 (至少在我拥有 WPF 应用程序的情况下不是。)所以请注意这一点。
如果您从上面设置了所有适当的 ExceptionHandlers,您应该记录所有可以记录的异常。 对于更严重的异常(如StackOverflowException
和ExecutionEngineException
),您必须找到另一种方法,因为它们发生后整个过程将无法使用。 一种可能的方法可能是监视主进程并记录任何致命错误的另一个进程。
额外提示:
AppDomain.CurrentDomain.UnhandledException
您可以安全地将e.ExceptionObject
为Exception
而不必担心 - 至少如果您没有任何抛出Exception
以外的其他对象的 IL 代码: 为什么 UnhandledExceptionEventArgs.ExceptionObject 是一个对象而不是一个例外?Dispatcher.UnhandledException
。感谢@haindl 指出您还可以使用[HandleProcessCorruptedStateExceptions]
1属性装饰处理程序方法,因此我制作了一个小测试应用程序,以确认事情是否真的按预期工作。
1注意:大多数答案都指出我还应该包括[SecurityCritical]
属性,尽管在下面的测试中省略它并没有改变行为(单独的[HandleProcessCorruptedStateExceptions]
似乎工作得很好)。 但是,我将在下面保留这两个属性,因为我假设所有这些人都知道他们在说什么。 这是“从 StackOverflow 复制”模式的学校示例。
显然,这个想法是从app.config
删除<legacyCorruptedStateExceptionsPolicy>
设置,即只允许我们最外层(入门级)处理程序捕获异常,记录它,然后失败。 添加设置将允许您的应用程序继续运行,如果您在某个内部处理程序中捕获异常,这不是您想要的:这个想法只是为了获取准确的异常信息,然后悲惨地死去。
我使用以下方法抛出异常:
static void DoSomeAccessViolation()
{
// if you have any questions about why this throws,
// the answer is "42", of course
var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);
}
1. 从Main
捕获异常:
[SecurityCritical]
[HandleProcessCorruptedStateExceptions]
static void Main(string[] args)
{
try
{
DoSomeAccessViolation();
}
catch (Exception ex)
{
// this will catch all CSEs in the main thread
Log(ex);
}
}
2. 捕获所有异常,包括后台线程/任务:
// no need to add attributes here
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += UnhandledException;
// throw on a background thread
var t = new Task(DoSomeAccessViolation);
t.Start();
t.Wait();
}
// but it's important that this method is marked
[SecurityCritical]
[HandleProcessCorruptedStateExceptions]
private static void UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// this will catch all unhandled exceptions, including CSEs
Log(e.ExceptionObject as Exception);
}
我建议只使用后一种方法,并从所有其他地方删除[HandleProcessCorruptedStateExceptions]
以确保异常不会在错误的地方被捕获。 即,如果您在某处有一个try/catch
块并且抛出了AccessViolationException
,您希望 CLR 在结束应用程序之前跳过catch
块并传播到UnhandledException
。
派对结束了吗? 没那么快
微软: “使用应用程序域来隔离可能导致进程中断的任务。”
下面的程序将保护您的主应用程序/线程免受不可恢复的故障的影响,而不会产生与使用HandleProcessCorruptedStateExceptions
和<legacyCorruptedStateExceptionsPolicy>
相关的风险
public class BoundaryLessExecHelper : MarshalByRefObject
{
public void DoSomething(MethodParams parms, Action action)
{
if (action != null)
action();
parms.BeenThere = true; // example of return value
}
}
public struct MethodParams
{
public bool BeenThere { get; set; }
}
class Program
{
static void InvokeCse()
{
IntPtr ptr = new IntPtr(123);
System.Runtime.InteropServices.Marshal.StructureToPtr(123, ptr, true);
}
// This is a plain code that will prove that CSE is thrown and not handled
// this method is not a solution. Solution is below
private static void ExecInThisDomain()
{
try
{
var o = new BoundaryLessExecHelper();
var p = new MethodParams() { BeenThere = false };
Console.WriteLine("Before call");
o.DoSomething(p, CausesAccessViolation);
Console.WriteLine("After call. param been there? : " + p.BeenThere.ToString()); //never stops here
}
catch (Exception exc)
{
Console.WriteLine($"CSE: {exc.ToString()}");
}
Console.ReadLine();
}
// This is a solution for CSE not to break your app.
private static void ExecInAnotherDomain()
{
AppDomain dom = null;
try
{
dom = AppDomain.CreateDomain("newDomain");
var p = new MethodParams() { BeenThere = false };
var o = (BoundaryLessExecHelper)dom.CreateInstanceAndUnwrap(typeof(BoundaryLessExecHelper).Assembly.FullName, typeof(BoundaryLessExecHelper).FullName);
Console.WriteLine("Before call");
o.DoSomething(p, CausesAccessViolation);
Console.WriteLine("After call. param been there? : " + p.BeenThere.ToString()); // never gets to here
}
catch (Exception exc)
{
Console.WriteLine($"CSE: {exc.ToString()}");
}
finally
{
AppDomain.Unload(dom);
}
Console.ReadLine();
}
static void Main(string[] args)
{
ExecInAnotherDomain(); // this will not break app
ExecInThisDomain(); // this will
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.