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.

Last Post!

jberling

jberling

Ok, thanks

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
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 records...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement