Re: [go-nuts] Best way to reliably detect that a TCP is connection

2,985 views
Skip to first unread message

Kyle Lemons

unread,
Aug 23, 2012, 1:14:47 PM8/23/12
to j...@cloudflare.com, golan...@googlegroups.com
Are you not reading from or writing to the connection?  Both of these operations should indicate an error in the socket, such as the remote side being closed.

On Thu, Aug 23, 2012 at 12:33 AM, <j...@cloudflare.com> wrote:
I need to detect that a TCP connection has been closed by a remote server. I am currently doing this (where c is a net.Conn):

one := make([]byte, 1)
c.SetReadDeadline(time.Now())
if _, err := c.Read(one); err == io.EOF {
c.Close()
} else {
var zero time.Time
c.SetReadDeadline(zero)
}

Is this correct? Is there a simply or faster way to do this?

John.



Message has been deleted

John Graham-Cumming

unread,
Aug 24, 2012, 5:06:58 AM8/24/12
to Peter Russell, golan...@googlegroups.com
On Fri, Aug 24, 2012 at 5:35 AM, Peter Russell <retep....@ymail.com> wrote:
On Thursday, August 23, 2012 10:14:47 AM UTC-7, Kyle Lemons wrote:
Are you not reading from or writing to the connection?  Both of these operations should indicate an error in the socket, such as the remote side being closed.

To be more specific, a Read or a Write will detect a clean close by the remote side.

 If you want to detect all disconnects by the remote side, then you need to use some sort of pinging and read with a timeout.

The situation I was seeing was that a Write would succeed but a Read would fail with io.EOF. The remote is nginx and after some period it is dropping the TCP connection. My code was attempting then to send a request to the server and, oddly, the Write would succeed. I did not expect the behaviour.

John.

Thomas Bushnell, BSG

unread,
Aug 24, 2012, 5:18:24 AM8/24/12
to John Graham-Cumming, Peter Russell, golang-nuts

That's TCP. The write succeeds when it's in your kernel's buffers, not when the remote end has ACKd it.

Thomas

John Graham-Cumming

unread,
Aug 24, 2012, 6:17:21 AM8/24/12
to Thomas Bushnell, BSG, Peter Russell, golang-nuts
On Fri, Aug 24, 2012 at 10:18 AM, Thomas Bushnell, BSG <tbus...@google.com> wrote:

That's TCP. The write succeeds when it's in your kernel's buffers, not when the remote end has ACKd it.

So, is what I'm doing a reasonable way to detect a closed connection?

John.
 

Kyle Lemons

unread,
Aug 24, 2012, 12:02:50 PM8/24/12
to John Graham-Cumming, Thomas Bushnell, BSG, Peter Russell, golang-nuts
Usually you'd have a goroutine waiting in Read() and signal completion when it returns.

Matt Joiner

unread,
Nov 29, 2012, 7:37:12 AM11/29/12
to golan...@googlegroups.com, Thomas Bushnell, BSG, Peter Russell
I'm not sure that even testing for a single byte is sufficient, since you won't know the connection has closed until you empty the kernel's buffers.

I asked a similar question on SO you might find insightful: http://stackoverflow.com/q/5686490/149482 

I'm sure there are OS-specific calls, stuff you can do parse from /proc with the socket's inode etc. that might give you better information.
Reply all
Reply to author
Forward
0 new messages