erlang timer gets timeout
-
13-11-2019 - |
Question
I have an erlang module with behaviour gen_server.
Now, I have:
init(_Args) ->
erlang:send_after(?PROCESS_STATE_INTERVAL,self(),processState),
{ok, []}.
and
handle_info(processState, _State)->
{ok, NewState} = gen_server:call(self(), {updateLvls}), %works fine, tested
timer:send_after(?PROCESS_STATE_INTERVAL,self(),processState),
{noreply, NewState}.
When I start it with something like {ok, Test}=gen_server:start_link({local,challenge_manager},challenge_manager,[],[]).
after a few seconds I get ** exception error: {timeout,{gen_server,call,[<0.329.0>,{updateLvls}]}}
Am I doing something wrong??
Solution
You cannot call your own gen_server
from within itself. That will result in a dead lock (which is what you see). The server process is busy handling your first request (since you haven't returned yet) and will queue the second request (which is made from the handling of the first), thus dead lock.
To solve this, either create a library function which both handle_call
and handle_info
uses, or take a look at the reply/2
function which will let you do asynchronous replies (if you return {noreply, ...}
from your handle_call
function).