garrison
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?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
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
cevado
elixir function
Kernel.exit/1delegates to erlangexit/1whileProcess.exit/2delegates to erlangexit/2.if you read the erlang documentation:
exit/1raises an exception:while
exit/2sends an exit signal:that’s why a catch can handle an
exit/1call but not anexit/2.You can get more details on erlang doc for processes:
jswanner
I suppose this is the documentation you were looking for:
garrison
Thank you both, I found those erlang docs but I missed that one line
derek-zhou
Think of it this way. If you do not have the sleep(1000), do you still expect to catch the exit here? No, right? It is because the Process.exit() is just sending a signal; the function call itself is fine. Now, it may be reasonable to expect that if the sleep is interrupted by the exit signal, the sleep would re-raise it as an exception; however, Elixir’s sleep does not do that.
garrison
This is indeed how you would do it, but the use case I was stumbling upon was actually, strangely, the opposite:
I was trying to replicate (fake) GenServer timeouts by sending
Process.exitfrom another process. But that’s not actually how GenServer calls time out, they callexit/1(in erlang) from within the caller process in areceive/afterblock, so I couldn’t get it right.Obviously what I was doing was extremely cursed and unusual, but I was really just wondering what the difference was since I didn’t spot it in the docs
garrison
To be clear, the sleep is interrupted by the exit signal. The second example exits immediately (not after one second). It’s just impossible to catch the exit for reasons mentioned above.
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
garrison
I’m not sure I follow what you mean. The reason I put a sleep there is because I was afraid that the
Process.exit/2signal was asynchronous and would arrive after the catch block closed, and I was trying to figure out how to catch it.But it doesn’t matter because you can’t catch the signal anyway (I did not understand this).
derek-zhou
You are correct. Ask yourself this question: Why try … catch should catch asynchronous event? Do you expect this to catch also?
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.