GenServer.handle_call/3 Where is the origin of the from arg?

Hello, I am currently working trough the Coding Gnome for Elixir 2. There is an excercise where I am expected to save active clients in a list and remove them when they get disconnected.

There is 1 GenServer that is created by a DynamicSupervisor. The Genserver contains a register_client function, which stores the PID of the from argument into an Agent. When the client disconnects the PID is removed from the Agent in handle_info.

So far that seems to work, but I dont really understand why. I can´t trace where the contents of the fromarg originate. Also I can´t find the PID in the erlang observer. My understanding is that the field is supposed to identify the calling client, but I am stuck here.

Can anyone shed some light?

When you call GenServer.call you’re sending a message of a specific format and the GenServer behaviour handles receiving these messages for you. One part of that format includes the caller’s pid, retrieved with self().

The pid is created around here:

And is handled around here:

An easy way to dig into the source is to click the </> on hexdocs. If you see an erlang function, e.g. :gen.call, then you’ll need to switch to the erlang source on GitHub.

2 Likes

Got it, thanks. Also additionally, this answer on PID format might help others as well.

1 Like