Non-blocking sockets, connect(), and socket states

From: Bernard Blackham (bernard_at_blackham.com.au)
Date: 04/28/05

  • Next message: Mingming Cao: "Re: [Ext2-devel] [RFC] Adding multiple block allocation"
    Date:	Thu, 28 Apr 2005 18:34:51 +0800
    To: linux-kernel@vger.kernel.org
    
    

    [previously sent to the allegedly dead netdev@oss.sgi.com]

    Hi,

    Through playing with tcpcp[1], I've found out about a quirk in the
    kernel's handling of non-blocking connection-based sockets. The
    sk_socket->state value can take on one of SS_FREE, SS_UNCONNECTED,
    SS_CONNECTING, SS_CONNECTED or SS_DISCONNECTING. On a standard
    *blocking*, connection-oriented socket (eg, TCP), after connect()
    returns, sk_socket->state will be SS_CONNECTED.

    However, if the socket is placed into non-blocking mode before the
    connect() call, connect() returns immediately with EINPROGRESS, and
    the sk_socket->state is set to SS_CONNECTING. When the socket
    finally does connect, the application is notified via poll(), but
    the state remains as SS_CONNECTING (which causes issues for tcpcp,
    though doesn't appear to have any other externally visible
    implications).

    Werner, the author of tcpcp, suggests that the application should
    call connect() on the socket a second time, after the successful
    connection, to force the sk_socket->state value to SS_CONNECTED.

    Should it be the kernel's responsibility to set SS_CONNECTED when
    the connection is established? Or should I go file bugs and submit
    patches on all the applications that use non-blocking sockets and
    don't call connect() a second time?

    Thanks in advance,

    Bernard.

    [1] http://tcpcp.sf.net/

    -- 
     Bernard Blackham <bernard at blackham dot com dot au>
    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at  http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at  http://www.tux.org/lkml/
    

  • Next message: Mingming Cao: "Re: [Ext2-devel] [RFC] Adding multiple block allocation"

    Relevant Pages

    • Re: [6.x] problem with AIO, non-blocking sockets on freebSD and IE7 on windows.
      ... Yes, Blame Microsoft, but we are breaking the TCP spec, not them. ... A TCP socket isn't the same thing as a named pape or FIFO. ... Using non-blocking I/O does not mean one can suddenly shortcut the FINWAIT-1 and FINWAIT-2 states before going into TIME_WAIT, nor the 2 * MSL timeout before the TCP control block is allowed to go away. ... Otherwise, you might end up sending a RST to a dup'ed packet like a stray ACK, which seems to be almost exactly the problem at hand. ...
      (freebsd-net)
    • Trying to learn Non-Blocking sockets on Solaris 8, please help! ready to fork() it.
      ... how to setup a non-blocking socket. ... I've found limited information on ... opening a socket and using the ioctlto set it to non-blocking ... Whats the differance between fcntl and ioctl? ...
      (comp.unix.programmer)
    • Re: java socket i/o with callback/non-blocking
      ... Basically I want to have some subroutine to be called whenever there is data available to read on a socket object without having to poll it. ... Note also that the technique described is blocking, not non-blocking, as indeed is the technique you described even if the channel is in non-blocking mode. ... For just one socket, you are probably much better off to dump all the NIO stuff, and simply use blocking I/O in its own thread. ...
      (comp.lang.java.programmer)
    • Re: [6.x] problem with AIO, non-blocking sockets on freebSD and IE7 on windows.
      ... Yes, Blame Microsoft, but we are breaking the TCP spec, not them. ... A TCP socket isn't the same thing as a named pape or FIFO. ... Using non-blocking I/O does not mean one can suddenly shortcut the FINWAIT-1 and FINWAIT-2 states before going into TIME_WAIT, nor the 2 * MSL timeout before the TCP control block is allowed to go away. ... Otherwise, you might end up sending a RST to a dup'ed packet like a stray ACK, which seems to be almost exactly the problem at hand. ...
      (freebsd-net)
    • Re: [6.x] problem with AIO, non-blocking sockets on freebSD and IE7 on windows.
      ... needs to set eh non-blocking socket option and use kqueue or similar ... This seems to be the correct behaviour. ... non-blocking IO in the manner it was intended. ...
      (freebsd-net)