Search code examples
erlangfsm

Why is this gen_statem call blocking?


Hello i am trying to figure out when using the gen_statem why do the calls block,since my fsm is running in a separate process.

-module(fsm).
-record(data,{
    current="None",
    intvCount=0,
    jobCount=0
}).
-export([init/1,terminate/3,callback_mode/0,code_change/4]).

-export([state/1,start/0,interview/2,reject/2,wait/1]).

-export([sitting_home/3,interviewing/3]).
-export([handle_event/3]).
-behaviour(gen_statem).

handle_event({call,From},get_state,Data)->
    io:format("why you need state>"),
    {keep_state,Data};
handle_event({call,From},Event,Data)->
    {keep,state,Data}.
%API
start()->
    gen_statem:start_link(?MODULE,[],[]).
state(PID)->
    gen_statem:call(PID,get_state).

interview(PID,Company)->
    gen_statem:call(PID,{intv,Company}).
reject(PID,Company)->
    gen_statem:call(PID,{reject,Company}).
wait(PID)->
    gen_statem:call(PID,{wait}).

%mandatory
code_change(V,State,Data,Extra)->{ok,State,Data}.
callback_mode() ->
    state_functions.
init([])->
    {ok,sitting_home,#data{current="None",jobCount=0,intvCount=0}}.
terminate(Reasom,State,Data)->
    void.

% State implementations

sitting_home({call,From},{intv,Company},Data=#data{intvCount=C})->
    io:format("called for interview"),
    {next_state,interviewing,Data#data{intvCount=C+1},{reply,From,something}};
sitting_home({call,From},Event,Data)->
    {keep_state,Data}.

interviewing({call,From},{rejected,Company},Data)->
    {next_state,sitting_home,Data,{reply,From,somethingelse}};
interviewing({call,From},wait,Data)->
    {keep_state,Data}.

Usage:

>{ok,Pid}=fsm:start().
>fsm:state(). //blocks !

>{ok,Pid}=fsm:start().
>fsm:interview(Pid).
 called for interview %and blocks

Why do both calls block? I am spawning a different process besides the shell in which the fsm runs using the gen_statem:start_link. Why in both cases it blocks?

Update I have updated my post after i was pointed out that i forgot to use reply in order to send something back to the caller.However the handle_event/3 still blocks even in this form:

handle_event({call,From},get_state,Data)->
    {keep_state,Data,[{reply,From,Data}]}.

Solution

  • Because that's what gen_statem:call does:

    Makes a synchronous call to the gen_statem ServerRef by sending a request and waiting until its reply arrives

    and your state functions don't send any replies. They should look like

    sitting_home({call,From},{intv,Company},Data=#data{intvCount=C})->
        io:format("called for interview"), 
        {next_state,interviewing,Data#data{intvCount=C+1},{reply,From,WhateverReplyYouWant}};
    

    or

    sitting_home({call,From},{intv,Company},Data=#data{intvCount=C})->
        io:format("called for interview"), 
        gen_statem:reply(From, WhateverReplyYouWant),
        {next_state,interviewing,Data#data{intvCount=C+1}};
    

    If there's no useful reply, consider

    1. using cast instead of call (and handling cast as the EventType in your state functions), or

    2. ok as the reply.