net: fix memory leaks on windows
Close netpoll descriptor along with socket.
Ensure that error paths close the descriptor as well.

R=golang-dev, mikioh.mikioh, alex.brainman
CC=golang-dev
https://golang.org/cl/11987043
diff --git a/src/pkg/net/fd_windows.go b/src/pkg/net/fd_windows.go
index 548c04e..5d58462 100644
--- a/src/pkg/net/fd_windows.go
+++ b/src/pkg/net/fd_windows.go
@@ -288,7 +288,7 @@
 func (fd *netFD) setAddr(laddr, raddr Addr) {
 	fd.laddr = laddr
 	fd.raddr = raddr
-	runtime.SetFinalizer(fd, (*netFD).closesocket)
+	runtime.SetFinalizer(fd, (*netFD).Close)
 }
 
 // Make new connection.
@@ -366,6 +366,9 @@
 	fd.sysmu.Lock()
 	fd.sysref--
 	if fd.closing && fd.sysref == 0 && fd.sysfd != syscall.InvalidHandle {
+		// Poller may want to unregister fd in readiness notification mechanism,
+		// so this must be executed before closesocket.
+		fd.pd.Close()
 		closesocket(fd.sysfd)
 		fd.sysfd = syscall.InvalidHandle
 		// no need for a finalizer anymore
@@ -409,10 +412,6 @@
 	return fd.shutdown(syscall.SHUT_WR)
 }
 
-func (fd *netFD) closesocket() error {
-	return closesocket(fd.sysfd)
-}
-
 // Read from network.
 
 type readOp struct {
@@ -585,14 +584,14 @@
 	o.newsock = s
 	_, err = iosrv.ExecIO(&o)
 	if err != nil {
-		closesocket(s)
+		netfd.Close()
 		return nil, err
 	}
 
 	// Inherit properties of the listening socket.
 	err = syscall.Setsockopt(s, syscall.SOL_SOCKET, syscall.SO_UPDATE_ACCEPT_CONTEXT, (*byte)(unsafe.Pointer(&fd.sysfd)), int32(unsafe.Sizeof(fd.sysfd)))
 	if err != nil {
-		closesocket(s)
+		netfd.Close()
 		return nil, &OpError{"Setsockopt", fd.net, fd.laddr, err}
 	}