Commit 3b6de5e8 authored by Dmitriy Vyukov's avatar Dmitriy Vyukov

net: fix memory leak on unix

If netFD is closed by finalizer, runtime netpoll descriptor is not freed.

R=golang-dev, dave, alex.brainman
CC=golang-dev
https://golang.org/cl/12037043
parent cd2eb487
...@@ -9,6 +9,7 @@ package net ...@@ -9,6 +9,7 @@ package net
import ( import (
"io" "io"
"os" "os"
"runtime"
"sync" "sync"
"syscall" "syscall"
"time" "time"
...@@ -29,7 +30,6 @@ type netFD struct { ...@@ -29,7 +30,6 @@ type netFD struct {
family int family int
sotype int sotype int
isConnected bool isConnected bool
sysfile *os.File
net string net string
laddr Addr laddr Addr
raddr Addr raddr Addr
...@@ -70,7 +70,7 @@ func newFD(fd, family, sotype int, net string) (*netFD, error) { ...@@ -70,7 +70,7 @@ func newFD(fd, family, sotype int, net string) (*netFD, error) {
func (fd *netFD) setAddr(laddr, raddr Addr) { func (fd *netFD) setAddr(laddr, raddr Addr) {
fd.laddr = laddr fd.laddr = laddr
fd.raddr = raddr fd.raddr = raddr
fd.sysfile = os.NewFile(uintptr(fd.sysfd), fd.net) runtime.SetFinalizer(fd, (*netFD).Close)
} }
func (fd *netFD) name() string { func (fd *netFD) name() string {
...@@ -129,15 +129,11 @@ func (fd *netFD) decref() { ...@@ -129,15 +129,11 @@ func (fd *netFD) decref() {
fd.sysref-- fd.sysref--
if fd.closing && fd.sysref == 0 { if fd.closing && fd.sysref == 0 {
// Poller may want to unregister fd in readiness notification mechanism, // Poller may want to unregister fd in readiness notification mechanism,
// so this must be executed before sysfile.Close(). // so this must be executed before closesocket.
fd.pd.Close() fd.pd.Close()
if fd.sysfile != nil { closesocket(fd.sysfd)
fd.sysfile.Close()
fd.sysfile = nil
} else {
closesocket(fd.sysfd)
}
fd.sysfd = -1 fd.sysfd = -1
runtime.SetFinalizer(fd, nil)
} }
fd.sysmu.Unlock() fd.sysmu.Unlock()
} }
......
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