Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNIX domain socket not closed after close()

I have a client application communicating with a QEMU process through a QMP Unix domain socket. Sometimes after the client calls close() on the socket connection, 'netstat -ap unix' still shows it in CONNECTED state. I do check the return value of the close() call and it returns successfully with a value of 0, but the connection still seems to be lingering.

Since QMP doesn't really support multiple connections on its socket, all the subsequent calls to connect to the socket fail since they wait indefinitely for the lingering connection to be closed.

Is there a way to make sure from the code that the socket is really closed, and is there a way to force the socket to close?

like image 343
mgamal Avatar asked Feb 18 '15 18:02

mgamal


People also ask

How do I close a UNIX socket?

When a socket is no longer of use, the process can discard it by calling close(S): close(s); If data is associated with a socket which promises reliable delivery (a stream socket), the system continues to attempt to transfer the data.

How do I delete a socket file?

You can remove the socket file with a simple: unlink(path); Wherever you program exit your program, check if it exists, and remove it.

Are UNIX domain sockets reliable?

Valid socket types in the UNIX domain are: SOCK_STREAM, for a stream-oriented socket; SOCK_DGRAM, for a datagram-oriented socket that preserves message boundaries (as on most UNIX implementations, UNIX domain datagram sockets are always reliable and don't reorder datagrams); and (since Linux 2.6.

How UNIX domain socket works?

Unix sockets are bidirectional. This means that every side can perform both read and write operations. While, FIFOs are unidirectional: it has a writer peer and a reader peer. Unix sockets create less overhead and communication is faster, than by localhost IP sockets.


1 Answers

It could be that the file descriptor has been duped, forked, or leaked.

Call shutdown(sock, SHUT_RDWR) on it to close the connection for sure before closeing.

like image 87
Maxim Egorushkin Avatar answered Sep 21 '22 18:09

Maxim Egorushkin