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.