Patchwork [gccgo] Avoid race condition

login
register
mail settings
Submitter Ian Taylor
Date July 28, 2010, 2:32 p.m.
Message ID <mcriq3zn0cj.fsf@google.com>
Download mbox | patch
Permalink /patch/60148/
State New
Headers show

Comments

Ian Taylor - July 28, 2010, 2:32 p.m.
I copied this patch from the other Go library to avoid a race condition
which gccgo tends to trigger when running the tests.  I didn't want to
take the time to do a full library upgrade right now.  Committed to
gccgo branch.

Ian

Patch

diff -r 18be60ce1a6e libgo/go/io/pipe.go
--- a/libgo/go/io/pipe.go	Wed Jul 28 06:47:29 2010 -0700
+++ b/libgo/go/io/pipe.go	Wed Jul 28 07:30:40 2010 -0700
@@ -52,6 +52,13 @@ 
 		case <-p.done:
 			if ndone++; ndone == 2 {
 				// both reader and writer are gone
+				// close out any existing i/o
+				if r1 == nil {
+					p.r2 <- pipeResult{0, os.EINVAL}
+				}
+				if w1 == nil {
+					p.w2 <- pipeResult{0, os.EINVAL}
+				}
 				return
 			}
 			continue
@@ -89,6 +96,11 @@ 
 				p.r2 <- pipeResult{0, werr}
 				continue
 			}
+			if rerr != nil {
+				// read end is closed
+				p.r2 <- pipeResult{0, os.EINVAL}
+				continue
+			}
 			r1 = nil // disable Read until this one is done
 		case wb = <-w1:
 			if rerr != nil {
@@ -96,6 +108,11 @@ 
 				p.w2 <- pipeResult{0, rerr}
 				continue
 			}
+			if werr != nil {
+				// write end is closed
+				p.w2 <- pipeResult{0, os.EINVAL}
+				continue
+			}
 			w1 = nil // disable Write until this one is done
 		}