[erlang-questions] Fixed Was: Strange Socket Behaviour (with spawn) Was:Parents always take children with them?

andrew cooke andrew@REDACTED
Sun Apr 29 15:50:12 CEST 2007

OK, I was almost there.  controlling_process/2 (as it says in the docs)
has to be called from the current controlling process.  So the following
works as expected (blocks even when the outer process terminates).

-define(BASE_TCP, [list, inet, {packet, raw}, {active, false}]).

start() ->
    {ok, A} = inet:getaddr("", inet),
    {ok, P} = gen_tcp:connect(A, 3128, ?BASE_TCP),
    Pid = proc_lib:spawn(fun() ->
                                 io:format("blocking on ~p~n", [P]),
                                 X = gen_tcp:recv(P, 0),
                                 io:format("~p~n", [X]),
                                 io:format("done") end),
    io:format("cp ~p~n", [gen_tcp:controlling_process(P, Pid)]).

And I am going to always write "expected = somefun()" instead of just

Thanks for the help,

More information about the erlang-questions mailing list