Race conditions with GenServer.call and exit from handle_call?


I am using GenServer.call to send some information to a GenServer process. Under some conditions, the GenServer will reply to the caller (GenServer.reply) and then stop normally with a {:stop, ...} return.

I can see in gen:do_call/4 that the calling process is waiting for a reply and a 'DOWN' message. Does monitors respect the rule that messages sent from a process to another are always delivered in order, or is it possible that the calling process receives the down message before the reply ?

I want to know if I can safely avoid to use a wrapping try/catch for the normal case when the server behaves as expected (i.e. send a reply then stop).

The message and signal ordering is guaranteed by the underlying system and BEAM. So your explicit reply message will arrive before the 'DOWN' message coming from the process terminating.


Thanks @rvirding !