Browse code

Wait for the reader fifo opening to block

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 0b2023130e285a0207be9fda4b22e1419997c552)
Signed-off-by: Tibor Vass <tibor@docker.com>

Tonis Tiigi authored on 2016/07/15 02:13:54
Showing 2 changed files
... ...
@@ -225,8 +225,9 @@ func (ctr *container) discardFifos() {
225 225
 		f := ctr.fifo(i)
226 226
 		c := make(chan struct{})
227 227
 		go func() {
228
+			r := openReaderFromFifo(f)
228 229
 			close(c) // this channel is used to not close the writer too early, before readonly open has been called.
229
-			io.Copy(ioutil.Discard, openReaderFromFifo(f))
230
+			io.Copy(ioutil.Discard, r)
230 231
 		}()
231 232
 		<-c
232 233
 		closeReaderFifo(f) // avoid blocking permanently on open if there is no writer side
... ...
@@ -79,7 +79,9 @@ func (r emptyReader) Read(b []byte) (int, error) {
79 79
 
80 80
 func openReaderFromFifo(fn string) io.Reader {
81 81
 	r, w := io.Pipe()
82
+	c := make(chan struct{})
82 83
 	go func() {
84
+		close(c)
83 85
 		stdoutf, err := os.OpenFile(fn, syscall.O_RDONLY, 0)
84 86
 		if err != nil {
85 87
 			r.CloseWithError(err)
... ...
@@ -90,6 +92,7 @@ func openReaderFromFifo(fn string) io.Reader {
90 90
 		w.Close()
91 91
 		stdoutf.Close()
92 92
 	}()
93
+	<-c // wait for the goroutine to get scheduled and syscall to block
93 94
 	return r
94 95
 }
95 96