When a process binds a name to a TCP socket (i.e. ip:port pair) and exits, it doesn't has to explicitly cleanup anything: another process can reuse the same ip:port. Same with the abstract UNIX sockets on Linux (when sun_path
starts with zero byte). However, when using traditional UNIX sockets, one has to unlink
it after it is no longer needed (or before bind
-ing, which is not very good since you can remove something important).
According to my observations, once the last file descriptor referring to the UNIX socket is closed, the socket file is useless garbage: open
returns ENODEV
, connect
returns ECONNREFUSED
, and bind
returns EADDRINUSE
. The only thing one can (and has to) do is unlink
.
Shouldn't OS remove socket file from file system automatically when it's no longer used?