Hi folks! If you work with LiveView for a while, you already know that the mount/3
callback is called twice: the first time for the dead mount and the second time for the live mount. However, something surprised me the other day: those are handled by two different processes!!! You can check this by running this during mount:
IO.inspect(self(), label: "CONNECTED? #{connected?(socket)}")
For some reason, I never paid enough attention to that, which is shocking considering I have been working with LiveView for many years. TBH, I don’t know why I expected otherwise, but I also don’t remember reading something explicitly mentioning it in the docs before; except for these two sentences:
It’s clear to me now that I overlooked the docs, considering that I can clearly read spawn mentioned in both sentences, but I wonder if this is still too subtle.
I learned this the hard way two times this week already, the first time by passing the LiveView pid on start_link
to a named GenServer, which made my server hold on to a dead pid
while it was running. The second time was a linked GenServer crash that was not terminating the LiveView except when trapping exists .
I want to leave this here for posterity so other people don’t lose their sanity trying to understand how this works, but I wonder if we could also make docs a tiny bit more explicit about this so others don’t fall into the same trap .