[erlang-questions] What happens if two gen_server call each other at the exact same time?

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Thu Mar 24 20:44:44 CET 2016


On Thu, Mar 24, 2016 at 4:49 PM, Caiyun Deng <dcy.dengcaiyun@REDACTED>
wrote:

> What happens if two gen_server call each other at the exact same time?
>
>
It depends on what the timeout values of the gen_server calls are set to.
But with 5000ms, A will be waiting on B to process, and B will be waiting
on A to process. Both servers will crash when the timeout is hit, unless
one of A or B manages to process and return an answer before.

In short: deadlock.

One way to break these situations is to process messages asynchronously
through casts, or to "order" your gen_servers such that the call direction
does not allow for loops in callers. Your example is the simplest cycle
possible, but you can easily hit the same problem for long call-chains
ending in a loop.

-- 
J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160324/d31292e9/attachment.htm>


More information about the erlang-questions mailing list