Does recv lock fail when closing a socket from another thread?

On Linux, if we call recv lock from one thread and close the same socket from another thread, recv does not exit.

Why?

+2
source share
2 answers

Ensure that all file descriptors for the socket are closed. If any of them remains open at the "remote end" (assuming that it is the one you are trying to close), " peer did not execute an orderly shutdown ."

If this still does not work, call shutdown(sock, SHUT_RDWR) at the remote end, this will close the socket, regardless of the number of links.

+7
source

The why is just the way it works, by design.

In the kernel, calling recv() called fget() on the struct file corresponding to the file descriptor, and this will prevent it from being freed up to the corresponding fput() .

You just need to change your design (your design is inherently very large), since this will happen, you should not block the file descriptor protection in user space, which means that close() could happen just before recv() call - and the file descriptor was even reused for something else).


If you want to wake up another stream that blocks the file descriptor, you must block it on select() , as well as in the handset included in the set of file descriptors that can be written to the main stream.

+11
source

Source: https://habr.com/ru/post/1440169/


All Articles