[erlang-questions] gen_server question

Christian <>
Tue Jan 13 14:44:48 CET 2009


> correct me if i am wrong:
>
> messages are dropped if the receiving process is dead.
>
> with gen_server, if you use synchronized call, you can specify a timeout value
>
> gen_server:call(ServerRef, Request, Timeout)
>
> "The call may fail for several reasons, including timeout and the
> called gen_server dying before or during the call."
>
> I think you get either a badarg (pid doesn't exist (as it's dead)) or
> timeout (if the process died after message had been sent)

Every time I want to remind myself on how this works i look at the
do_call/4 function in
lib/stdlib/src/gen.erl, it is the one that implement the
gen_server:call currently.

(Disclaimer, there can of course be undocumented features in the code
that one should not rely on. But it is so much easier to understand
code than english that describes code. :)

What you learn is that gen_server:call will create a monitor on the
server for the duration of the call. So it will detect immediately if
the server has crashed, or if it crashes while it is waiting on a
reply. Another fun trivia is that the monitor reference is used as a
unique id to identify the correct request/response from the
gen_server.

Timeouts are really just relevant for gen_servers that are so bogged
down that they cant get around to process your request and reply to it
in time. (Also some code there for if its a node that doesnt support
monitoring of processes). The gen_server will not notice that the
caller have timed out, so it will process the request when it gets to
it, unless it has crashed for other reasons by then.



More information about the erlang-questions mailing list