gen_server 和运行时错误
我在 gen_server 的 init 部分遇到运行时错误。 - 初始化由 process_flag(trap_exit,true) 开始 - gen_server 是监督树的一部分 我尝试在终止模块中打印原因,但它似乎在其他地方退出。 - 为什么不调用终止? 应用程序因关闭而停止。 - 如何以及在哪里捕获运行时错误?
I have a run-time error in the init part of a gen_server.
- Init begin by process_flag(trap_exit,true)
- gen_server is part of a supervision tree
I try to print the reason in the terminate module but it seems to exit elsewhere.
- why terminate is not called ?
The application stops with shutdown as reason.
- How and where to catch the run-time error ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,通常会调用
terminate
回调,即因为您已陷入退出状态。唯一不是这种情况的地方是崩溃发生在 init 函数中。在这种情况下,责任就落在主管身上,而主管通常会因此而解职。然后这个错误会爬上supervisor树,直到最终终止整个应用程序。
通常,主管将记录上下文设置为
start_error
的主管报告。这是你的暗示,监督树的部分有问题,你应该处理。您应该检查这一点,因为您可能对错误发生的位置有错误的假设。从此处编辑
您的问题是您根本不了解 SASL。研究一下。这是如何使用它的示例。
从您的示例中提取代码:
首先,bahlonga 需要告诉 Erlang 我们有一个 gen_server。
我们破解了#state{}记录,以便它可以与您的代码一起使用
基本start_linkage...
您的init函数,包括spawn问题。
上面我们为了简化而进行了一些修改...
针对缺失的功能进行了修改...
其余的只是基础知识...,所以我省略了它们。
现在对于
foo_sup
来说,foo
的主管:基本开始链接...
基本 ChildSpec。让 foo 子进程启动并运行...
在启用 SASL 的情况下启动 Erlang:
让我们尝试生成主管...
然后我们得到:
上面我们看到
foo:init/1
发生了崩溃code> 由于异常blabla
。现在主管可以报告该问题!
上下文完全正如我所说的那样......
并且具有预期的原因。
The
terminate
callback is normally called in this situation, namely because you have trapped exits.The only place where this is not the case is if the crash happens in the init-function. In that case, the responsibility is on the supervisor, who usually terminates itself as a result. Then this error crawls up the supervisor tree until it ends up terminating your whole application.
Usually, the supervisor will log a supervisor report with the context set to
start_error
. This is your hint that the part of the supervision tree has problems you should handle. You should check for this, because you may have the wrong assumption on where the error occurs.EDITED FROM HERE
Your problem is that you don't know about SASL at all. Study it. Here is an example of how to use it.
Hoisted code from your example:
First, the bahlonga needed to tell Erlang we have a gen_server.
We hack the #state{} record so it can be used with your code
Basic start_linkage...
Your init function, spawn problem included.
Above we have hacked a bit for the sake of simplification...
Hacks for missing functions....
The rest is just the basics..., so I omit them.
Now for
foo_sup
the supervisor forfoo
:Basic start link...
Basic ChildSpec. Get the foo child up and running...
Boot Erlang with SASL enabled:
Let us try to spawn the supervisor...
And we get this:
Above we see that we have a crash in
foo:init/1
due to an exceptionblabla
.And now the supervisor gets to report about the problem!
The context is exactly as I said it would be...
And with the expected reason.