Network Programming in Elixir and Erlang: race condition in example page 18-19 (in handle_info(:accept, listen_socket))

There seems to be a race condition in this example, I am actually experiencing this in a running application. The problem happens when the client connects to the server and immediately disconnects. Then with some probability this disconnect (receiving TCP FIN) happens before the call to :gen_tcp.controlling_process and the :tcp_closed message is delivered to the accepting process instead of the child process. This message is not handled by this process which results in an error.

Is there any simple way to make sure than the :tcp_closed message is correctly sent to the child process?