gpt4 book ai didi

rust - 当 panic hook panic 时会发生什么?

转载 作者:行者123 更新时间:2023-12-05 01:22:47 28 4
gpt4 key购买 nike

如果我传递给 std::panic::set_hook 的函数发生 panic 会怎样?

我可以想象对此做出多种 react :考虑这个 UB,中止程序 like C++ does ,为新的 panic 再次调用 panic 处理程序,简单地中止钩子(Hook)的执行……Rust 在这里到底 promise 了什么?

上下文。我正在编写一个带有 Rust/WASM 后端的网络应用程序,我想制作一个 panic hook,将任何错误发送到服务器进行调试。这涉及网络操作,它本身可能会失败。所以我想弄清楚如何确保在这种双重故障情况下出现一些合理的行为。

最佳答案

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

source code对于 std 中的 panic 入口点有这样的评论:

// 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.

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


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

关于rust - 当 panic hook panic 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73467248/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com