mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
net: do not use RLock around Accept
It might be non-blocking, but it also might be blocking. Cannot take the chance, as Accept might block indefinitely and make it impossible to acquire ForkLock exclusively (during fork+exec). Fixes #4737. R=golang-dev, dave, iant, mikioh.mikioh CC=golang-dev https://golang.org/cl/7309050
This commit is contained in:
parent
3c1dfb2b9a
commit
18441e8ade
3 changed files with 9 additions and 6 deletions
|
|
@ -661,6 +661,9 @@ func (fd *netFD) dup() (f *os.File, err error) {
|
||||||
syscall.ForkLock.RUnlock()
|
syscall.ForkLock.RUnlock()
|
||||||
|
|
||||||
// We want blocking mode for the new fd, hence the double negative.
|
// We want blocking mode for the new fd, hence the double negative.
|
||||||
|
// This also puts the old fd into blocking mode, meaning that
|
||||||
|
// I/O will block the thread instead of letting us use the epoll server.
|
||||||
|
// Everything will still work, just with more threads.
|
||||||
if err = syscall.SetNonblock(ns, false); err != nil {
|
if err = syscall.SetNonblock(ns, false); err != nil {
|
||||||
return nil, &OpError{"setnonblock", fd.net, fd.laddr, err}
|
return nil, &OpError{"setnonblock", fd.net, fd.laddr, err}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,14 +50,14 @@ func accept(fd int) (int, syscall.Sockaddr, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// See ../syscall/exec_unix.go for description of ForkLock.
|
// See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
// It is okay to hold the lock across syscall.Accept
|
// It is probably okay to hold the lock across syscall.Accept
|
||||||
// because we have put fd.sysfd into non-blocking mode.
|
// because we have put fd.sysfd into non-blocking mode.
|
||||||
syscall.ForkLock.RLock()
|
// However, a call to the File method will put it back into
|
||||||
|
// blocking mode. We can't take that risk, so no use of ForkLock here.
|
||||||
nfd, sa, err = syscall.Accept(fd)
|
nfd, sa, err = syscall.Accept(fd)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
syscall.CloseOnExec(nfd)
|
syscall.CloseOnExec(nfd)
|
||||||
}
|
}
|
||||||
syscall.ForkLock.RUnlock()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, nil, err
|
return -1, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,14 +35,14 @@ func sysSocket(f, t, p int) (int, error) {
|
||||||
// descriptor as nonblocking and close-on-exec.
|
// descriptor as nonblocking and close-on-exec.
|
||||||
func accept(fd int) (int, syscall.Sockaddr, error) {
|
func accept(fd int) (int, syscall.Sockaddr, error) {
|
||||||
// See ../syscall/exec_unix.go for description of ForkLock.
|
// See ../syscall/exec_unix.go for description of ForkLock.
|
||||||
// It is okay to hold the lock across syscall.Accept
|
// It is probably okay to hold the lock across syscall.Accept
|
||||||
// because we have put fd.sysfd into non-blocking mode.
|
// because we have put fd.sysfd into non-blocking mode.
|
||||||
syscall.ForkLock.RLock()
|
// However, a call to the File method will put it back into
|
||||||
|
// blocking mode. We can't take that risk, so no use of ForkLock here.
|
||||||
nfd, sa, err := syscall.Accept(fd)
|
nfd, sa, err := syscall.Accept(fd)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
syscall.CloseOnExec(nfd)
|
syscall.CloseOnExec(nfd)
|
||||||
}
|
}
|
||||||
syscall.ForkLock.RUnlock()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return -1, nil, err
|
return -1, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue