jberling

jberling

Why are there no warnings when a process send a message to itself?

Note: this question is based on a misunderstanding. It seems like one cannot delete a topic in the forum, so it will remain forever as a testament to my lack of reasoning ability.

I got bitten hard yesterday by a mistake I made in a live component. The code looked something like this:

defmodule ScribeWeb.PageContentGenerator do
  use MyWeb, :live_component

  . . .

  def handle_event("an_event", _, socket) do
    pid = self()

    page_id = generate_page_content!(
      handler: %{
		on_llm_new_delta: fn _model, delta ->
          if delta.content do
			send(pid, {:delta, delta.content})
		  end
		end
      }
    )

    {:noreply, assign(socket, page_id: page_id)}
  end

  . . .
end

I’m not a very experienced Elixir developer, and I didn’t understand why it didn’t work. It took a long time to realize the view had frozen when I called send. Because that is what happens, right? The view froze until I added the call to generate_page_content! in a Task.async.

I can’t be the first one to make this mistake. It’s easy to do, and when you do, things behave strangely, which makes it hard to debug.

It got me thinking. I’m curious to hear: why am I allowed to send a message from a process to itself? And if there are good reasons for sending messages to oneself sometimes, couldn’t this be done via a send_to_myself function?

I’m sure the current design is the result of a lot of thinking and experimenting. I’m curious to hear why there are no guard rails, hindering or at least warning me, when I lock a process.

Marked As Solved

LostKobrakai

LostKobrakai

That’s correct understanding. The only way to make this work is by not blocking till generate_page_content! returns to make the handle_event function return. Moving generate_page_content! to a separate process would do so, but you’d not be able to work with it’s return anymore within the handle_event.

Also Liked

LostKobrakai

LostKobrakai

That shouldn’t be the root cause here. Send doesn’t block, no matter if you send a message to yourself or another process – alive or not.

There are also a lot of valid usecase to send a message to oneself.

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jaysoifer
Is there a way to rollback a specific migration and only that one ("skipping" all the other ones)? Would mix ecto.rollback -v 2008090...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
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

Other popular topics Top

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
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement