无法让 gen_server 因 spawn_linked 进程崩溃而崩溃
根据我在文档中读到的内容,gen_servers 不会捕获退出。 此外,我的理解是,如果一个进程使用spawn_link启动另一个进程,并且子进程崩溃,父进程也会崩溃。
然而,这不是我所看到的。 我有一个 gen_server 可以生成链接进程。 我在子进程中设置了一个函数,如下所示:
test(JobIsHalfDone) ->
case JobIsHalfDone of
true -> exit(test);
false -> ok
end.
当该函数发送退出信号时,我收到一条消息:
** 异常退出:测试
但其父 gen_server 仍保持正确状态。 为什么?
From what I've read in the docs, gen_servers don't trap exits. Moreover, my understanding is that if a process starts another process with spawn_link, and the child process crashes, the parent crashes too.
However, this is not what I'm seeing. I've got a gen_server that spawn_links a process. I set up a function in the child process like so:
test(JobIsHalfDone) ->
case JobIsHalfDone of
true -> exit(test);
false -> ok
end.
When this function sends the exit signal, I get a message:
** exception exit: test
Yet its parent gen_server keeps right on ticking. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我们比较一下经验,我有以下行为,表明当链接的进程死亡时它确实会死亡。
也就是说,我通过spawn_link:ing一个进程,它除了死亡之外并没有太多作用,导致服务器崩溃。
事实上,您看到“***异常退出:测试”似乎表明您从shell中使用了gen_server:start_link / 3,因此您的gen服务器链接到了shell进程。 这可能会带来额外的混乱,但没有任何解释为什么你会认为服务器没有死掉。
Lets compare experience, I have the following behaviour that says that it does die when a linked process dies.
That is, I made it crash by spawn_link:ing a process that does not much but dying, bringing the server down with it.
The fact that you see the "*** exception exit: test" seems to indicate that you used gen_server:start_link/3 from the shell, so that you gen server is linked to the shell process. That might introduce additional confusion, but nothing explaining why you would think the server didnt die.
我认为您的spawn_linked进程的上下文是发出调用的进程,而不是gen_server。 要么在 init() 回调中生成循环,要么执行 gen_server:call() 并在 handle_call 中生成循环。 然后循环将链接到正在运行 gen_server 的进程。
I think the context of your spawn_linked process is the process that made the call, not the gen_server. Either spawn the loop in the init() callback or do a gen_server:call() and spawn the loop in a handle_call. Then the loop will be linked to the process that is running the gen_server.
我错了,我想这让我更接近理解为什么它没有杀死我的真实服务器。
...并在 shell 中进行测试:
I was wrong, which I guess leaves me a step closer to understanding why it's not killing my real server.
...and testing in shell: