Search code examples
erlangerlang-shell

Programming Erlang(2nd, Armstrong) p.15-18: Why does the shell hang after a second receive?


Here is my code:

-module(afile_server).
-export([start/1, loop/1]).

start(Dir) -> 
    spawn(afile_server, loop, [Dir]).

loop(Dir) ->
    receive
        {Client, list_files} -> 
            Client ! {self(), file:list_dir(Dir)};
        {Client, {get_file, File}} -> 
            Full = filename:join(Dir, File),
            Client ! {self(), file:read_file(Full)}
    end,
    loop(Dir).

Here are the shell commands:

$ erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Eshell V6.4  (abort with ^G)

2> c(afile_server).
{ok,afile_server}

3> Server = afile_server:start(".").
<0.43.0>

4> Server ! {self(), list_files}.
{<0.32.0>,list_files}

5> receive X -> X end. 
{<0.43.0>,
 {ok,["afile_client.erl","afile_server.beam",
      "afile_server.erl","hello.beam","hello.erl","old"]}}

6> Server ! {self(), {get_file, "hello.erl"}}.
{<0.32.0>,{get_file,"hello.erl"}}

7> receive X -> X end.

<hangs>

Solution

  • Okay, I found the answer here:

    http://erlang.org/pipermail/erlang-questions/2007-April/026121.html

    Thanks Anders Nygren!

    For the first receive, X starts off unbound, and it will match anything. When the first receive examines the message from the file server, X gets bound to the message, which is the value:

     {ok,["afile_client.erl","afile_server.beam",
          "afile_server.erl","hello.beam","hello.erl","old"]}}
    

    In the second receive, X is already bound, so the receive is looking for a match to:

     {ok,["afile_client.erl","afile_server.beam",
          "afile_server.erl","hello.beam","hello.erl","old"]}}
    

    The second receive hangs because the message sent by the file server is:

    {ok,<<"-module(hello).\n-export([start/0]).\n\nstart() ->
        io:format(\"hello world~n\").\n">>}}
    

    which doesn't match X (the ok's match up, but after that there is no match).

    The solution is just to use a different variable for the second receive:

    7> receive Y -> Y end.