Commit 9192ec2e authored by Michael Hoisie's avatar Michael Hoisie Committed by Russ Cox

netFD: fix race between Close and Read/Write

Fixes #783.

R=rsc, cw
CC=golang-dev
https://golang.org/cl/1207043
parent 79814069
...@@ -362,13 +362,16 @@ func (fd *netFD) Close() os.Error { ...@@ -362,13 +362,16 @@ func (fd *netFD) Close() os.Error {
} }
func (fd *netFD) Read(p []byte) (n int, err os.Error) { func (fd *netFD) Read(p []byte) (n int, err os.Error) {
if fd == nil || fd.sysfile == nil { if fd == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
fd.rio.Lock() fd.rio.Lock()
defer fd.rio.Unlock() defer fd.rio.Unlock()
fd.incref() fd.incref()
defer fd.decref() defer fd.decref()
if fd.sysfile == nil {
return 0, os.EINVAL
}
if fd.rdeadline_delta > 0 { if fd.rdeadline_delta > 0 {
fd.rdeadline = pollserver.Now() + fd.rdeadline_delta fd.rdeadline = pollserver.Now() + fd.rdeadline_delta
} else { } else {
...@@ -430,13 +433,16 @@ func (fd *netFD) ReadFrom(p []byte) (n int, sa syscall.Sockaddr, err os.Error) { ...@@ -430,13 +433,16 @@ func (fd *netFD) ReadFrom(p []byte) (n int, sa syscall.Sockaddr, err os.Error) {
} }
func (fd *netFD) Write(p []byte) (n int, err os.Error) { func (fd *netFD) Write(p []byte) (n int, err os.Error) {
if fd == nil || fd.sysfile == nil { if fd == nil {
return 0, os.EINVAL return 0, os.EINVAL
} }
fd.wio.Lock() fd.wio.Lock()
defer fd.wio.Unlock() defer fd.wio.Unlock()
fd.incref() fd.incref()
defer fd.decref() defer fd.decref()
if fd.sysfile == nil {
return 0, os.EINVAL
}
if fd.wdeadline_delta > 0 { if fd.wdeadline_delta > 0 {
fd.wdeadline = pollserver.Now() + fd.wdeadline_delta fd.wdeadline = pollserver.Now() + fd.wdeadline_delta
} else { } else {
...@@ -444,6 +450,7 @@ func (fd *netFD) Write(p []byte) (n int, err os.Error) { ...@@ -444,6 +450,7 @@ func (fd *netFD) Write(p []byte) (n int, err os.Error) {
} }
nn := 0 nn := 0
var oserr os.Error var oserr os.Error
for { for {
n, errno := syscall.Write(fd.sysfile.Fd(), p[nn:]) n, errno := syscall.Write(fd.sysfile.Fd(), p[nn:])
if n > 0 { if n > 0 {
......
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