From 1f14d45e7dc17d397e437e3bd9b507e5316e6ed6 Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Sat, 21 Apr 2012 10:01:32 +1000 Subject: [PATCH] net: fix race between Close and Read Fixes #3507. Applied the suggested fix from rsc. If the connection is in closing state then errClosing will bubble up to the caller. The fix has been applied to udp, ip and unix as well as their code path include nil'ing c.fd on close. Func tests are available in the linked issue that verified the bug existed there as well. R=rsc, fullung, alex.brainman, mikioh.mikioh CC=golang-dev https://golang.org/cl/6002053 --- src/pkg/net/iprawsock_posix.go | 4 +--- src/pkg/net/tcpsock_posix.go | 4 +--- src/pkg/net/udpsock_posix.go | 4 +--- src/pkg/net/unixsock_posix.go | 4 +--- 4 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/pkg/net/iprawsock_posix.go b/src/pkg/net/iprawsock_posix.go index 6bbe67c3d9..9fc7ecdb94 100644 --- a/src/pkg/net/iprawsock_posix.go +++ b/src/pkg/net/iprawsock_posix.go @@ -83,9 +83,7 @@ func (c *IPConn) Close() error { if !c.ok() { return syscall.EINVAL } - err := c.fd.Close() - c.fd = nil - return err + return c.fd.Close() } // LocalAddr returns the local network address. diff --git a/src/pkg/net/tcpsock_posix.go b/src/pkg/net/tcpsock_posix.go index 15f8efdd70..f886a6b5c5 100644 --- a/src/pkg/net/tcpsock_posix.go +++ b/src/pkg/net/tcpsock_posix.go @@ -108,9 +108,7 @@ func (c *TCPConn) Close() error { if !c.ok() { return syscall.EINVAL } - err := c.fd.Close() - c.fd = nil - return err + return c.fd.Close() } // CloseRead shuts down the reading side of the TCP connection. diff --git a/src/pkg/net/udpsock_posix.go b/src/pkg/net/udpsock_posix.go index 9e820e1c57..9c6b6d3933 100644 --- a/src/pkg/net/udpsock_posix.go +++ b/src/pkg/net/udpsock_posix.go @@ -88,9 +88,7 @@ func (c *UDPConn) Close() error { if !c.ok() { return syscall.EINVAL } - err := c.fd.Close() - c.fd = nil - return err + return c.fd.Close() } // LocalAddr returns the local network address. diff --git a/src/pkg/net/unixsock_posix.go b/src/pkg/net/unixsock_posix.go index 37a2b1e09e..ea411a65f0 100644 --- a/src/pkg/net/unixsock_posix.go +++ b/src/pkg/net/unixsock_posix.go @@ -141,9 +141,7 @@ func (c *UnixConn) Close() error { if !c.ok() { return syscall.EINVAL } - err := c.fd.Close() - c.fd = nil - return err + return c.fd.Close() } // LocalAddr returns the local network address, a *UnixAddr.