{noproc,{gen_server{call.... Error in simple rebar3/Erlang application with a gen_server

104 views Asked by At

This is driving me nuts. I know it's something simple. I just can't see it.

I've got two nodes running on the same machine. They have a shared cookie. I can net_adm:ping/1 both ways.

Here is the code from the gen_server I'm trying to access. I've left out the comments and the -spec lines.

-module(back_server).
-behaviour(gen_server).

-export([start/0,start/3,stop/0,setState/1,getState/0]).

-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
         terminate/2, code_change/3]).

start() ->
    gen_server:start_link({global, ?MODULE}, ?MODULE, [], []).
start(Registration_type,Name,Args) ->
    gen_server:start_link({Registration_type, Name}, ?MODULE, Args, []).
stop() -> gen_server:call(?MODULE, stop).

setState(NewState)->
        io:format("setState called~n"),
        gen_server:cast(?MODULE,{update,NewState}).

getState()->
        io:format("getState called~n"),
        gen_server:call(?MODULE,get).
init([]) ->
        {ok,started}.
handle_call(get, _From, State) ->
        {reply,State,State};
handle_call(stop, _From, _State) ->
        {stop,normal,
                stopped,
          down}.

handle_cast({update,Value}, State) ->
        io:format("changing state from ~p to ~p~n",[State,Value]),
        {noreply,Value};
handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.
terminate(_Reason, _State) ->
    ok.
code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

This code runs on the back@ node. I'm using rebar3 and a supervisor to start it up. No errors on startup.

I see the registered name, back_server, for the server when I use global:registered_names/0 from front@.

When I call erpc:call('back@<ip>',back_server,getState,[]). I get the following error.

getState called
** exception exit: {exception,{noproc,{gen_server,call,[back_server,get]}}}
     in function  erpc:call/5 (erpc.erl, line 503)

I know this will be a forehead slapper, duh experience. I just don't have anyone else to look at this with me.

Thanks.

1

There are 1 answers

0
legoscia On

This happens because a call like gen_server:call(?MODULE,get) looks for a process that's locally registered as ?MODULE, but your server process is globally registered as ?MODULE. So change the getState function to:

getState()->
        io:format("getState called~n"),
        gen_server:call({global,?MODULE},get).

(That also means that you don't need erpc - if the back_server module is loaded on the front@ node, you can just call back_server:getState() directly, and it will send the request to the globally registered process on the other node.)