Re: recv() hangs until SIGCHLD ?



On Sat, 11 Oct 2008 05:20:37 -0700
"David Schwartz" <davids@xxxxxxxxxxxxx> wrote:


Nicolas Cannasse wrote:

The sockets are non-blocking.

Ouch, that's a serious bug. Non-blocking operations shouldn't block!

Checking with netstat and ss I can confirm that both Send and Recv
queues are empty, which makes the recv() behavior consistent.

However since this problem does not occur without threads, we can be
sure that the blame is still on the receiver.

In a practical case, we have a thread blocked in recv() for more than 12
hours, which is way beyond the timeout of the sender connection. The
socket has already been closed by the sender so recv() should at least
be noticed and returns 0.

Can you clarify what you mean by "the socket has already been closed by the
sender"? You mean the other end of the TCP connection shut it down? By "the
socket", you don't mean the socket you called 'recv' on, right? You mean the
socket on the other end that's connected to it?

Is it safe to assume that when either send() or recv() get interrupted
by a signal and returns EINTR, no actual data has been either sent or
consumed ? And if it's not, is there any other way around this ?

EINTR can only be return if 'send' or 'recv' have not sent or received
anything. Otherwise the connection would be left in an indeterminate state.

Does application correctly handle the case where recv() returns 0?
This indicates the TCP connection is closed by the other end.
It is incorrect to assume that a return of 0 in non-blocking mode
is the same as -1. The only correct action after receiving 0 bytes
(even in non-blocking mode), is to close the socket. If you attempt
to do another receive, the result could be that the recv() waits for
another event (more data or FIN), which can never happen since socket
is closed.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: Socket Server... Why two?
    ... I hope you meant non-blocking way ... The accept just accepts a connection and returns as soon ... It is just an information to the application that now one more socket ... nothing to do without that data, you can do a blocking recv(). ...
    (comp.unix.programmer)
  • Re: Socket Server... Why two?
    ... I hope you meant non-blocking way ... The accept just accepts a connection and returns as soon ... It is just an information to the application that now one more socket ... nothing to do without that data, you can do a blocking recv(). ...
    (comp.unix.programmer)
  • Re: Synchronization with CAsyncSocket in CE 6.0
    ... number of bytes in the first packet, followed by packets of, say 1024 bytes. ... recv() operations, reassembling it into the format that you need. ... don't forget that recv can return zero, if the socket has ... So, CAsyncSocket while possibly a bad way to do things, does not appear to ...
    (microsoft.public.windowsce.embedded)
  • Re: How can I tell when a remote TCP connection is closed?
    ... Recv only returns zero if the peer has ... to write to the socket and I get an broken pipe error. ... >I have a client with a TCP connection to a server. ... >need the code to be portable), is there a way that I can tell if the remote ...
    (microsoft.public.win32.programmer.networks)
  • Re: how to know if socket is still connected
    ... connection is still ok, if not will reconnect. ... If the server has closed the connection, then a recv() on the ... socket will raise an exception. ...
    (comp.lang.python)