I am implementing simple tcp server with the following sequence:
{ok, LS} = gen_tcp:listen(Port,[{active, true}, {reuseaddr, true}, {mode, list}]),
{ok, Socket}
If the socket is active, inet:tcp_controlling_process
(called by gen_tcp:controlling_process
) sets the socket to passive, then selectively receives all messages related to that socket and sends them to the new owner, effectively moving them to the new owner's message queue. Then it restores the socket to active.
So there's no race condition: they have already thought of that and fixed it in the library.