FAQ
Reviewers: rsc, ality,

Message:
Hello rsc@golang.org, ality@pbrane.org (cc:
golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go


Description:
net, os, syscall: don't hold ForkLock during blocked opens on Plan 9

If os.OpenFile holds ForkLock on files that block opens,
then threads that simultaneously try to do fork-exec will
get hung up (until the open succeeds).

This CL introduces a pseudo-open-flag O_CANBLOCK,
which os.OpenFile interprets as a trigger to *not* hold
ForkLock. Here, it is used by the Plan 9 implementation
of Accept() (which tries to open a network "listen" file)
to avoid the aforementioned problem.

Please review this at https://codereview.appspot.com/7235066/

Affected files:
M src/pkg/net/ipsock_plan9.go
M src/pkg/os/file_plan9.go
M src/pkg/syscall/zerrors_plan9_386.go
M src/pkg/syscall/zerrors_plan9_amd64.go


Index: src/pkg/net/ipsock_plan9.go
===================================================================
--- a/src/pkg/net/ipsock_plan9.go
+++ b/src/pkg/net/ipsock_plan9.go
@@ -9,6 +9,7 @@
import (
"errors"
"os"
+ "syscall"
)

// /sys/include/ape/sys/socket.h:/SOMAXCONN
@@ -150,7 +151,7 @@
}

func (l *netFD) acceptPlan9() (*netFD, error) {
- f, err := os.Open(l.dir + "/listen")
+ f, err := os.OpenFile(l.dir+"/listen", os.O_RDONLY|syscall.O_CANBLOCK, 0)
if err != nil {
return nil, err
}
Index: src/pkg/os/file_plan9.go
===================================================================
--- a/src/pkg/os/file_plan9.go
+++ b/src/pkg/os/file_plan9.go
@@ -86,6 +86,7 @@
excl bool
trunc bool
append bool
+ block bool
)

if flag&O_CREATE == O_CREATE {
@@ -103,8 +104,14 @@
flag = flag &^ O_APPEND
append = true
}
+ if flag&syscall.O_CANBLOCK == syscall.O_CANBLOCK {
+ flag = flag &^ syscall.O_CANBLOCK
+ block = true
+ }

- syscall.ForkLock.RLock()
+ if !block {
+ syscall.ForkLock.RLock()
+ }
if (create && trunc) || excl {
fd, e = syscall.Create(name, flag, syscallMode(perm))
} else {
@@ -117,7 +124,9 @@
}
}
}
- syscall.ForkLock.RUnlock()
+ if !block {
+ syscall.ForkLock.RUnlock()
+ }

if e != nil {
return nil, &PathError{"open", name, e}
Index: src/pkg/syscall/zerrors_plan9_386.go
===================================================================
--- a/src/pkg/syscall/zerrors_plan9_386.go
+++ b/src/pkg/syscall/zerrors_plan9_386.go
@@ -13,6 +13,7 @@
O_APPEND = 0x00400
O_NOCTTY = 0x00000
O_NONBLOCK = 0x00000
+ O_CANBLOCK = 0x00060
O_SYNC = 0x00000
O_ASYNC = 0x00000

Index: src/pkg/syscall/zerrors_plan9_amd64.go
===================================================================
--- a/src/pkg/syscall/zerrors_plan9_amd64.go
+++ b/src/pkg/syscall/zerrors_plan9_amd64.go
@@ -13,6 +13,7 @@
O_APPEND = 0x00400
O_NOCTTY = 0x00000
O_NONBLOCK = 0x00000
+ O_CANBLOCK = 0x00060
O_SYNC = 0x00000
O_ASYNC = 0x00000



--

---
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Rsc at Jan 30, 2013 at 4:14 pm
    https://codereview.appspot.com/7235066/diff/3/src/pkg/net/ipsock_plan9.go
    File src/pkg/net/ipsock_plan9.go (right):

    https://codereview.appspot.com/7235066/diff/3/src/pkg/net/ipsock_plan9.go#newcode154
    src/pkg/net/ipsock_plan9.go:154: f, err := os.OpenFile(l.dir+"/listen",
    os.O_RDONLY|syscall.O_CANBLOCK, 0)
    This is hardly the only open that can block on Plan 9. I think you have
    to accept the race and just never hold the ForkLock across an open.

    https://codereview.appspot.com/7235066/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Akshat Kumar at Jan 30, 2013 at 4:21 pm
    Yeah, it can happen from user code as well, and the
    trade-off is between leaking fds and being able to
    use os/exec properly. The latter seems more important,
    indeed.
    On 30 January 2013 16:14, wrote:

    https://codereview.appspot.com/7235066/diff/3/src/pkg/net/ipsock_plan9.go
    File src/pkg/net/ipsock_plan9.go (right):

    https://codereview.appspot.com/7235066/diff/3/src/pkg/net/ipsock_plan9.go#newcode154
    src/pkg/net/ipsock_plan9.go:154: f, err := os.OpenFile(l.dir+"/listen",
    os.O_RDONLY|syscall.O_CANBLOCK, 0)
    This is hardly the only open that can block on Plan 9. I think you have
    to accept the race and just never hold the ForkLock across an open.

    https://codereview.appspot.com/7235066/
    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Seed at Jan 30, 2013 at 4:45 pm
    PTAL.

    Also updated the description.


    https://codereview.appspot.com/7235066/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rsc at Jan 30, 2013 at 5:38 pm
    LGTM


    https://codereview.appspot.com/7235066/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Rsc at Jan 30, 2013 at 5:41 pm
    *** Submitted as
    https://code.google.com/p/go/source/detail?r=0e84a5cf4e81 ***

    os: don't hold ForkLock across opens on Plan 9

    If os.OpenFile holds ForkLock on files that block opens,
    then threads that simultaneously try to do fork-exec will
    get hung up (until the open succeeds). Blocked opens are
    common enough on Plan 9 that protecting against fd leaks
    into fork-execs means not being able to do fork-execs
    properly in the general case. Thus, we forgo taking the
    lock.

    R=rsc, ality
    CC=golang-dev
    https://codereview.appspot.com/7235066

    Committer: Russ Cox <rsc@golang.org>


    https://codereview.appspot.com/7235066/

    --

    ---
    You received this message because you are subscribed to the Google Groups "golang-dev" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedJan 30, '13 at 1:11p
activeJan 30, '13 at 5:41p
posts6
users2
websitegolang.org

2 users in discussion

Rsc: 3 posts Seed: 3 posts

People

Translate

site design / logo © 2022 Grokbase