exjutsu

exjutsu

I’m hoping you can help me to understand a strange issue that I’m experiencing. I’m going through some of the tutorials on the Pragmatic Studio for LiveView and in one of the lessons it illustrates updating the page using :timer.send_interval(1000, self(), :tick) to trigger a handle_info(:tick, socket) call.

The example in the course works just fine, but I wanted to try triggering a handle_info function after some async work had completed, so I tried triggering a Task.async within another button click event that would send pid, :tick at the end of the task…this is where things went sideways. Even if I just try to run a Task.async(fn -> IO.puts "Task running" end) the moment that the task is triggered I get this error:

** (FunctionClauseError) no function clause matching in MyAppWeb.MyAppLive.handle_info/2

Is there something that prevents Task.async from working within a :live_view?

I feel like I’m chasing the wrong problem and that there’s a better way to do what I’m trying to do, which is to have some type of user action trigger a few async and then let the interface update as each task completes.

Any help you can provide would be greatly appreciated.

Showing Posts 1 to 7

NobbZ

NobbZ

A task will always send a message to its creator when it finishes. If I recal correctly, it follows the form {task_id, return_value}, perhaps that one is causing the error? You sadly haven’t shown us the full error message, as well as you haven’t shown us some code how exactly you do it.

ityonemo

ityonemo

You have three options:

  1. the liveview doesn’t directly care about the result of the task or you will take care of sending a result manually (via call, or, in your example by sending directry). In this case use Task.start_link or better, Task.Supervised.start_link instead of Task.async. So this is probably your best option.

  2. you care about the result but will handle it in the body of the same block where you issued the task. Then you need to catch the result with Task.await. I don’t think this is what you are looking for.

  3. you care about the result but you want to catch the answer outside of the function block. Then you must catch the response message with a handle_info clause. Liveviews don’t come with one by default but it is a valid callback. Guidelines are the same as gen_server (but mind that the liveview handle_info take a socket instead of state): Task — Elixir v1.20.2. I think what is happening is that Task.async is trying to send the message back to your liveview, but you don’t have a catchall handle_info, like:

def handle_info(_, socket), do: {:noreply, socket}

so what happens is it triggers function clause not found when the Task.async shoots back its response.

exjutsu

exjutsu OP

I was about to post some code and error messages but when I went back and looked at it again, after your comment and you got me on the right track.

If I sent the result of Task.async(...) |> Task.await() the error goes away, so it sounded like the problem was really coming from what you exactly mentioned, about the Task trying to send a message back to it’s creator.

After that I had to add two handle_info functions to get the errors to stop.

One in the format that you mentioned:

def handle_info({_task_id, _return_value} = task_info, socket) do
  IO.inspect(task_info)
  {:noreply, socket}
end

# Returns: {#Reference<0.2773227633.484966403.150753>, "Finished"}

And then I also needed another in this format:

def handle_info({_, _, _, _, _} = details, socket) do
  IO.inspect(details)
  {:noreply, socket}
end

# Returns: {:DOWN, #Reference<0.2773227633.484966404.152877>, :process, #PID<0.1204.0>, :normal}
NobbZ

NobbZ

A catch all handle_info/2 should also always log at least a warning. Not explicitly handled messages can hint on leaking messages.

ityonemo

ityonemo

So that second one is because task.async I guess is taking out a monitor on the async process. TIL but I guess it makes sense!

https://github.com/elixir-lang/elixir/blob/1145dc01680aab7094f8a6dbd38b65185e14adb4/lib/elixir/lib/task.ex#L423

I personally don’t like to use Task.async in fully remote catching mode, so I recommend Task.start_link for most concurrent tasks.

egze

egze

You should demonitor the process in the first handle_info

Process.demonitor(ref, [:flush])

Supamic

Supamic

I just wanted to post this shorter ElixirConf talk and accompanying github repo this for anyone else landing her looking for more recent information on this topic because I found it very useful.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews