Commit b8734748 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

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
parent 6350e458
......@@ -288,7 +288,7 @@ func newFD(fd syscall.Handle, family, sotype int, net string) (*netFD, error) {
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 @@ func (fd *netFD) decref() {
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 @@ func (fd *netFD) CloseWrite() error {
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 @@ func (fd *netFD) accept(toAddr func(syscall.Sockaddr) Addr) (*netFD, error) {
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}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment