1
0
mirror of https://github.com/golang/go synced 2024-11-18 13:44:48 -07:00

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
This commit is contained in:
Dmitriy Vyukov 2013-07-29 20:01:13 +04:00
parent 6350e45892
commit b8734748b6

View File

@ -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}
}