erlangeunit

Error Report from a terminated process (expected to be normal)


I have a test module that tests my Table module. My Table module, when it terminates, calls this:

terminate(_, State = {Board, Status, Players}) ->
  gen_server:stop(Board),
  ...stopping other processes,
  io:format("Table Terminating.~p~n", [State]),
  ok.

This is the only part of my code that would stop a Board process.

After running my tests, I will get this after about a minute:

=ERROR REPORT==== 21-Jul-2017::22:28:40 ===
** Generic server <0.92.0> terminating
** Last message in was []
** When Server state == [[{spawn,x,none},
                          {recent,x,none},
                          {empty,null,none},
                          {empty,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {ridge,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {empty,null,none},
                          {ridge,null,none},
                          {empty,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {ridge,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {empty,null,none}],
                         [{empty,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {empty,null,none},
                          {spawn,o,none}]]
** Reason for termination ==
** {terminated,[{io,format,
                    [<0.90.0>,"Board.~p~n",
                     [[[{spawn,x,none},
                        {recent,x,none},
                        {empty,null,none},
                        {empty,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {ridge,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {empty,null,none},
                        {ridge,null,none},
                        {empty,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {ridge,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {empty,null,none}],
                       [{empty,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {empty,null,none},
                        {spawn,o,none}]]]],
                    []},
                {board,terminate,2,[{file,"board.erl"},{line,319}]},
                {gen_server,try_terminate,3,
                            [{file,"gen_server.erl"},{line,629}]},
                {gen_server,terminate,7,[{file,"gen_server.erl"},{line,795}]},
                {proc_lib,init_p_do_apply,3,
                          [{file,"proc_lib.erl"},{line,247}]}]

which looks like the Board module terminated improperly. The Board's terminate is such:

terminate(normal, State) ->
    io:format("Board.~p~n", [State]),
    ok.

I tried to reproduce this in a standalone module, b:

-module(b).
-compile(export_all).

init([]) -> {ok, {1, 2}}.

terminate(_, State) ->
  io:format("Table Terminating.~p~n", [State]),
  ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}. 

handle_cast(_, State) ->
    {noreply, State}.

handle_info(Msg, State) ->
  io:format("Unexpected message: ~p~n",[Msg]),
  {noreply, State}.

go() ->
  gen_server:start_link(?MODULE, [], []).

Unsuccessfully:

4> {ok, B} = b:go(). {ok,<0.74.0>} 5> gen_server:stop(B). Table Terminating.{1,2}

What I am wondering is, what kind of code should I look for that would cause my Board to get the ** Reason for termination == ** {terminated reason for stopping?

Full source is here: git@github.com:QuantumProductions/tunnel.git

Edit: Board includes this

handle_call(stop, _From, State) ->
    {stop, normal, shutdown_ok, State};
handle_call(_, _, Board) ->
  {reply, {error, unrecognized, Board}, Board}.

EDIT: My best guess is eunit is automatically terminated processes created within its tests?


Solution

  • Why borad last message is []? According to the code you used gen_server:stop/1 which calls gen:stop/1 which calls gen:stop/3 which calls proc_lib:stop/3 which finally calls sys:terminate/3. When you call sys:terminate/3 for a gen_server, sys calls gen_server:system_terminate/4 and it calls gen_server:terminate/6 with [] as last message !

    why borad terminated with reason {terminated, ...}? According to the io code, io:format/2 finally calls io:execute_request/2 and you got this because io process terminated and in here it becomes {terminated, ...}.