[go] net: use runtime.Keepalive for *netFD values

41 views
Skip to first unread message

Ian Lance Taylor (Gerrit)

unread,
Aug 23, 2016, 8:48:36 PM8/23/16
to golang-co...@googlegroups.com
Ian Lance Taylor uploaded a change:
https://go-review.googlesource.com/27650

net: use runtime.Keepalive for *netFD values

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
---
M src/net/fd_poll_nacl.go
M src/net/fd_poll_runtime.go
M src/net/fd_windows.go
3 files changed, 4 insertions(+), 1 deletion(-)



diff --git a/src/net/fd_poll_nacl.go b/src/net/fd_poll_nacl.go
index cda8b82..5fde2d2 100644
--- a/src/net/fd_poll_nacl.go
+++ b/src/net/fd_poll_nacl.go
@@ -22,6 +22,7 @@
pd.closing = true
if pd.fd != nil {
syscall.StopIO(pd.fd.sysfd)
+ runtime.KeepAlive(pd.fd)
}
}

diff --git a/src/net/fd_poll_runtime.go b/src/net/fd_poll_runtime.go
index 6c1d095..bfa62c9 100644
--- a/src/net/fd_poll_runtime.go
+++ b/src/net/fd_poll_runtime.go
@@ -7,6 +7,7 @@
package net

import (
+ "runtime"
"sync"
"syscall"
"time"
@@ -33,6 +34,7 @@
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)
}
diff --git a/src/net/fd_windows.go b/src/net/fd_windows.go
index b0b6769..d1c368e 100644
--- a/src/net/fd_windows.go
+++ b/src/net/fd_windows.go
@@ -541,7 +541,7 @@
netfd.Close()
return nil, os.NewSyscallError("setsockopt", err)
}
-
+ runtime.KeepAlive(fd)
return netfd, nil
}


--
https://go-review.googlesource.com/27650

Gobot Gobot (Gerrit)

unread,
Aug 23, 2016, 8:48:46 PM8/23/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net: use runtime.Keepalive for *netFD values

Patch Set 1:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=4273b10b

--
https://go-review.googlesource.com/27650
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Aug 23, 2016, 8:50:10 PM8/23/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net: use runtime.Keepalive for *netFD values

Patch Set 1:

Build is still in progress...
This change failed on nacl-386:
See
https://storage.googleapis.com/go-build-log/4273b10b/nacl-386_7e822ff4.log

Consult https://build.golang.org/ to see whether it's a new failure. Other
builds still in progress; subsequent failure notices suppressed until final
report.

Gobot Gobot (Gerrit)

unread,
Aug 23, 2016, 8:59:51 PM8/23/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net: use runtime.Keepalive for *netFD values

Patch Set 1: TryBot-Result-1

3 of 18 TryBots failed:
Failed on nacl-386:
https://storage.googleapis.com/go-build-log/4273b10b/nacl-386_7e822ff4.log
Failed on nacl-amd64p32:
https://storage.googleapis.com/go-build-log/4273b10b/nacl-amd64p32_ef92d995.log
Failed on misc-compile:
https://storage.googleapis.com/go-build-log/4273b10b/misc-compile_ece3e5cf.log

Consult https://build.golang.org/ to see whether they are new failures.

Ian Lance Taylor (Gerrit)

unread,
Aug 23, 2016, 9:28:43 PM8/23/16
to Gobot Gobot, golang-co...@googlegroups.com
Reviewers: Gobot Gobot

Ian Lance Taylor uploaded a new patch set:
https://go-review.googlesource.com/27650

net: use runtime.Keepalive for *netFD values

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
---
M src/net/fd_poll_nacl.go
M src/net/fd_poll_runtime.go
M src/net/fd_windows.go
3 files changed, 5 insertions(+), 1 deletion(-)

Gobot Gobot (Gerrit)

unread,
Aug 23, 2016, 9:28:46 PM8/23/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net: use runtime.Keepalive for *netFD values

Patch Set 2:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=275c2ebe
Gerrit-HasComments: No

Brad Fitzpatrick (Gerrit)

unread,
Aug 23, 2016, 9:32:05 PM8/23/16
to Ian Lance Taylor, Brad Fitzpatrick, Gobot Gobot, golang-co...@googlegroups.com
Brad Fitzpatrick has posted comments on this change.

net: use runtime.Keepalive for *netFD values

Patch Set 2: Code-Review+2

--
https://go-review.googlesource.com/27650
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>

Gobot Gobot (Gerrit)

unread,
Aug 23, 2016, 9:40:17 PM8/23/16
to Ian Lance Taylor, Brad Fitzpatrick, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

net: use runtime.Keepalive for *netFD values

Patch Set 2: TryBot-Result+1

TryBots are happy.

Ian Lance Taylor (Gerrit)

unread,
Aug 24, 2016, 12:58:02 PM8/24/16
to golang-...@googlegroups.com, Gobot Gobot, Brad Fitzpatrick, golang-co...@googlegroups.com
Ian Lance Taylor has submitted this change and it was merged.

net: use runtime.Keepalive for *netFD values

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 <ia...@golang.org>
Reviewed-by: Brad Fitzpatrick <brad...@golang.org>
TryBot-Result: Gobot Gobot <go...@golang.org>
---
M src/net/fd_poll_nacl.go
M src/net/fd_poll_runtime.go
M src/net/fd_windows.go
3 files changed, 5 insertions(+), 1 deletion(-)

Approvals:
Gobot Gobot: TryBots succeeded
Brad Fitzpatrick: Looks good to me, approved
Ian Lance Taylor: Run TryBots


--
https://go-review.googlesource.com/27650
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Reply all
Reply to author
Forward
0 new messages