Process.exit(pid, :not_kill_reason) 不会调用 handle_info({:EXIT, pid, reason}, state)。

huangapple go评论68阅读模式
英文:

Process.exit(pid, :not_kill_reason) doesn't call handle_info({:EXIT, pid, reason},state)

问题

抱歉,你的内容已经被翻译如下:

EDIT:我完全错了。这个GenServer示例没有意义,因为真正的代码有点复杂(一个启动GenServer并运行任务的GenServer等等),原来我退出了错误的进程,弄乱了日志。抱歉。

我有一个带有退出陷阱的GenServer。如果我调用 Process.exit(pid, :kill),回调 handle_info 会被调用,但如果是其他原因,回调 不会被调用

简化的GenServer

defmodule Example do
  use GenServer

  def start(args, opts \\ []) do
    GenServer.start(__MODULE__, args, opts)
  end

  def init({task, meta}) do
    Process.flag(:trap_exit, true)
  end

  def kill do
    Process.exit(self, :not_kill_reason)
  end

  def handle_info({:EXIT, pid, reason}, state) do
    IO.puts("handle_info,原因: #{reason}")
    {:stop, :normal, nil}
  end
end

我认为我漏掉了一些东西,根据文档:
https://hexdocs.pm/elixir/1.12/Process.html#exit/2

如果pid正在捕获退出,退出信号将转换为消息 {:EXIT, from, reason} 并传递给pid的消息队列。

如果原因是原子 :kill,也就是如果调用 Process.exit(pid, :kill),则会向pid发送不可捕获的退出信号,pid将无条件退出,原因是 :killed。

所以,不知何故,我成功地使其在相反的情况下工作,:exit 触发 handle_info 但 :not_kill_reason 不触发:/

英文:

EDIT: I was totally wrong.. this GenServer Example doesn't make sense since the real code is a little bit more complex (a GenServer that starts GenServers that runs tasks etc..), It turns out i was exiting the wrong process and messed it up with the logs. Sorry.

I have a GenServer with an exit trap.. If I call Process.exit(pid, :kill) the callback handle_info is called, but if it is another reason the callback is never called

Simplified GenServer

defmodule Example do
  use GenServer

  def start(args, opts \\ []) do
    GenServer.start(__MODULE__, args, opts)
  end

  def init({task, meta}) do
    Process.flag(:trap_exit, true)
  end

  def kill do
    Process.exit(self, :not_kill_reason)
  end

  def handle_info({:EXIT, pid, reason}, state) do
    IO.puts("handle_info, reason: #{reason}")
    {:stop, :normal, nil}
  end
end

I think I'm missing something, following the docs:
https://hexdocs.pm/elixir/1.12/Process.html#exit/2

If pid is trapping exits, the exit signal is transformed into a message {:EXIT, from, reason} and delivered to the message queue of pid.

If reason is the atom :kill, that is if Process.exit(pid, :kill) is called, an untrappable exit signal is sent to pid which will unconditionally exit with reason :killed.

So.. somehow I managed to make it work in reverse, :exit triggers handle_info but :not_kill_reason doesn´t :/

答案1

得分: 2

请努力提供一个可行的示例。

当您调用 Example.kill/0 时,它会终止调用它的进程,而不是您的 GenServer。

英文:

Please make an effort to provide a working example.

When you call Example.kill/0, it is killing the calling process, not your GenServer.

huangapple
  • 本文由 发表于 2023年7月20日 21:12:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定