Exit without close leaks socket?

Hello. Correct me if I’m wrong, but when a process with a gen_tcp connection exits, the socket is not automatically closed, e.g. no TCP RST nor FIN is sent to the peer.

What are the implications of this? The peer thinks the connection is still up? The OS keeps some socket stuff around for a while? Anything else?

When a process dies because it’s linked to another process which exits, the only way to make sure to properly close the socket is to use trap_exit and call gen_tcp:close/1 before exiting, is that right?


If the process that is the owner (controlling process) of the socket exits a close on the socket is performed. It is up to the OS to decide exactly when a RST or FIN is sent to the peer. It can take up to a minute before this happens and it depends on the linger option and if there is something in the OS buffers.


Thanks Kenneth. Yes, it’s the controlling process exiting. So it doesn’t really matter if I call gen_tcp:close/1 before exiting? The result is the same?

I suppose the default value for linger is {false, _} but the docs don’t say (neither the inet nor the gen_tcp man pages).


The default value for linger is the system (OS) default which as far as we know always is {false,_}.