]> Cypherpunks repositories - gostls13.git/commitdiff
net: use runtime.Keepalive for *netFD values
authorIan Lance Taylor <iant@golang.org>
Wed, 24 Aug 2016 00:47:11 +0000 (17:47 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 24 Aug 2016 16:57:50 +0000 (16:57 +0000)
The net package sets a finalizer on *netFD. I looked through all the
uses of *netFD in the package, looking for each case where a *netFD
was passed as an argument and the final reference to the argument was
not a function or method call. I added a call to runtime.KeepAlive after
each such final reference (there were only three).

The code is safe today without the KeepAlive calls because the compiler
keeps arguments alive for the duration of the function. However, that is
not a language requirement, so adding the KeepAlive calls ensures that
this code remains safe even if the compiler changes in the future.

Change-Id: I4e2bd7c5a946035dc509ccefb4828f72335a9ee3
Reviewed-on: https://go-review.googlesource.com/27650
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/fd_poll_nacl.go
src/net/fd_poll_runtime.go
src/net/fd_windows.go

index cda8b82c131642092d33fa3ad8f8337e32a5e25c..83987602a585a2c48cd56cabb34b71a6c7abe8b9 100644 (file)
@@ -5,6 +5,7 @@
 package net
 
 import (
+       "runtime"
        "syscall"
        "time"
 )
@@ -22,6 +23,7 @@ func (pd *pollDesc) evict() {
        pd.closing = true
        if pd.fd != nil {
                syscall.StopIO(pd.fd.sysfd)
+               runtime.KeepAlive(pd.fd)
        }
 }
 
index 6c1d095bd7f57849bcf682552e2237d7b34f749e..bfa62c9f2d08d79bf6e9bcd3613eb89a7bbbcd7d 100644 (file)
@@ -7,6 +7,7 @@
 package net
 
 import (
+       "runtime"
        "sync"
        "syscall"
        "time"
@@ -33,6 +34,7 @@ var serverInit sync.Once
 func (pd *pollDesc) init(fd *netFD) error {
        serverInit.Do(runtime_pollServerInit)
        ctx, errno := runtime_pollOpen(uintptr(fd.sysfd))
+       runtime.KeepAlive(fd)
        if errno != 0 {
                return syscall.Errno(errno)
        }
index b0b6769eb3cf74ab97e31bcaed07ea36a18f886b..d1c368e8836fa3a7231734fa509c81017a9bbc5b 100644 (file)
@@ -541,7 +541,7 @@ func (fd *netFD) acceptOne(rawsa []syscall.RawSockaddrAny, o *operation) (*netFD
                netfd.Close()
                return nil, os.NewSyscallError("setsockopt", err)
        }
-
+       runtime.KeepAlive(fd)
        return netfd, nil
 }