garrison

garrison

Why can't I catch Process.exit/2?

Alright, fair warning: I may be missing something obvious here.

I can use try/catch to catch an exit sent with exit/1:

try
  exit(:timeout)
catch
  :exit, :timeout -> :ok
end

But if I catch an exit sent with Process.exit/2, it doesn’t work (it exits):

try
  Process.exit(self(), :timeout)
  :timer.sleep(1000)
catch
  :exit, :timeout -> :ok
end

What’s really getting me here is I can’t find any mention of this in the docs. I managed to find one random comment which mentions this fact completely off-hand, and that’s it.

Am I missing something?

Marked As Solved

cevado

cevado

elixir function Kernel.exit/1 delegates to erlang exit/1 while Process.exit/2 delegates to erlang exit/2.

if you read the erlang documentation:
exit/1 raises an exception:

Raises an exception of class exit with exit reason Reason.
erlang — erts v15.2.1

while exit/2 sends an exit signal:

Sends an exit signal with exit reason Reason to the process or port identified by Pid.
erlang — erts v15.2.1

that’s why a catch can handle an exit/1 call but not an exit/2.

You can get more details on erlang doc for processes:
https://www.erlang.org/doc/system/ref_man_processes.html

Also Liked

jswanner

jswanner

I suppose this is the documentation you were looking for:

The functions erlang:exit/1 and erlang:exit/2 are named similarly but provide very different functionalities. The erlang:exit/1 function should be used when the intent is to stop the current process while erlang:exit/2 should be used when the intent is to send an exit signal to another process. Note also that erlang:exit/1 raises an exception that can be caught while erlang:exit/2 does not cause any exception to be raised.

derek-zhou

derek-zhou

You are correct. Ask yourself this question: Why try … catch should catch asynchronous event? Do you expect this to catch also?

Process.exit(self(), :timeout)
try
  :timer.sleep(1000)
catch
  :exit, :timeout -> :ok
end

You can; you just need to do Process.flag(:trap_exit, true) and have a handle_info clause to handle the {:EXIT, pid, reason} message that you would receive.

jswanner

jswanner

I think you want to look at Process.flag/2 and the meaning of :trap_exit. It still won’t do what you want, as you’ll need to handle it by receiving the message in the process mailbox

derek-zhou

derek-zhou

Exactly. My point is, to do what you want it to do, the sleep would need to be enhanced to install a clean up callback and raise when something else happened. It is a lot of work for no apparent gain. The Elixir document on sleep is clear that you should not use it: Process — Elixir v1.18.2

Use this function with extreme care. For almost all situations where you would use sleep/1 in Elixir, there is likely a more correct, faster and precise way of achieving the same with message passing

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement