[erlang-questions] Synchronous way to handle timeout between gen_server(s)

Erik Søe Sørensen <>
Tue Oct 22 17:45:32 CEST 2013


Arun: It's not that kind of timeout.
- Returning {reply, Reply, State, Timeout} is what triggers
handle_info(timeout, ...) in case the process doesn't *get a message while
idle* within the timeout period;
- Performing a gen_server:call(...) is what triggers a *timeout exception*
in case the process doesn't *get a reply while calling* within the timeout
period.

Just to clear things up :-)


2013/10/22 Arun Muralidharan <>

> Not 100% sure, but you can catch the timeout with handle_info.
> Somethig like
> handle_info(timeout, State)->
>   # do something .
>
> -Arun
>
>
> On Tue, Oct 22, 2013 at 4:16 PM, Attila Rajmund Nohl <
> > wrote:
>
>> Hello!
>>
>> Exactly what would you like to achieve? If you don't expect serv2 to
>> freeze, you can set the timeouts in the gen_server:calls to infinity.
>>
>> 2013/10/22 Tien Le <>:
>> > Hi,
>> >
>> > Please look at my sample code below. I have 2 gen_servers (serv1 and
>> serv2).
>> > serv1 start_link serv2, and traps exit.
>> >
>> > %%% serv1:do/0 implementation: %%%
>> > do() ->
>> >     gen_server:call(serv1, do).
>> >
>> > handle_call(do, _From, State) ->
>> >     Result = serv2:do(),
>> >     {reply, Result, State}.
>> >
>> >
>> > %%% serv2:do/0 implementation: %%%
>> > do() ->
>> >     gen_server:call(serv2, do).
>> >
>> > handle_call(do, _From, State) ->
>> >     %% Something that takes time like timer:sleep/1
>> >     Result = timer:sleep(5000),
>> >     {reply, Result, State}.
>> >
>> >
>> > As you can see that serv1's gen_server:call and serv2's gen_server:call
>> have
>> > the same timeout value (5000ms by default).
>> >
>> > When I call serv1:do().
>> > serv1 always terminates first due to timed out:
>> >
>> > =CRASH REPORT==== 22-Oct-2013::10:26:38 ===
>> >   crasher:
>> >     initial call: serv1:init/1
>> >     pid: <0.43.0>
>> >     registered_name: serv1
>> >     exception exit: {timeout,{gen_server,call,[serv2,do]}}
>> >       in function  gen_server:terminate/6 (gen_server.erl, line 747)
>> > ...
>> >
>> > One workaround is to increase the timeout of serv1 and serv2
>> > gen_server:call/3, like this:
>> > gen_server:call(serv1, do, 5020) => gen_server:call(serv2, do, 5010) =>
>> > timer:sleep(5000)
>> >
>> > I would like to ask you if there's any nicer and synchronous way to
>> handle
>> > the timeout in this case?
>> >
>> >
>> >
>> > _______________________________________________
>> > erlang-questions mailing list
>> > 
>> > http://erlang.org/mailman/listinfo/erlang-questions
>> >
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131022/4efc1c9c/attachment.html>


More information about the erlang-questions mailing list