How to test for missing tail recursive calls in service loops?

Magnus Henoch <>
Fri Sep 25 22:26:25 CEST 2009


Jeremy Raymond <> writes:

> If a service loop is missing a tail recursive call in a receive clause then
> the first time a message is sent to the process matching the bad clause the
> call the receive succeeds. However subsequent calls will fail as the process
> is no longer waiting on a receive due to the missing tail recursive call. Is
> there a good way to test for this error besides just make multiple calls to
> the service matching the same clause? Is there some way to interrogate the
> process to see if it's currently waiting on a receive?

typer comes to mind.

This is my foo.erl.  Note that foo/0 does a proper tail call, but I
forgot to make bar/0 do the same.

-module(foo).
-compile(export_all).

foo() ->
    receive
        foo ->
            erlang:display(foo),
            foo();
        stop ->
            {ok, done}
    end.

bar() ->
    receive
        bar ->
            erlang:display(bar);
        stop ->
            {ok, done}
    end.

Let's run typer on it:

$ typer foo.erl

%% File: "foo.erl"
%% ---------------
-spec foo() -> {'ok','done'}.
-spec bar() -> 'true' | {'ok','done'}.

Here we see that foo/0 always returns {ok, done}, but bar/0 sometimes
returns true - that's our warning signal.

-- 
Magnus Henoch, 
Erlang Training and Consulting
http://www.erlang-consulting.com/


More information about the erlang-questions mailing list