Compiling the module shown below and calling example:run().
yields the following output for me:
Client: Connection closed.
handle_recv_error(2599) -> send closed to <0.101.0> when
Reason: closed
ShowReason: closed
Server: Connection closed.
The Connection closed
lines are printed by my module. The handle_recv_error
message was introduced by commit 711d1d4 and is printed when the client closes the TCP connection. This only happens with {inet_backend, socket}
, and only if the server socket was handed over to another process.
Is this desired for some reason, or shall I submit an issue?
-module(example).
-export([run/0, run/1]).
run() ->
run(1234).
run(Port) ->
Opts = [{inet_backend, socket}, {active, false}, {reuseaddr, true}],
{ok, ListenSocket} = gen_tcp:listen(Port, Opts),
_ = spawn(fun() -> client(Port) end),
{ok, Socket} = gen_tcp:accept(ListenSocket),
Pid = spawn(fun() -> handle_connection(Socket) end),
ok = gen_tcp:controlling_process(Socket, Pid),
ok = gen_tcp:close(ListenSocket).
handle_connection(Socket) ->
ok = inet:setopts(Socket, [{active, true}]),
receive_loop(Socket).
receive_loop(Socket) ->
receive
{tcp, Socket, _Data} ->
receive_loop(Socket);
{tcp_closed, Socket} ->
ok = io:format("Server: Connection closed.~n"),
ok = gen_tcp:close(Socket)
end.
client(Port) ->
{ok, Socket} = gen_tcp:connect("localhost", Port, [{active, false}]),
ok = gen_tcp:send(Socket, "Hello, world!"),
ok = timer:sleep(100),
ok = gen_tcp:close(Socket),
ok = io:format("Client: Connection closed.~n").