archimondain
Killing a process synchronously
I would like to know if the function
Process.exit(pid, :kill)
Is synchronous : once the function has returned, are we sure that no more code from the process pid will be executed ? If not how could we kill a process in a synchronous way ?
Best
Marked As Solved
lud
I am not sure that the function is synchronous, but to be sure, you could first set a monitor on the process with Process.monitor, then kill it, then wait for the :DOWN message from the monitor.
Also Liked
Qqwy
Because the Elixir documentation (as well as the Erlang documentation of :erlang.exit/2, which this function delegates to) only mention “sends an exit signal”, I am fairly certain that the function is asynchronous.
The approach mentioned by @lud works. However, this will not (always) prevent other code from running on pid before it exits. It will only prevent code running on the process calling Process.exit to wait with continuing before pid has exited.
However, another question would be: what are you trying to do? It seems like a rather odd request. Maybe your actual problem can be resolved in a wholly different manner.
Qqwy
From the documentation of Process.monitor/1:
If the process is already dead when calling
Process.monitor/1, a:DOWNmessage is delivered immediately.
So you will receive the :DOWN message in that case as well.
lud
Also if you call Process.monitor on a pid that is not alive, the down message will contain :noproc as the reason, so you will know your process was not alive.







