Erlang gen_server cast bad return value - erlang

I try to cast message to a gen_server:
gen_server:cast({global, ID}, {watchers}).
The handler is:
handle_cast({watchers}, State) ->
case State#table_state.watchers of
[] ->
{reply, no_watchers, State};
_ ->
{reply, State#table_state.watchers, State}
end;
But when I execute gen_server:cast the gen_server terminates with error:
=ERROR REPORT==== 29-Apr-2011::18:26:07 ===
** Generic server 1 terminating
** Last message in was {'$gen_cast',{watchers}}
** When Server state == {table_state,1,"1",11,[]}
** Reason for termination ==
** {bad_return_value,{reply, no_watchers, {table_state,3,"3",11,[]}}}
Why do I get bad_return_value?

You cannot reply using cast (see gen_server documentation). That is the whole point of casting an asynchronous message instead of using call.
In your case you want to return a reply, so use gen_server:call/2 instead.

Related

Unsupervised gen_server doesn't call terminate when it receives exit signal

gen_server documentation on Module:terminate callback says:
Even if the gen_server process is not part of a supervision tree, this
function is called if it receives an 'EXIT' message from its parent.
Reason is the same as in the 'EXIT' message.
Here is my handle_info and terminate function:
handle_info(UnknownMessage, State) ->
io:format("Got unknown message: ~p~n", [UnknownMessage]),
{noreply, State}.
terminate(Reason, State) ->
io:format("Terminating with reason: ~p~n", [Reason]).
I start this server using gen_server:start. I assume when I call erlang:exit(Pid, fuckoff), it should call terminate callback function. But it shows:
Got unknown message: {'EXIT',<0.33.0>,fuckoff}
Which means it is calling handle_info. But when I call gen_server:stop, everything works as mentioned in documentation. I'm calling my gen_server from shell. Would you please clarify this?
[UPDATE]
Here is source code of decode_msg function inside gen_server. If it receives any 'EXIT' message it should call terminate function:
decode_msg(Msg, Parent, Name, State, Mod, Time, Debug, Hib) ->
case Msg of
{system, From, Req} ->
sys:handle_system_msg(Req, From, Parent, ?MODULE, Debug,
[Name, State, Mod, Time], Hib);
{'EXIT', Parent, Reason} ->
terminate(Reason, Name, Msg, Mod, State, Debug);
_Msg when Debug =:= [] ->
handle_msg(Msg, Parent, Name, State, Mod);
_Msg ->
Debug1 = sys:handle_debug(Debug, fun print_event/3,
Name, {in, Msg}),
handle_msg(Msg, Parent, Name, State, Mod, Debug1)
end.
In my case it doesn't call terminate function.
[UPDATE]
When I start gen_server using gen_server:start_link(), sending an exit signal using erlang:exit(Pid, Reason) will result in calling terminate call back function which is an expected behaviour. It seems there is a difference in interpreting an exit signal whether a process is linked to its parent or not.
Short answer:
If you call the exit/2 function from inside the gen_server actor, it behaves as expected based on the documentation and the terminate/2 callback will be called.
Long answer:
When you send the exit message from the shell, the Parent value of exit tuple is set to the shell process id, on the other hand when you start the gen_server process form shell its Parent value is set to its own process id, not the shell process id, therefore when it gets the exit message it doesn't match the second clause of the receive block in decode_msg/8 function so the terminate/6 function is not called and finally the next clause is matched which is calling handle_msg/5 function.
Recommendation:
For getting the terminate/3 callback called even by sending an exit message to the gen_server process, you can trap the exit message in handle_info/2 and then return with stop tuple as follows:
init([]) ->
process_flag(trap_exit, true),
{ok, #state{}}.
handle_info({'EXIT', _From, Reason}, State) ->
io:format("Got exit message: ~p~n", []),
{stop, Reason, State}.
terminate(Reason, State) ->
io:format("Terminating with reason: ~p~n", [Reason]),
%% do cleanup ...
ok.
When you start your gen_server, its a simple process, so, erlang:exit/1 or erlang:exit/2 work as expected.
If Pid is not trapping exits, Pid itself exits with exit reason Reason.
If Pid is trapping exits, the exit signal is transformed into a message {'EXIT', From, Reason} and delivered to the message queue of Pid.
So, currently your code trap 'EXIT' signal because this one is send like any other message into the mailbox and match handle_info/2 wildcard pattern.
If you want more information about that, you can read gen_server source code and see how it works. You can also find your problem described in this code.

Error in gen_server also terminates the calling process?

My gen_server contains a method like this:
handle_call(error, From, State) ->
io:format("Inside the handle_call error~n"),
1/0.
It provides a start (not start_link) function:
start() ->
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
normal_call() ->
gen_server:call(?MODULE, {normal}).
error_call() ->
gen_server:call(?MODULE, error).
I call the start function from the shell:
c(some_module).
some_module:start().
I then call the error call that terminates the server process because of the divide-by-zero error, but it also terminates the shell (the calling process). I don't understand why? They are not linked but still the shell restarts with a new pid. Is that an expected behavior of gen_server, or am I doing something wrong?
Updated :
It is still not working,, to help i m posting complete code.
-module(some_test).
-behaviour(gen_server).
-compile(export_all).
%% api functions, can be directly used by the user
start() ->
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
normal_call() ->
gen_server:call(?MODULE, normal, infinity).
error_call() ->
gen_server:call(?MODULE, error, infinity).
%% service specific function shoule not be call directly
init(_) ->
io:format("Inside the init ( ~p )~n", [self()]),
io:format("Inside the init...~n"),
{ok, nil}.
handle_call(normal, _, _) ->
io:format("Inside the handle_call normal~n"),
{reply, ok, nil};
handle_call(error, _, nil) ->
io:format("Inside the handle_call error~n"),
1/0.
terminate(Reason, _) ->
io:format("Inside the terminate~p~n", [Reason]).
%% just to complete
handle_info(_, _) ->
{noreply, nil}.
handle_cast(_, _) ->
{noreply, nil}.
code_change(_, _, _) ->
{ok, nil}.
%% a single test function that prove that called process was removed immeditely, did not wait for 5 seconds
test_function() ->
io:format("Id is : ~p~n", [self()]),
?MODULE:start(),
?MODULE:normal_call(),
?MODULE:error_call(),
io:format("Id is : ~p~n", [self()]). %% never reached :(
and to start i used this :-
c(some_test).
some_test:test_function().
and got the output :-
20> some_test:test_function().
Id is : <0.84.0>
Inside the init ( <0.92.0> )
Inside the init...
Inside the handle_call normal
Inside the handle_call error
Inside the terminate{badarith,
[{some_test,handle_call,3,
[{file,"some_test.erl"},{line,29}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]}
=ERROR REPORT==== 3-Dec-2015::18:36:22 ===
** Generic server some_test terminating
** Last message in was error
** When Server state == nil
** Reason for termination ==
** {badarith,[{some_test,handle_call,3,[{file,"some_test.erl"},{line,29
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,6
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]}
** exception exit: {{badarith,
[{some_test,handle_call,3,
[{file,"some_test.erl"},{line,29}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]},
{gen_server,call,[some_test,error,infinity]}}
in function gen_server:call/3 (gen_server.erl, line 212)
in call from some_test:test_function/0 (some_test.erl, line 51)
21>
so as we can see that last line after the some_test:error_call() is never called ?? because calling process also terminated ??
gen_server:call/2,3 is a synchronous messaging function. That means it does not return until the gen_server sends a reply to the sender. In this case the gen_server is crashing, and so never sends a response. The result of this is that the calling function times out, and the result of that timeout is a crash.
You will notice that the callee crashes instantly, but the caller crashes 5 seconds later. That is because the default timeout is 5000 milliseconds (check the docs, linked above). Try setting it to infinity -- your calling process will just hang, blocking forever waiting for a response that is never coming.
[UPDATE: The call coming directly from the shell is crashing immediately because an exception is being raised in the middle of its execution. This is different from the timeout expiring -- both cases result in crashes, but an exception is instant.]
The way around this is to send asynchronous messages, using gen_server:cast/2. Try defining this:
handle_cast(error, State) ->
io:format("Inside the handle_cast error~n"),
1/0.
This will only cause the callee to crash; the caller will continue on the instant the message is away. Its like throwing a baseball and walking away instead of throwing a boomerang and waiting.
As you gain experience with Erlang you will tend to write code with as many casts as possible and only resort to calls when the situation really demands a synchronous message (some state value actually depends on the sequence of events). This makes a system much more loosely coupled in many ways, and makes your calling functions resistant to failures in the processes they are sending data to.
EDIT
The shell crashing is because the call it was making crashed during execution. This does not happen with an asynch message, though. I added another module and broadened yours to illustrate the point. Now there is a new module called some_tester that relays everything we send, so it crashes, not the shell. Here are the relevant bits:
-module(some_tester).
-compile(export_all).
start() ->
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
relay(Nature, Message) ->
gen_server:cast(?MODULE, {Nature, Message}).
init(State) ->
ok = z("Starting..."),
{ok, State}.
handle_call(Message, From, S) ->
ok = z("Received ~tp from ~tp", [Message, From]),
{reply, ok, S}.
handle_cast({cast, Message}, S) ->
ok = some_test:cast(Message),
{noreply, S};
handle_cast({call, Message}, S) ->
ok = z("Sending call with ~tp", [Message]),
Reply = some_test:normal_call(Message),
ok = z("Received ~tp as reply", [Reply]),
{noreply, S};
handle_cast({infinite, Message}, S) ->
ok = z("Sending infinite with ~tp", [Message]),
Reply = some_test:infinite_call(Message),
ok = z("Received ~tp as reply", [Reply]),
{noreply, S};
handle_cast(Message, S) ->
ok = z("Unexpected ~tp", [Message]),
{noreply, S}.
Here are the relevant bits of some_test:
start() ->
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
normal_call(Message) ->
gen_server:call(?MODULE, Message).
infinite_call(Message) ->
gen_server:call(?MODULE, Message, infinity).
cast(Message) ->
gen_server:cast(?MODULE, Message).
% ...
handle_call(normal, _, S) ->
io:format("Inside the handle_call normal~n"),
{reply, ok, S};
handle_call(error, _, S) ->
io:format("Inside the handle_call error~n"),
{reply, 1/0, S};
handle_call(bad_reply, _, S) ->
io:format("Inside the handle_call error~n"),
foo;
handle_call(Message, From, S) ->
io:format("Received ~tp from ~tp~n", [Message, From]),
{reply, ok, S}.
handle_cast(error, S) ->
io:format("Bad arith: ~tp~n", [1/0]),
{noreply, S};
handle_cast(Message, S) ->
io:format("Received ~tp~n", [Message]),
{noreply, S}.
Here is a run playing around with it. Note the output of the shell's own call to self() and flush():
1> c(some_test).
some_test.erl:31: Warning: this expression will fail with a 'badarith' exception
some_test.erl:32: Warning: variable 'S' is unused
some_test.erl:40: Warning: this expression will fail with a 'badarith' exception
{ok,some_test}
2> c(some_tester).
{ok,some_tester}
3> {ok, Test} = some_test:start().
Inside the init ( <0.45.0> )
Inside the init...
{ok,<0.45.0>}
4> {ok, Tester} = some_tester:start().
<0.47.0> some_tester: Starting...
{ok,<0.47.0>}
5> monitor(process, Test).
#Ref<0.0.2.178>
6> monitor(process, Tester).
#Ref<0.0.2.183>
7> self().
<0.33.0>
8> some_tester:relay(call, foo).
<0.47.0> some_tester: Sending call with foo
ok
Received foo from {<0.47.0>,#Ref<0.0.2.196>}
<0.47.0> some_tester: Received ok as reply
9> some_tester:relay(cast, bar).
Received bar
ok
10> some_tester:relay(call, error).
<0.47.0> some_tester: Sending call with error
ok
Inside the handle_call error
Inside the terminate{badarith,
[{some_test,handle_call,3,
[{file,"some_test.erl"},{line,31}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]}
Inside the terminate{{badarith,
[{some_test,handle_call,3,
[{file,"some_test.erl"},{line,31}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]},
{gen_server,call,[some_test,error]}}
11>
=ERROR REPORT==== 3-Dec-2015::22:52:17 ===
** Generic server some_test terminating
** Last message in was error
** When Server state == nil
** Reason for termination ==
** {badarith,[{some_test,handle_call,3,[{file,"some_test.erl"},{line,31}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]}
=ERROR REPORT==== 3-Dec-2015::22:52:17 ===
** Generic server some_tester terminating
** Last message in was {'$gen_cast',{call,error}}
** When Server state == []
** Reason for termination ==
** {{{badarith,[{some_test,handle_call,3,[{file,"some_test.erl"},{line,31}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]},
{gen_server,call,[some_test,error]}},
[{gen_server,call,2,[{file,"gen_server.erl"},{line,204}]},
{some_tester,handle_cast,2,[{file,"some_tester.erl"},{line,24}]},
{gen_server,try_dispatch,4,[{file,"gen_server.erl"},{line,615}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,681}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,240}]}]}
11> self().
<0.33.0>
12> flush().
Shell got {'DOWN',#Ref<0.0.2.178>,process,<0.45.0>,
{badarith,
[{some_test,handle_call,3,
[{file,"some_test.erl"},{line,31}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]}}
Shell got {'DOWN',#Ref<0.0.2.183>,process,<0.47.0>,
{{badarith,
[{some_test,handle_call,3,
[{file,"some_test.erl"},{line,31}]},
{gen_server,try_handle_call,4,
[{file,"gen_server.erl"},{line,629}]},
{gen_server,handle_msg,5,
[{file,"gen_server.erl"},{line,661}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,240}]}]},
{gen_server,call,[some_test,error]}}}
ok
Read through that carefully.

Erlang basic general server debugger output interpretation

I'm having some trouble with an Erlang module. Here is the one that I wrote:
-module(basic_gen_server).
-export([start/1, call/2, cast/2]).
start(Module) ->
register(server, spawn(basic_gen_server,gen_server_loop,[Module, Module:init()])), server.
call(Pid,Request) ->
Pid ! {call, self(), Request},
receive
Reply -> Reply
end.
cast(Pid,Request) ->
Pid ! {cast, self(), Request},
receive
_ -> ok
end.
gen_server_loop(Module, CurrentState) ->
io:fwrite("gen_server_loop~n", []),
receive
{call, CallPid, Request} ->
{reply, Reply, NewState} = Module:handle_call(Request,self(),CurrentState),
CallPid ! Reply,
gen_server_loop(Module, NewState);
{cast, CastPid, Request} ->
{noReply, NewState} = Module:handle_cast(Request, CurrentState),
CastPid ! noReply,
gen_server_loop(Module, NewState)
end.
And here is the callback module that was defined:
% Written by Caleb Helbling
% Last updated Oct 10, 2014
-module(name_server).
-export([init/0, add/3, whereis/2, handle_cast/2,
handle_call/3, handle_swap_code/1]).
%% client routines
add(ServerPid, Person, Place) ->
basic_gen_server:cast(ServerPid, {add, Person, Place}).
whereis(ServerPid, Person) ->
basic_gen_server:call(ServerPid, {whereis, Person}).
%% callback routines
init() ->
maps:new().
handle_cast({add, Person, Place}, State) ->
NewState = maps:put(Person, Place, State),
{noreply, NewState}.
handle_call({whereis, Person}, _From, State) ->
Reply = case maps:find(Person, State) of
{ok, Place} -> Place;
error -> error
end,
NewState = State,
{reply, Reply, NewState}.
handle_swap_code(State) ->
{ok, State}.
Upon trying to initialize the server with the following command:
MyServer = basic_gen_server:start(name_server).
I get the following debug output:
=ERROR REPORT==== 29-Oct-2014::12:41:42 ===
Error in process <0.70.0> with exit value: {undef,[{basic_gen_server,gen_server_loop,[name_server,#{}],[]}]}
Conceptually, I understand the notion of making serial code into a basic server system, but I believe that I have a syntax error that I haven't been able to find using either syntax highlighting or Google. Thanks in advance for the help!
Function gen_server_loop is not exported. So you can not call basic_gen_server:gen_server_loop(Module, Module:init()), which is what is happening inside spawn(basic_gen_server,gen_server_loop,[Module, Module:init()]).
If you read your error message it tells you that function you are trying to call in undefined (trougn undef atom). Function being {basic_gen_server,gen_server_loop,[name_server,#{}],[]}, or where you have {Module, Function, ListOfArgs, ...}. You always should check that
there are no types module or function name
function arity match number of arguments in call (List in error message)
function is exported
All local calls (like loop(SomeArgs), without module specified) will not compile if function is not defined. And you can do local call dynamically (FuntionName(SomeArgs) again without module name).
EDIT after comment about need of local calls.
You actually could use lambda for this. There is spawn/1 funciton, which takes lambda (or fun if you like), so you can call spawn( fun local_functino/0).. Only issue with that is fact that your fun can not take any arguments, but there is a way around it, with use of closures.
spawn(fun () ->
gen_server_loop(Module, Module:init())
end).
And gen_serve_loop stays local call.

Receive after result not returned

Given a function:
%% #doc Retrieves client's state.
-spec(state(pid()) -> atom()).
state(Pid) when is_pid(Pid) ->
case process_info(Pid) of
undefined ->
undefined;
_Else ->
Pid ! {state, self()},
receive
{state, State} ->
State
after
1000 ->
undefined
end
end.
It works as expected for dead pids and for alive clients:
> client:state(A).
undefined
> client:state(Pid).
online
But for some reason returns Pid if process Pid will not reply his status during 1 second:
> client:state(self()).
<0.172.0>
I'm expecting 'undefined' atom there.
How can I fix this code?
This happens because you are receiving the message you sent. Your function is running on the shell process and sends itself the {state, self()} message. Right after sending the message, it receives the message and the function ends with State, which is the self() pid you sent.
I hope I've not been too confusing.

'function not exported' in erlang

This is my code, when i run: gen_server_test:alloc, it gives me the error, please help me out
-module(gen_server_test).
-behaviour(gen_server).
-export([start_link/0]).
-export([alloc/0, free/1]).
-export([init/1, handle_call/3, handle_cast/2]).
start_link() ->
gen_server:start_link({local, gen_server_test}, gen_server_test, [], []).
alloc() ->
io:format("allocing"),
gen_server:call(?MODULE, {alloc}).
free(Ch) ->
gen_server:cast(gen_server_test, {free, Ch}).
init(_Args) ->
{ok, channels()}.
handle_call({alloc}, _From, LoopData) ->
io:format("handle alloc").
handle_cast({free, Ch}, LoopData) ->
io:format(Ch).
free() ->
io:format("free").
channels() ->
io:format("channels").
the error:
23> gen_server_test:alloc().
allocinghandle alloc
=ERROR REPORT==== 6-May-2011::18:05:48 ===
** Generic server gen_server_test terminating
** Last message in was {alloc}
** When Server state == ok
** Reason for termination ==
** {'function not exported',
[{gen_server_test,terminate,[{bad_return_value,ok},ok]},
{gen_server,terminate,6},
{proc_lib,init_p_do_apply,3}]}
** exception exit: undef
in function gen_server_test:terminate/2
called as gen_server_test:terminate({bad_return_value,ok},ok)
in call from gen_server:terminate/6
in call from proc_lib:init_p_do_apply/3
Here is the important part of the error report: {bad_return_value,ok}
Valid return values for handle_call/3 are:
{reply,Reply,NewState}
| {reply,Reply,NewState,Timeout}
| {reply,Reply,NewState,hibernate}
| {noreply,NewState}
| {noreply,NewState,Timeout}
| {noreply,NewState,hibernate}
| {stop,Reason,Reply,NewState}
| {stop,Reason,NewState}
Rewriting your handle_call into:
handle_call({alloc}, _From, LoopData) ->
Reply = io:format("handle alloc"),
{reply, Reply, LoopData}
would "solve" the problem now. But I recommend going through the documentation of gen_server in general cause you have some other strange stuff going on. Your state for instance is the result of ìo:format("channels"). This doesn't make any sense at all.
I guess you're learning about gen_servers and that's great and I promise the documentation will help you solve a lot of minor problems along your path towards knowledge.
your handle_call and handle_cast methods need to return a tuple of the type {reply, Response, NewState} or {noreply, NewState}. Take a look at the gen_server documentation for more information of the allowed return values..
The io:format(..) function returns only ok.. which is the reason why u r seeing this error..

Resources