Adding a TCP port to a supervision tree

Because ports behave like processes I am assuming it should be possible to link to them.
I am trying to add a listen socket to a supervision tree.

To test how to link to the processes I have been trying to inspect an exit message, however for some reason the port is not generating exit messages. So maybe I have miss understood that in this case ports are not quite like processes.

Sorry for all the inspect statements but it shows what I have been looking at

test "scratchpad" do
    me = self()
    t = Task.async fn () ->
      {:ok, listen_socket} = :gen_tcp.listen(8090, mode: :binary)
      Process.link(listen_socket) |> IO.inspect
      send(me, {:lsock, listen_socket})
      :timer.sleep(500)
      receive do
        exit_message -> exit_message |> IO.inspect
      end
    end
    lsock = receive do
      {:lsock, s} ->
        s
    end

    IEx.Info.Port.info(lsock) |> IO.inspect
    :timer.sleep(1000)
    :gen_tcp.close(lsock)
    |> IO.inspect
    IEx.Info.Port.info(lsock) |> IO.inspect
    assert_receive task_complete_message, 1_000
  end

In this test I would expect that when the test closes the socket that the task process receives an exit message which then allows the task to complete sending a task completed message to the test process.

1 Like

I was missing this line before Process.link(listen_socket)

:erlang.process_flag(:trap_exit, true)

1 Like