From 09b01499b72a8ef532eabfebb8b7a5529ce6dd7c Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Thu, 14 Jul 2016 10:13:54 -0700 Subject: [PATCH] Wait for the reader fifo opening to block Signed-off-by: Tonis Tiigi (cherry picked from commit 0b2023130e285a0207be9fda4b22e1419997c552) Signed-off-by: Tibor Vass --- libcontainerd/container_linux.go | 3 ++- libcontainerd/process_linux.go | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libcontainerd/container_linux.go b/libcontainerd/container_linux.go index 52214167f5..324905ed41 100644 --- a/libcontainerd/container_linux.go +++ b/libcontainerd/container_linux.go @@ -225,8 +225,9 @@ func (ctr *container) discardFifos() { f := ctr.fifo(i) c := make(chan struct{}) go func() { + r := openReaderFromFifo(f) close(c) // this channel is used to not close the writer too early, before readonly open has been called. - io.Copy(ioutil.Discard, openReaderFromFifo(f)) + io.Copy(ioutil.Discard, r) }() <-c closeReaderFifo(f) // avoid blocking permanently on open if there is no writer side diff --git a/libcontainerd/process_linux.go b/libcontainerd/process_linux.go index 136a6e250c..3c48576fe2 100644 --- a/libcontainerd/process_linux.go +++ b/libcontainerd/process_linux.go @@ -79,7 +79,9 @@ func (r emptyReader) Read(b []byte) (int, error) { func openReaderFromFifo(fn string) io.Reader { r, w := io.Pipe() + c := make(chan struct{}) go func() { + close(c) stdoutf, err := os.OpenFile(fn, syscall.O_RDONLY, 0) if err != nil { r.CloseWithError(err) @@ -90,6 +92,7 @@ func openReaderFromFifo(fn string) io.Reader { w.Close() stdoutf.Close() }() + <-c // wait for the goroutine to get scheduled and syscall to block return r }