Can a socket be closed from another thread when a send / recv on the same socket is going on?

Jay picture Jay · Aug 28, 2010 · Viewed 10.9k times · Source

Can a socket be closed from another thread when a send / recv on the same socket is going on?

Suppose one thread is in blocking recv call and another thread closes the same socket, will the thread in the recv call know this and come out safely?

I would like to know if the behavior will differ between different OS / Platforms. If yes, how will it behave in Solaris?

Answer

Jorge Fuentes González picture Jorge Fuentes González · Jan 6, 2015

In linux closing a socket won't wake up recv(). Also, as @jxh says:

If a thread is blocked on recv() or send() when the socket is closed by a different thread, the blocked thread will receive an error. However, it is difficult to detect the correct remedial action after receiving the error. This is because the file descriptor number associated with the socket may have been picked up by yet a different thread, and the blocked thread has now been woken up on an error for a "valid" socket. In such a case, the woken up thread should not call close() itself.

The woken up thread will need some way to differentiate whether the error was generated by the connection (e.g. a network error) that requires it to call close(), or if the error was generated by a different thread having called close() on it, in which case it should just error out without doing anything further to the socket.

So the best way to avoid both problems is to call shutdown() instead of close(). shutdown() will make the file descriptor still available, so won't be allocated by another descriptor, also will wake up recv() with an error and the thread with the recv() call can close the socket the normal way, like a normal error happened.