<div dir="ltr"><div><div>Hi,<br><br></div>Please look at my sample code below. I have 2 gen_servers (serv1 and serv2). serv1 start_link serv2, and traps exit. <br><br></div><div>%%% serv1:do/0 implementation: %%%<br></div>
<div>do() -><br></div><div> gen_server:call(serv1, do).<br><br></div><div>handle_call(do, _From, State) -><br></div><div> Result = serv2:do(),<br></div><div> {reply, Result, State}.<br><br><br><div>%%% serv2:do/0 implementation: %%%<br>
</div><div>do() -><br></div><div> gen_server:call(serv2, do).<br><br></div><div>handle_call(do, _From, State) -><br></div><div> %% Something that takes time like timer:sleep/1<br></div><div> Result = timer:sleep(5000),<br>
</div> {reply, Result, State}.<br><br></div><div><br>As you can see that serv1's gen_server:call and serv2's gen_server:call have the same timeout value (5000ms by default).<br><br></div><div>When I call serv1:do().<br>
</div><div>serv1 always terminates first due to timed out:<br></div><div><br>=CRASH REPORT==== 22-Oct-2013::10:26:38 ===<br> crasher:<br> initial call: serv1:init/1<br> pid: <0.43.0><br> registered_name: serv1<br>
exception exit: {timeout,{gen_server,call,[serv2,do]}}<br> in function gen_server:terminate/6 (gen_server.erl, line 747)<br>...<br><br></div><div>One workaround is to increase the timeout of serv1 and serv2 gen_server:call/3, like this:<br>
</div><div>gen_server:call(serv1, do, <b>5020</b>) => gen_server:call(serv2, do, <b>5010</b>) => timer:sleep(<b>5000</b>)<br><br></div><div>I would like to ask you if there's any nicer and synchronous way to handle the timeout in this case?<br>
<br><br></div></div>