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

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
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
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
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
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement