[erlang-questions] gen_tcp:recv and hairloss

Edwin Fine <>
Tue Nov 11 03:15:12 CET 2008

> A lot of advice I got was to use gen_tcp:controlling_process, but
> doesn't my success with passing a socket
> and reading in a Fsm, as well as the example above, show thats its a
> common myth that  you need to use controlling_process if you
> wish to use gen_tcp:recv in a process that didn't accept the socket in
> the first place?  I believe you only need to use
> controlling_process if you want the socket data sent as a erlang message
> to the process, and for that to happen it has to be {active, true}.

Actually, there's more to it than that. When a process creates a socket, it
becomes linked to the port corresponding to the socket. When that process
dies, so do all the sockets (ports) it has created. If in the meantime you
have passed such sockets to other processes, they will get a rude surprise
(i.e. {error, closed}) when the creating process dies, because all the
sockets it is linked to will get closed. Therefore, if you have a listener
process that is trying to spawn independent processes that won't be affected
if the listener dies, you must use gen_tcp:controlling_process/2 to transfer
the socket to the other process.

I'm sure there are other effects, too, but that's a biggie. In your case, if
you actually want the gen_fsm's socket to close when your main process
exits, then don't call controlling_process.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081110/62408c8c/attachment.html>

More information about the erlang-questions mailing list