alexandrubagu
Hi,
I want to handle the following exit example ( String.to_integer(input) is jus an example and can be replaced with raise). This happen when I call My.GenServer.some_method(My.GenServer, "test"). I will be grateful if someone has a suggestion for this.
10:26:42.321 [error] GenServer My.GenServer terminating
** (stop) exited in: Task.await(%Task{owner: #PID<0.124.0>, pid: #PID<0.128.0>, ref: #Reference<0.4181578440.4035444737.94083>}, 5000)
** (EXIT) an exception was raised:
** (ArgumentError) argument error
:erlang.binary_to_integer("test")
Here’s the code:
defmodule My.Application do
use Application
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
worker(My.GenServer, []),
supervisor(Task.Supervisor, [[name: My.Task.Supervisor, restart: :transient]]),
]
opts = [strategy: :one_for_one, name: Test.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule My.GenServer do
def start_link do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
# Process.flag(:trap_exit, true)
{:ok, []}
end
def some_method(pid, input) do
GenServer.call(pid, {:some_method, input})
end
def handle_call({:some_method, input}, _from, state) do
# because we don't want to terminate GenServer use Task.async_nolink
task = Task.Supervisor.async_nolink(My.Task.Supervisor, fn ->
# raise argument error when passing binary
String.to_integer(input)
end)
{:reply, Task.await(task), state}
end
def handle_info({:EXIT, from, reason}, state) do
IO.inspect "handle_info::exit"
IO.inspect reason
{:noreply, state}
end
def handle_info({_ref, result}, state) do
IO.inspect "handle_info::result"
IO.inspect result
{:noreply, state}
end
def handle_info({:DOWN, _ref, :process, _pid, reason}, state) do
IO.inspect "handle_info::down"
IO.inspect reason
{:noreply, state}
end
def handle_info(msg, state) do
IO.inspect "handle_info"
IO.inspect msg
{:noreply, state}
end
end
I’ve read the docs from here regarding Task.Supervisor.async_link: if you create a task using async_nolink inside an OTP behaviour like GenServer, you should match on the message coming from the task inside your GenServer.handle_info/2 callback.
My problem is that handle_info is not called, I try to debug using observer attaching a trace to My.GenServer and I see that My.GenServer is receiving a :DOWN message:
Any solution to this problem ?
Thanks
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
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
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
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
Other Trending Topics
Hologram: The Journey to Local-First Elixir in the Browser - Bart Blast | ElixirConf EU 2026
https://www.youtube.com/watch?v=qqpNovT7cys...
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
- #ai
- #elixirconf-us
- #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)
dom
You’re not getting a result because there is no result: the task failed. It never got a chance to reply. :DOWN is the right thing to handle in that case.
NobbZ
When using
Task.await/1it will handle the:DOWN-message from theTask. It will handle it by callingexit/1. So theGenServerwill be exited because of that.If you want to be more failsafe, you have to do the waiting for the result and the
:DOWN-message completely on your own.alexandrubagu
I don’t get a :DOWN message in
handle_info({:DOWN, _ref, :process, _pid, reason}, state)If I remove
Task.await(task)and replace{:reply, Task.await(task), state}with{:reply, :ok, state}I got that a :DOWN message but I need to wait for response from task.dom
Oh, I see what you mean. Instead of using await you can store the task’s ref and the caller in your GenServer state, then use GenServer.reply in your handle_info to send back the response to the caller.
NobbZ
As it is now you gain nothing from the
Taskexcept for the timeout, you could do the work in theGenServerdirectly and it wouldn’t matter (as long as no timout would happen in theTask)The more correct way were to put the returned task into your state, alongside the
fromand return a:noreturntuple inhandle_call/3.Some time later you will receive a message with either the result of the computation done in the Task, which you then can
GenServer(from, answer)to your caller.Or you might receive a
:DOWN, which you will need to find ways to tell your caller about. Most idiomatic way were to use an:error-tuple I think.alexandrubagu
Thanks a lot for your help @NobbZ and @dom , right now works.
NobbZ
This will break when your
GenServerwill be hit by multiple calls before the result of the spawned task is sent back!Each time you end up in your
handle_castyou are overwriting the receiver of the answer.Once you get an answer back, you will send it to the last one who asked for the info, letting all other processes starve. Also when subsequent answers from all the other tasks come in, you will send them into the mailbox of the same process (unless another one has asked in the meantime), spamming that processes mailbox with stuff that will probably never be read again.
Please make sure, that you map a task to a caller, and only send the answers/results back correspondingly.
alexandrubagu
Something like this ?
Thanks again, @NobbZ
NobbZ
Looks good on a first glance. I’d prefer to not call functions from inside the result tuple, but thats more of a personal and stylistic issue.
kaa.python
what’s the name of this application?
https://forum.elixirforum.com/uploads/default/original/2X/c/c695484a987ec6420117f28fa472ca5b9b9b6bf3.png