当恐慌钩子恐慌时会发生什么?

And*_*kin 4 rust

如果我传递给的函数会发生什么std::panic::set_hook

我可以想象对此做出多种反应:考虑这个 UB,像 C++ 那样中止程序中止程序,为新的恐慌再次调用恐慌处理程序,简单地中止钩子的执行...... Rust 在这里到底承诺了什么?

语境。我正在编写一个带有 Rust/WASM 后端的 Web 应用程序,我想制作一个恐慌钩子,将任何错误发送到服务器进行调试。这涉及到网络操作,该操作本身可能会失败。因此,我试图弄清楚如何在这种双重失败的情况下确保一些合理的行为。

cdh*_*wie 6

它没有记录在源代码之外。

恐慌入口点的源代码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.
Run Code Online (Sandbox Code Playgroud)

因此,您问题的答案是“为新的恐慌再次调用恐慌处理程序”或“中止程序”,具体取决于挂钩已经恐慌的次数。


这一切都假设您没有使用#![no_std]. 如果是,那么您要么完全禁用恐慌,要么使用 实现自己的恐慌处理程序#[panic_handler],在这种情况下,您可以自己决定会发生什么。