std::panic::set_hook
函数传入的函数如果发生了panic,会有什么后果?有多种处理方式,例如将其视为未定义行为、像C++一样终止程序(参考链接)、为新的panic再次调用panic处理器、或是简单地中止hook的执行…… Rust在这里承诺了什么?
背景:我正在使用Rust/WASM编写Web应用程序,并希望创建一个panic hook,将任何错误发送到服务器进行调试。这涉及到网络操作,本身也可能失败。因此,我正在尝试弄清楚如何确保在这种双重故障情况下的某些合理行为。
std::panic::set_hook
函数传入的函数如果发生了panic,会有什么后果?这并没有在源代码之外进行记录。
std
中的恐慌入口点的源代码有这样的注释:
// If this is the third nested call (e.g., panics == 2, this is 0-indexed),
// the panic hook probably triggered the last panic, otherwise the
// double-panic check would have aborted the process. In this case abort the
// process real quickly as we don't want to try calling it again as it'll
// probably just panic again.
#![no_std]
。如果您使用了该属性,则要么完全禁用了恐慌,要么使用了#[panic_handler]
自己实现了恐慌处理程序,在这种情况下,您可以自行决定会发生什么。std
提供的一种工具,而不是语言,并且它是从panic handler中调用的。RFC表示任何双重panic都会导致中止,但是std
中的panic handler具有明确处理双重panic的逻辑,这意味着要么std
在某种程度上抑制了panic运行时的自动中止部分,要么panic运行时实际上没有自动中止,或者std
不必要地防范双重panics。 - cdhowie