question about "active" option (regarding gen_tcp)

Rick Pettit <>
Wed Nov 7 19:27:01 CET 2001



> I have a concurrent TCP server which spawns a new process every time
> gen_tcp:accept() returns.  I call gen_tcp:listen() and set active to
> true, such that tcp packets arrive as messages in my mailbox.
> 
> I had hoped to call gen_tcp:controlling_process() immediately after my
> call to accept, so that any TCP packets for that connection will be sent
> to the spawned processes mailbox.
> 
> I fear that there is a race condition between the call to accept and the
> call to controlling_process (which means the parent may receive TCP
> packets in its mailbox instead of the child).

I must apologize for replying to my own post, but I did some digging and
came across inet:tcp_sync_input() which appears to handle this race
condition (but I still seek confirmation from the "great ones").

This inet:tcp_sync_input() is called by
inet:tcp_controlling_process() which is called by
gen_tcp:controlling_process().

It appears as though the call to controlling process() will pull all tcp
packets from the previous owner's mailbox and place them in the new
owner's mailbox before returning, making the call to
controlling_process() atomic from the callers perspective.

Thanks again.

Rick




More information about the erlang-questions mailing list